Reputation: 321
i'm new to hibernate, while i add an element and cancel it, I see that the data gets saved in db. Nowhere in my code i called save method to save it.
Upvotes: 4
Views: 5460
Reputation: 871
In my case, I have
@Transactional(propagation = Propagation.REQUIRES_NEW)
void A(){
B()
}
@Transactional(propagation = Propagation.REQUIRES_NEW)
void B(){
//get data from table
//update data
//throw a exception
//Call .save()
}
Data is auto update to DB without run save(), I remove propagation = Propagation.REQUIRES_NEW
it not auto save anymore.
Upvotes: 0
Reputation: 1
You may have used the @Transactional
annotation.
Try just removing the annotation.
Upvotes: -1
Reputation: 2856
Once you load the data from the db, it becomes persistent, and any changes made to it will be updated, if it is updated before the session is closed. If you do not want the data in the db to be updated with the changes you are making after loading it, make the changes only after closing the session. Then after that, if you want to persist the data again, open one more session and call save() or persist().
EDIT: 1) Make sure cache is disabled in order to ensure there is no caching between different sessions.
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
2) Follow the steps:
i) begin session --> begin transaction --> get data from both tables --> close transaction --> close session.
ii) create object of 3rd table--> do whatever u want with it, like adding data from the first two tables.
iii) begin new session --> begin new transaction --> save the object of 3rd table using session.save() --> close transaction --> close session.
After step (i) is done, the objects from table1 and table2 are no more 'persistent', and are 'detached'. If you don't do session.save() in step (iii), the object of table3 won't get saved, because it is no longer dealing with persistent objects.
This is from my understanding of persistent and detached objects. If it doesn't work, do reply. I will code it down and find a solution.
And one more advice, do consider using session.persist() instead of session.save(). If you want to understand their difference, this is the link: What's the advantage of persist() vs save() in Hibernate?
Good Luck!
Upvotes: 0
Reputation: 12531
If you're modifying an object already associated with an Hibernate session all your modifications will be saved. Check the manual.
For example if you do something like:
save()
method.Upvotes: 6
Reputation: 788
seems you have AutoFlash
and/or AutoCommit
parameters On
in your hibernate configuration. Try disable them.
Upvotes: -1