Reputation: 10530
In most of the hibernate applications i see the explicit update statement to update any hibernate entity in db
Session session1 = factory.openSession();
Transaction tx1 = session1.beginTransaction();
College college= (College)session1.get(College.class, 1);
college.setCollegeName("College_update");
session1.saveOrUpdate(college); // at line 1
tx1.commit();
session1.close();
But even i miss statement 1 , entity is updated in DB. My question is it a good practice to mention explicit update statement when it is not required even ?
Upvotes: 2
Views: 934
Reputation: 1182
Update is required to take data from one session or transaction and save it into the database in another transaction.
A a = session1.load(......);
// Commit transaction
//Start New session ......
//Start transaction
session2.update (a);
If you read an entity change its properties within the same session then the results will get saved when you flush the session.
Upvotes: 0
Reputation: 61
No, the saveOrUpdate statement is not required. The College entity is attached to your Hibernate session because is was instantiated by Hibernate. Any mutations to the College entity will be UPDATEd to the database when that session is flushed.
Use saveOrUpdate when you need to attach an existing (detached) entity to a hibernate session. Any subsequent mutations to that entity made in the scope of that session will be persisted.
Upvotes: 3