Results 1 to 3 of 3
- 02-02-2012, 01:42 AM #1
The javadoc for EntityManager.persist(...) says it throws a EntityExistsException if the entity already exists. Does "already exists" mean that if I accidentally create a new entity object with a duplicate primary key, the EntityManager will throw an exception when I try to persist it? What if the key exists in the database, but that particular EM is not currently managing the associated object?
I know, I could just test it with my JPA provider... but I want to know what the contract is.Get in the habit of using standard Java naming conventions!
- 02-02-2012, 10:49 AM #2Moderator
- Join Date
- Apr 2009
- Rep Power
The Managers are associated with the general context I think, so another EntityManager should also "see" the same set of entities.
At least, presuming they are in the same system (of course).
So it's not the EM that actually manages them, but the context that sits behind them (at least as far as tracking the entities in the system).
As far as the key existing in the database, but not necessarily read out into the context, then you won't see the exception until the flush or commit. In other words until the end of the transaction.
- 02-03-2012, 03:21 AM #3
It's been a while since I used JPA, and I had forgotten about the differences between persist() and merge(). I rediscovered this helpful article that reassures me I cannot accidentally "overwrite" an existing entity with a new one. (I could still clobber data with a merge, but I'm not worried about that right now.)Get in the habit of using standard Java naming conventions!