0

Hello I am trying to understand the behavior of save and persist method of hibernate. For this I write a very simple method and try to run it with transaction and with out transaction but it actually confused me.

    @Test
    public void saveOrPersist() {
        Person person = new Person();
        person.setFirstName("naveen");
        person.setLastName("kumar");
// 1        session.beginTransaction();
         session.persist(person);
// 2    session.save(person);
// 3         session.flush();
// 4    session.getTransaction().commit();
    }

with this method I did following things. Uncomment line 1, 2 and 4 and comment the line just above line 2 to test the behavior of save method. It works fine and give me the following out on eclipse console.

Hibernate: select nextval ('hibernate_sequence')
Hibernate: insert into BaseEntity (createDateTime, description, updateDateTime, firstName, lastName, discriminator, id) values (?, ?, ?, ?, ?, 'p', ?)

also check db and one more entry inserted into person table. second i comment the line 1 and line 4 again run the same test case with transaction. Then it gives me the below out put

Hibernate: select nextval ('hibernate_sequence')

This time there was no entry into person table.

Then I did the same thing with persist method. Run persist method twice with transaction and without transaction and I got the same result on eclipse console.

Now My question is apart from return type there is one more difference between persist and save. That is persist method does not work out side of transaction. I was trying to learn this thing. Can some tell what's wrong with this concept or I understood wrongly. If it then correct my code with little bit more description. Thanks in advance.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
keepmoving
  • 1,813
  • 8
  • 34
  • 74
  • What is your question exactly? – JB Nizet Jul 23 '15 at 15:59
  • my question is what does this line means "persist will not be executed if its called from outside transactions boundaries." , and i was trying to execute this statement into code. – keepmoving Jul 23 '15 at 16:03
  • It means just what you saw: no row will be inserted in the database. – JB Nizet Jul 23 '15 at 16:07
  • but in case of save method I found this line "save will be executed even outside transaction boundary.", which means what i understood save method should insert record even there is no transaction. – keepmoving Jul 23 '15 at 16:13
  • Where did you saw this line? You should not use the session outside of a transaction. Just don't. – JB Nizet Jul 23 '15 at 16:23
  • Please see this link, In this thread still people are still confused how to write test to see how persist() method does not work out side transaction boundary and how save can works even out side the transaction boundary. http://stackoverflow.com/questions/8097679/whats-the-difference-between-persist-and-save-in-hibernate. I am also looking for some code where i can test this statement. – keepmoving Jul 23 '15 at 16:35

0 Answers0