Why I need the line "session.save(user);" in the following code snippet? I thought, with the find call the user is already attached to the session and changes will be tracked and commited. Would you mind to explain the details for me? Or do I need a special configuration or other circumstances where I could have heard about this 'feature'?
session = createSession();
ta = session.beginTransaction();
assertEquals(1, session.createCriteria(MyUser.class).list().size());
// find one user
MyUser user = session.createCriteria(MyUser.class).uniqueResult();
user.setName("Rocker!");
// ### HERE ###
// WHY this 'save' is necessary!!??
session.save(user);
ta.commit();
ta = session.beginTransaction();
assertEquals(1, session.createCriteria(MyUser.class).list().size());
MyUser user = session.createCriteria(MyUser.class).uniqueResult();
assertEquals("Rocker!", user.getName());
ta.commit();
UPDATE 1
The same question applies to
- session.save(user);
- user.setName("Rocker!");
- ta.commit();
UPDATE 2
The solution to the problem is: I am using guice / warp persist. And in some cases I was incorrectly bound a code block to a transaction via @Transactional: so the transaction was committed too early opened and hence the separate change was not included in the commit. Thanks guys! So, always make sure you know about your transaction scope in case you are using spring or guice...