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.
-
I have many-to-many relationship b'w the fields. <set name="instructionCodes" table="INSTRUCTION_CODE_IN_GROUP"> <key column="INSTRUCTION_GROUP_SYSTEMID" /> <many-to-many column="INSTRUCTION_CODE_SYSTEMID" class="com.InstructionCode" /> </set> I'm loading the object from db using load & trying to modify the objectKrishna– Krishna2013-01-02 13:06:45 +00:00Commented Jan 2, 2013 at 13:06
5 Answers
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:
- Load an object from a DB
- Modify the object by adding or removing values
- The modifications will be saved even if you don't use the
save()method.
2 Comments
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!
3 Comments
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.
Comments
seems you have AutoFlash and/or AutoCommit parameters On in your hibernate configuration. Try disable them.
1 Comment
<property name="hibernate.connection.autocommit">false</property> in your hibernate configuration. also you can open a transaction (which is best approach) then call rollback to avoid saving data into DB