openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jeff Melching <jme...@us.ibm.com>
Subject What is special about an entity with Id = 0?
Date Mon, 28 Jan 2008 18:17:13 GMT
It seems that if I have a persisted entity with an id of 0, and I look up 
that entity with em.find(XYZ.class, 0), then in the same transaction I try 
to persist a new entity of type XYZ, I get the following exception: 

org.apache.openjpa.persistence.EntityExistsException: An object of type 
"XYZ" with oid "XYZ-0" already exists in this context; another cannot be 
persisted.

Why is the id of zero special? I see that the LongId class is defaulting 
the id to 0 if it is a new object but I don't think it shouldn't be doing 
this.

The problem seems to be here in BrokerImpl, see my comments with !!!--- :

 // create id for instance
            if (id == null) {
                if (meta.getIdentityType() == 
ClassMetaData.ID_APPLICATION)
                    id = ApplicationIds.create(pc, meta);  !!!--- this is 
returning 0, but for a new entity, shouldn't it return the id from the 
generator?
                else if (meta.getIdentityType() == 
ClassMetaData.ID_UNKNOWN)
                    throw new UserException(_loc.get("meta-unknownid", 
meta));
                else
                    id = StateManagerId.newInstance(this);
            }

            // make sure we don't already have the instance cached
            StateManagerImpl other = getStateManagerImplById(id, false);
            if (other != null && !other.isDeleted() && !other.isNew()) 
                throw new ObjectExistsException(_loc.get("cache-exists",
                    obj.getClass().getName(), id)).setFailedObject(obj); 
!!!--- so in turn we get here because same type exists with id of 0.

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message