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.
4 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.

- 12,231
- 6
- 49
- 62
-
yes you are right i'm loading an object from a db & modify the object by adding or removing values. – Krishna Jan 02 '13 at 12:12
-
yes i understand that persistent objects will be automatically saved. but how to save it on calling save(). also there is many-to-many relationship b/w the tables. – Krishna Jan 02 '13 at 12:44
seems you have AutoFlash
and/or AutoCommit
parameters On
in your hibernate configuration. Try disable them.

- 780
- 1
- 6
- 18
-
if you are creating your hibernate session with spring, you should add properties like: `
false ` in your hibernate configuration. also you can open a transaction (which is best approach) then call `rollback` to avoid saving data into DB – Hazhir Jan 02 '13 at 11:55
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!

- 1
- 1

- 2,766
- 3
- 23
- 27
-
What i'm trying to do is get the data from two tables & put it in the third table which has a reference of these two tables. without calling save the data gets saved in third table. – Krishna Jan 03 '13 at 10:13
-
thanks for your reply. But I can't disable the cache.
please suggest me another solution – Krishna Jan 09 '13 at 06:55 -
You may have used the @Transactional
annotation.
Try just removing the annotation.

- 748
- 7
- 21