Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPA Hibernate merge performs insert instead of update

Tags:

hibernate

jpa

I have a simple test, where I am trying to update the object, but merge seems to be performing an insert instead of update.

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:spring/app-context.xml","classpath:spring/testdb-context.xml"})
public class UserJPATest {
@Test
public void testUpdate() throws Exception {
    System.out.println("update");

    User entity = ObjectManager.USER_DAO.findById(3L);
    entity.setUsername("harryUpdate");

    ObjectManager.USER_DAO.update(entity);

    User selEntity = ObjectManager.USER_DAO.findById(3L);
    Assert.assertEquals(entity.getUsername(),selEntity.getUsername());
}

}

This is my update method

@Override
@Transactional(propagation= Propagation.REQUIRES_NEW)
public T update(T entity) throws Exception {
    try {
        T merged = entityManager.merge(entity);
        return merged;
    } catch (Exception e) {
        e.printStackTrace();
        throw new Exception(e);
    }
}

Update to code

@Override
@Transactional(propagation= Propagation.REQUIRES_NEW)
public T update(T entity) throws Exception {
    try {
        T merged = null;
        BaseEntity baseEntity = null;
        if(entity instanceof BaseEntity){
            baseEntity = (BaseEntity)entity;
            merged = entityManager.find(entityClass, baseEntity.getId());
        }
        merged = entityManager.merge(entity);
        entityManager.flush();
        return merged;
    } catch (Exception e) {
        e.printStackTrace();
        throw new Exception(e);
    }
}


Now I get the following error Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Transaction marked as rollbackOnly

like image 250
user373201 Avatar asked Feb 24 '11 13:02

user373201


People also ask

Which method can be used for performing both insert as well as update operation in JPA?

You can use Hibernate's update or JPA's merge method to associate a detached entity with a persistence context. After you've done that, Hibernate will update the database based on the entity attribute values.

What is difference between Merge and update in Hibernate?

Hibernate handles persisting any changes to objects in the session when the session is flushed. update can fail if an instance of the object is already in the session. Merge should be used in that case. It merges the changes of the detached object with an object in the session, if it exists.

How does Hibernate merge work?

Hibernate merge can be used to update existing values, however this method create a copy from the passed entity object and return it. The returned object is part of persistent context and tracked for any changes, passed object is not tracked. This is the major difference with merge() from all other methods.


1 Answers

I had a version column which was not set when seed data was inserted into database. Hence all the problems with update and delete

like image 92
user373201 Avatar answered Oct 14 '22 22:10

user373201