I am using JPA through the Play Framework.
I check if the user object is cached, and if so, I get it and merge () so that I can update the 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 that he is trying to insert a new user, even if that user should be, and is already in the database. Why merge () this?
Or maybe I'm going to do it completely wrong - the advice will be appreciated.
source share