dolf
dolf

Reputation: 43

Java - Why does hibernate detects a dirty object though it is already saved?


 I'm using spring and hibernate in our web application and we have an issue about multiple row inserted in just one execution. There's no loop in our code to insert 3 rows in one user request. I's just wondering if this (hibernate detects a dirty object) might be the cause.
Please help...
Thanks,

Upvotes: 2

Views: 575

Answers (1)

Mik378
Mik378

Reputation: 22191

When the session is opened and the object is just saved in or retrieved from the database. This state is called persistent. During this state Hibernate manages the object and saves your changes, if you commit them. Below you can see an example. A car is saved and the name is changed afterwards. As the car is in persistent state, the new name will be saved.

Session session = HibernateSessionFactory.currentSession();
tx = session.beginTransaction();
session.save(car);
car.setName(“Peugeot”);
tx.commit();

The following code loads a car by id and changes the name. There is no session.update involved. Every object which is loaded by session.get, session.load or a query is in persistent state. It is stored in the persistence context of the session. When tx.commit() is called, Hibernate will flush the persistence context and all not yet written insert, update and delete statements are executed.

Check that you're not modifiying the object (relationships) after the saving operation and before the transaction commit.

Upvotes: 2

Related Questions