HowToDoInJavaTwitterFacebookLinkedinRedditPocket
- Hibernate Cascade Generated Key Insert Template
- Jpa Cascade Delete
- Hibernate Cascade Generated Key Insert 2017
2013-2-21 default-cascade 指定了未明确注明cascade属性的.Net属性和集合类.Net会采取什么样的默认级联风格。 auto-import属性默认让我们在查询语言中可以使用非全限定名的类名。assembly和 namespace指定了持久化类的应用程序集名称和其所在的名称空间名。.
- JPA's and Hibernate's persist, save, merge and update methods seem to do the same but there are important differences. Better use the right one.
- Hibernate insert with foreign key. Ask Question Asked 4 years, 9 months ago. Browse other questions tagged java mysql hibernate or ask your own question.
hibernate works only with persistent entities and persistent entities are classes which are attached to any hibernate session. Please note that creating an instance of a class, you mapped with a hibernate annotations, does not automatically persist the object to the database. It must be save explicitly after attaching it to a valid hibernate session.
In this tutorial, learn to use hibernate save() and saveOrUpdate() methods under different usecases.
1. Hibernate save() method
In hibernate, we generally use one of below two versions of save() method:
Both save() methods take a transient object reference (which must not be null) as an argument. Second method takes an extra parameter ‘entityName‘ which is useful in case you have mapped multiple entities to a Java class. Here you can specify which entity you are saving using save() method.
A simple example to demo hibernate save() method.
Now let’s save this hibernate entity.
Program Output.
1.1. Calling save() method on persistent entity
We got our
Employee
entity saved. So easy. But in reality, it is not so simple usecase. There you may need to update again employee entity and then save again in another session. Should you call save()
method again? Let’s check out.Program Output.
Here hibernate tried to insert the entity again. Though it was failed due to primary key check, but check may not be there for other entities and you may end up with duplicate rows.
Note: While second
save()
method causes duplicate row in different sessions, BUT in same session they will work correct.Look at below example.
Program Output.
It’s confusing. Right? Let’s make it simple. And rule is below:
Remember that you should not call save()
method on a persistent entity (entity associated with any hibernate session). Any changes done to persistent entity is automatically saved.1.2. Changing state of persistent entity
Any change to persistent entity is saved automatically. Let’s understand this concept in simple example.
Program Output.
In above example, we made the ‘
emp
‘ object persistent using first save()
method. Afterward when we updated the last name to ‘temp‘, an update query was executed as expected. This we verified in returned data as well. This is the correct way to work with hibernate persistent entities.2. Hibernate saveOrUpdate() method
In discussion of
save()
method, we forgot about case where we had to save persistent entity in another session and that got resulted in duplicate key error. That is also a valid scenario.To handle such cases, you must use
saveOrUpdate()
method. Strictly speaking, you should use saveOrUpdate() with even non-persistent entities. Personally, I do not see any harm in doing so. Though, It may make you a little bit careless. So be cautious.Hibernate Cascade Generated Key Insert Template
2.1. Hibernate saveOrUpdate() example
Let’s see how
saveOrUpdate()
method can be used along with entity persisted with save()
method.Program Output.
Now we are able to save the entity as well as update the entity as well using
saveOrUpdate()
method.Please remember that if you have used
saveOrUpdate()
method in place of save()
method above, then also result would have been same. saveOrUpdate()
can be used with persistent as well as non-persistent entities both. Persistent entities will get updated, and transient entities will be inserted into database.3. Suggestion For production code – best practices
Jpa Cascade Delete
It wouldn’t be advisable to try to use above code in production code. Ideally, what you would do is pass VO object to DAO layer, load the entity from the session and update the entity with by copying VO data onto it. This means that the updates take place on a persistent object, and we don’t actually have to call
Session.save()
or Session.saveOrUpdate()
at all.Once an object is in a persistent state, Hibernate manages updates to the database itself as you change the fields and properties of the object. It’s big relief.
4. Summary
- Save() method stores an object into the database. It will Persist the given transient instance, first assigning a generated identifier. It returns the id of the entity created.
- SaveOrUpdate() calls either save() or update() on the basis of identifier exists or not. e.g if identifier does not exist, save() will be called or else update() will be called.
- Probably you will get very few chances to actually call save() or saveOrUpdate() methods, as hibernate manages all changes done in persistent objects.
Let me know is something is not clear or needs more explanation related to hibernate save() and saveOrUpdate() methods.
Happy Learning !!