You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by holod <se...@gmail.com> on 2008/12/09 12:35:42 UTC
Can't persist detached object
I'm in strange situation.
I have an entity Person.
I have an EJB stateless bean which has three methods:
1. public createPerson(Person newPerson){...}
2. public updatePerson(Person newPerson, Person oldPerson){...}
3. private attachFields(Person detachedPerson){...}
When I'm creating Person, my logic goes this way:
1. Outside bean Person is constructed, but It can have detached entities.
2. Invoke stateless bean -> createPerson(newPerson);
3. Inside this method attachFields(newPerson) in invoked.
Everything goes fine! All detached fields (for example only id of some
referenced entity is specified) are attached, Person is successfully
persisted.
When I'm trying to update person, I get strange Exception (it's not strange,
but I can't get why does in happen).
1. Invoke updatePerson(newPerson, oldPerson); oldPerson is attached entity,
newPerson is a raw entity with detached referenced enitities.
2. delete referenced entities from oldPerson.
3. use em.flush(); to force deletion data from db.
4. set new fields from newPerson to oldPerson.
5 attachFields(oldPerson) now, oldPerson CAN'T have detached entities
6. use em.merge(oldPerson) to save newly created referenced entities
And... I get exception: can't persist detached entity! and JPA prints entity
class bla.bla2.SomeEntityClass + [it's key].
But It can't be so! I get this entity from EntityManager and save it to
field of oldPerson.
The same procedure works fine when I create Person.
I don't understand what's going on.
Is there any possibility to know more details about such exception? I've put
logger.debug everywhere and I clearly see how every field of person is taked
from EntityManager.
--
View this message in context: http://n2.nabble.com/Can%27t-persist-detached-object-tp1633496p1633496.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.
Re: Can't persist detached object
Posted by holod <se...@gmail.com>.
Hi, the problem was in my code.
1. I get old Entity from DB. (db_entity)
2. I have new Entity from outside. (out_entity)
3. I take values from out_entity and set them to db_entity. It's plain
update.
4. I persist db_entity with new values
BUT!
While replacing values db_entity I have removed some other entities stored
into db_entity fields. And I forgot to clean their links from other objects.
That is why I got the exeption. My removed entities really were not fully
removed.
I've reviewed my code, cleaned all links to entites I wanted to delete and
finally problem is solved.
I was inattentive, that is why I made such stupid mistake and I was hard to
catch such bag.
2009/1/19 Ognjen Blagojevic-3 (via Nabble) <
ml-user+124662-1888642945@n2.nabble.com<ml...@n2.nabble.com>
>
> Hi Holod,
>
> What is your enviroment? Which app. server are you using?
>
> Maybe you could try to describe step 4. in more detail? How do you copy
> referenced entities?
>
> -Ognjen
>
> holod wrote:
> > I'm in strange situation.
> > I have an entity Person.
> > I have an EJB stateless bean which has three methods:
> > 1. public createPerson(Person newPerson){...}
> > 2. public updatePerson(Person newPerson, Person oldPerson){...}
> > 3. private attachFields(Person detachedPerson){...}
> >
> > When I'm creating Person, my logic goes this way:
> > 1. Outside bean Person is constructed, but It can have detached entities.
>
> > 2. Invoke stateless bean -> createPerson(newPerson);
> > 3. Inside this method attachFields(newPerson) in invoked.
> >
> > Everything goes fine! All detached fields (for example only id of some
> > referenced entity is specified) are attached, Person is successfully
> > persisted.
> >
> > When I'm trying to update person, I get strange Exception (it's not
> strange,
> > but I can't get why does in happen).
> > 1. Invoke updatePerson(newPerson, oldPerson); oldPerson is attached
> entity,
> > newPerson is a raw entity with detached referenced enitities.
> > 2. delete referenced entities from oldPerson.
> > 3. use em.flush(); to force deletion data from db.
> > 4. set new fields from newPerson to oldPerson.
> > 5 attachFields(oldPerson) now, oldPerson CAN'T have detached entities
> > 6. use em.merge(oldPerson) to save newly created referenced entities
> >
> > And... I get exception: can't persist detached entity! and JPA prints
> entity
> > class bla.bla2.SomeEntityClass + [it's key].
> > But It can't be so! I get this entity from EntityManager and save it to
> > field of oldPerson.
> > The same procedure works fine when I create Person.
> >
> > I don't understand what's going on.
> >
> > Is there any possibility to know more details about such exception? I've
> put
> > logger.debug everywhere and I clearly see how every field of person is
> taked
> > from EntityManager.
> >
> >
> >
> >
> >
> >
> >
>
>
>
> ------------------------------
> This email is a reply to your post @
> http://n2.nabble.com/Can%27t-persist-detached-object-tp1633496p2180467.html
> You can reply by email or by visting the link above.
>
>
--
View this message in context: http://n2.nabble.com/Can%27t-persist-detached-object-tp1633496p2185649.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.
Re: Can't persist detached object
Posted by Ognjen Blagojevic <og...@etf.bg.ac.rs>.
Hi Holod,
What is your enviroment? Which app. server are you using?
Maybe you could try to describe step 4. in more detail? How do you copy
referenced entities?
-Ognjen
holod wrote:
> I'm in strange situation.
> I have an entity Person.
> I have an EJB stateless bean which has three methods:
> 1. public createPerson(Person newPerson){...}
> 2. public updatePerson(Person newPerson, Person oldPerson){...}
> 3. private attachFields(Person detachedPerson){...}
>
> When I'm creating Person, my logic goes this way:
> 1. Outside bean Person is constructed, but It can have detached entities.
> 2. Invoke stateless bean -> createPerson(newPerson);
> 3. Inside this method attachFields(newPerson) in invoked.
>
> Everything goes fine! All detached fields (for example only id of some
> referenced entity is specified) are attached, Person is successfully
> persisted.
>
> When I'm trying to update person, I get strange Exception (it's not strange,
> but I can't get why does in happen).
> 1. Invoke updatePerson(newPerson, oldPerson); oldPerson is attached entity,
> newPerson is a raw entity with detached referenced enitities.
> 2. delete referenced entities from oldPerson.
> 3. use em.flush(); to force deletion data from db.
> 4. set new fields from newPerson to oldPerson.
> 5 attachFields(oldPerson) now, oldPerson CAN'T have detached entities
> 6. use em.merge(oldPerson) to save newly created referenced entities
>
> And... I get exception: can't persist detached entity! and JPA prints entity
> class bla.bla2.SomeEntityClass + [it's key].
> But It can't be so! I get this entity from EntityManager and save it to
> field of oldPerson.
> The same procedure works fine when I create Person.
>
> I don't understand what's going on.
>
> Is there any possibility to know more details about such exception? I've put
> logger.debug everywhere and I clearly see how every field of person is taked
> from EntityManager.
>
>
>
>
>
>
>