Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why I need to explicitely save after a find with Hibernate?

Tags:

java

hibernate

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

  1. session.save(user);
  2. user.setName("Rocker!");
  3. 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...

like image 762
rocker Avatar asked Jul 14 '10 11:07

rocker


People also ask

What is difference between save and saveOrUpdate in Hibernate?

Difference between save and saveOrUpdate in Hibernate The main difference between save and saveOrUpdate method is that save() generates a new identifier and INSERT record into the database while saveOrUpdate can either INSERT or UPDATE based upon the existence of a record.

Which is better save or persist in Hibernate?

Difference between save and persist method in HibernateFirst difference between save and persist is there return type. Similar to save method persist also INSERT records into database but return type of persist is void while return type of save is Serializable object.

How does Hibernate save work?

Hibernate save method returns the generated id immediately, this is possible because primary object is saved as soon as save method is invoked. If there are other objects mapped from the primary object, they gets saved at the time of committing transaction or when we flush the session.

How do I stop Spring data JPA from doing a select before a save ()?

An easy way is to make your Entity implements Persistable (instead of Serializable), which will make you implement the method "isNew".


1 Answers

You are correct that hibernate should automatically detect changes to the state of persistent objects:

Persistent - a persistent instance has a representation in the database and an identifier value. It might just have been saved or loaded, however, it is by definition in the scope of a Session. Hibernate will detect any changes made to an object in persistent state and synchronize the state with the database when the unit of work completes. Developers do not execute manual UPDATE statements, or DELETE statements when an object should be made transient.

I would assume that criteria results aren't returned in persistent state (but I can't seem to find proof of this in the docs)

Try using an HQL query, for which the docs are clear:

Entity instances retrieved by a query are in a persistent state

Also make sure that the flush mode of the session is set to AUTO or COMMIT.

like image 72
Bozho Avatar answered Oct 17 '22 18:10

Bozho