4

In the following code, CustomerService.test() method persists the customer object implicitly, i.e. without any call to merge() or update(). Why is this the case and how can I force it to persist the entities only when I explicitly call merge/update?

Controller :

@Controller @RequestMapping("/samples") public class Samples { @Autowired private CustomerService customerService; @RequestMapping(value = "/test", method = RequestMethod.GET) @ResponseBody public String test(){ customerService.test(); return "ok"; } } 

Service :

@Service @Transactional public class CustomerService { @Autowired private BaseDao baseDao; public Customer findById(Long id) { return baseDao.find(Customer.class,id); } public void test() { Customer customer = findById(1L); customer.setEmail("[email protected]"); } } 

Dao :

@Repository("baseDao") public class BaseDao { @PersistenceContext private EntityManager em; public void create(BaseEntity entity) { em.persist(entity); } public <T> List<T> list(Class<T> entityClass) { Session session = (Session) em.getDelegate(); List<T> list = session.createCriteria(entityClass).list(); return list; } public <T> T find(Class<T> entityClass, long primaryKey) { return em.find(entityClass, primaryKey); } public <T> T update(T entity) { return em.merge(entity); } public void remove(BaseEntity entity) { em.remove(entity); } public void remove(Class<? extends BaseEntity> entityClass, long primaryKey) { BaseEntity entity = em.find(entityClass, primaryKey); if (entity != null) { em.remove(entity); } } } 
3
  • @mhlz thanks for understanding :-) Commented Jul 28, 2015 at 12:27
  • It doesn't persist alright, but if the entity exist, it get managed by the Hibernate, it updates the entity if there were changes. Commented Jul 28, 2015 at 12:36
  • @Jens I am not sure you understood the question. The entity is being persisted even without me calling update/merge. Commented Jul 28, 2015 at 12:55

1 Answer 1

4

You need to be aware that the method test is within a Hibernate transaction. Therefore you need to know your entity life cycle.

Hibernate Life cycle

After you load your entity, its state is Persistent. When you commit the transaction, Hibernate checks all persistent entities within the transaction: if they are dirty (have changes), they are updated.

Looking up that diagram, either you can evict() your entity or change its email after the transaction.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks Nathan, I'm still waiting for my morning coffee!
Andre, do you mean to say hibernate will implicitly update the entity whenever there are changes? Is there no way to prevent it from doing so? I want to update the entity manually.
Yes, hibernate will implicitly update the entity whenever there are changes, when the entity is at persistent state. If you want to manually update, either find and evict/clear or find and update without a transaction. Closing is not a option because spring already manages the transaction.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.