You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by Jeff Melching <jm...@us.ibm.com> on 2008/01/28 19:17:13 UTC
What is special about an entity with Id = 0?
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.
Re: What is special about an entity with Id = 0?
Posted by Jeff Melching <jm...@us.ibm.com>.
Obviously on the first one :)
I am using a generated id using a db sequence, so I never manually set the
id, and openjpa will complain if I try to do so.
I think the cache is being checked to try and save on some performance,
but I found that my test case and all test cases in the openjpa source
pass if I comment out the 2 lines that check the cache.
From:
"Patrick Linskey" <pl...@gmail.com>
To:
users@openjpa.apache.org
Date:
01/29/2008 12:00 AM
Subject:
Re: What is special about an entity with Id = 0?
0 is the Java language default for fields of type long.
What happens if you set your id field to be something other than 0
(the default) in your source file? -1, for example.
Alternately, what happens if you wait until after you set the ID to
call persist() on the record?
-Patrick
On Jan 28, 2008 10:17 AM, Jeff Melching <jm...@us.ibm.com> wrote:
> 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.
>
--
Patrick Linskey
202 669 5907
Re: What is special about an entity with Id = 0?
Posted by Patrick Linskey <pl...@gmail.com>.
0 is the Java language default for fields of type long.
What happens if you set your id field to be something other than 0
(the default) in your source file? -1, for example.
Alternately, what happens if you wait until after you set the ID to
call persist() on the record?
-Patrick
On Jan 28, 2008 10:17 AM, Jeff Melching <jm...@us.ibm.com> wrote:
> 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.
>
--
Patrick Linskey
202 669 5907