Loading...
середу, 9 квітня 2014 р.

EntityManager.merge() issue: does not store to DB

Strange issue happened when tried to store some entity to database using entityManager.merge() or entityManager.persist() - code seemed to be working, but no data appeared in the database. Sample code for service class:

@Service("myService")
@Transactional
public class MyService {

    private EntityManager entityManager;

    @PersistenceContext
    public void setEntityManager(EntityManager entityManager) {
        this.entityManager = entityManager;
    }

    public void store(User u) {
             entityManager.merge(u);
    }
}

The issue had simple solution, so if you are experiencing troubles with EntityManager.merge() not updating database check your Spring configuration file for the following line, that enables @Transactional annotation support:
<tx:annotation-driven mode="proxy" transaction-manager="transactionManager"/>

0 коментарі:

 
TOP