Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Comparison between EntityManager Interface in JPA and Session Interface in Hibernate

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly talks about "the comparison of EntityManager interface in JPA and Session interface in Hibernate". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn the comparison between the EntityManager interface in JPA and the Session interface in Hibernate.

Many functions of the EntityManager interface are similar to those of the Session interface, and the methods of the two interfaces are compared in Table 1-1 below.

Table 1-1 compares EntityManager interface with Session interface

As you can see from the above table, most of the methods in the EntityManager interface have corresponding methods in the Sesson interface. The remove () method of EntityManager is basically the same as the delete () method of Session, with one small difference: the delete () method of Session can delete persistent and free objects, while the remove () method of EntityManager can only delete persistent objects.

If the program accesses the database mainly through JPA API, but needs to access Hibernate API in a few cases, the underlying Session object can be obtained from the EntityManager interface:

/ / get SessionSession session = entityManager.unwrap (Session.class) in Hibernate API

Here are two common ways to update data in a database:

(1) first load the persistent object, modify the properties of the persistent object, and then the underlying Session automatically updates the corresponding data in the database synchronously when cleaning the cache.

The following code updates the persistent object through JPA API:

/ / use JPA APItx = entityManager.getTransaction (); tx.begin (); / / start a transaction Customer customer= (Customer) entityManager.find (Customer.class, Long.valueOf (1)); customer.setName ("Jack"); / / modify the name property tx.commit () of the Customer persistent object; / / clean up the persistence cache and update the corresponding data in the database

The following code updates the persistent object through Hibernate API:

/ / use Hibernate APItx = session.beginTransaction (); Customer customer= (Customer) session.get (Customer.class, Long.valueOf (1)); customer.setName ("Jack"); / / modify the name property tx.commit () of the Customer persistent object; / / clean up the persistence cache and update the corresponding data in the database

(2) modify the properties of the free object, and then transform the free object into a persistent object.

The following code updates the corresponding data in the database through the merge () method of EntityManager in JPA API:

/ / use JPA APICustomer customer= … / / assume that customer is a free object customer.setName ("Jack"); / / modify the name property of the Customer free object tx = entityManager.getTransaction (); tx.begin (); / / start a transaction / / plan to execute a SQL update statement Customer mergedCustomer=entityManager.merge (customer); tx.commit (); / / clean up the persistence cache and update the corresponding data in the database

The following code updates the corresponding data in the database through the update () method of Session in Hibernate API:

/ / use Hibernate APICustomer customer= … / / assume that customer is a free object customer.setName ("Jack"); / / modify the name property of Customer free object tx = session.beginTransaction (); session.update (customer); / / plan to execute a SQL update statement tx.commit (); / / clean up the persistent cache and update the corresponding data in the database to this point. I believe you have a deeper understanding of "the comparison between EntityManager interface in JPA and Session interface in Hibernate". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report