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.