4

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.

1
  • 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 object Commented Jan 2, 2013 at 13:06

5 Answers 5

6

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:

  1. Load an object from a DB
  2. Modify the object by adding or removing values
  3. The modifications will be saved even if you don't use the save() method.
Sign up to request clarification or add additional context in comments.

2 Comments

yes you are right i'm loading an object from a db & modify the object by adding or removing values.
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.
0

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

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.
thanks for your reply. But I can't disable the cache.<br> please suggest me another solution
do you mean that the cache is enabled?
0

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

-1

seems you have AutoFlash and/or AutoCommit parameters On in your hibernate configuration. Try disable them.

1 Comment

if you are creating your hibernate session with spring, you should add properties like: <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
-1

You may have used the @Transactional annotation.

Try just removing the annotation.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.