Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPA EntityManager: merge() is trying to create a new row in the db - why?

I'm using JPA through the Play Framework.

I'm checking to see if a User object is cached, and if so I retrieve it and merge() it such that I can update fields and save the changes later:

user = (User) Cache.get("user-auth-" + sessionAuthToken);
if (user != null) {
    user = user.merge();  // I believe this is the same as EntityManager.merge()
}

However when I do this I get the following error:

PersistenceException occured : 
  org.hibernate.exception.ConstraintViolationException: 
    could not insert: [models.User]
...
Caused by: com.mysql.jdbc.exceptions.jdbc4.
   MySQLIntegrityConstraintViolationException: 
     Duplicate entry '1235411688335416533' for key 'authToken'

It seems like its trying to insert a new user, even though this user should be, and is already in the database. Why would merge() do that?

Or perhaps I'm going about this entirely the wrong way - advice would be appreciated.

like image 445
sanity Avatar asked Feb 03 '11 17:02

sanity


4 Answers

I think it may be a poblem with hashCode() and equals(). If there are not implemented correctly, a new entity my be inserted instead of updateing an existing one.

like image 109
Christian Kuetbach Avatar answered Sep 28 '22 10:09

Christian Kuetbach


I believe your problem is the way Play manages the JPA environment (and transactions).

Once you receive a request the framework immediately creates the JPA manager and a transaction. From that moment onwards all your Model entities are automatically linked to the manager.

Play facilitates working with this model in 2 ways:

  • You have to explicitly indicate that you want to save changes to an object (via save())
  • Transaction is committed automatically unless there is an exception or you flag it for rollback (JPA.setRollbackOnly())

By running "merge" you are trying to add to the Manager an entity which is already there, which causes the unique key exception. If you just load the entity from the cache, you will be able to modify and call save() once done, and it will work.

like image 32
Pere Villega Avatar answered Sep 28 '22 09:09

Pere Villega


See What is the proper way to re-attach detached objects in Hibernate?. Merge tries to write the stale state to the db in order to overwrite possible other concurrent updates. The linked question mentions session.lock(entity, LockMode.NONE); as a possible solution, I haven't tried it though.

like image 24
Alexander Torstling Avatar answered Sep 28 '22 09:09

Alexander Torstling


If authToken is not a primary key, then perhaps primary key of the User instance being merged doesn't match the primary key of its counterpart in the database, therefore merge() thinks that it's a new User and tries to insert it.

So, check the primary key of the User, perhaps it have been corrupted or lost somehow.

like image 25
axtavt Avatar answered Sep 28 '22 10:09

axtavt