• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JPA detaching entity still persists upon exit

 
Rj Ewing
Ranch Hand
Posts: 93
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am having trouble with autopersist using hibernate and spring-data. I have the following service where I create entityA.



Where I am having trouble is the "entity.setRegistered(true);" line for the entityA being created. When I walk through the process, after "registerExternal();" in the create method, I can see in the db that the newest entityA.registered = true. However after create method finishes, entityA.registered gets set back to false.

I have tried to call "entityManager.detach(entityA) and entityManager.clear() before the "registerExternal()" call, as well as "entityManager.refresh(entityA) after the call and nothing seems to work.

Is there a way to set an object to not autopersist?
 
Tim Holloway
Saloon Keeper
Posts: 18367
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't see anything that definitely points to the error, but one thing that can bite you in JPA when you do a merge() to save an updated JPA object is that the object returned from merge() may not be the exact same object as what was passed to merge.

That is:


The result of this will typically be that "before.equals(after)" is true, but "before == after" is false

Once merged, use the "after" version of the entity or pain and suffering will be yours.
 
Rj Ewing
Ranch Hand
Posts: 93
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks. I'm not sure what I am doing differently, (maybe code didn't update?) but when I went to try to use merge, I noticed it was working without calling merge.
 
Tim Holloway
Saloon Keeper
Posts: 18367
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unfortunately, I can't tell, because I don't know how the save() method in class EntityARepository works.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic