Comparison of the JPA and Hibernate EntityManager Interface Session interface

In this paper, Session EntityManager interfaces and Hibernate JPA interface are compared by reference herein "proficient JPA and Hibernate: Java object persistence technology explain" 3rd edition, published in 2020, author: Sunweiqin.

EntityManager interface and the many features of the Session interface very similar to Table 1-1 of these two interface methods was compared.
1-1 Comparison EntityManager interface Session interface tables
Comparison of the JPA and Hibernate EntityManager Interface Session interface
can be seen from the above table, most of the methods EntityManager interface has a corresponding interface in Sesson method. The EntityManager remove () method of action and the Session delete () method is substantially the same, a difference is small: Session to delete () method can delete the persistent object and free objects, and the EntityManager remove () method You can only delete persistent objects.
If the program mainly through the JPA API to access the database, but in individual cases you need to access the Hibernate API, you can obtain a Session object from the underlying EntityManager interface:

//获得Hibernate API中的Session
Session session = entityManager.unwrap( Session.class );

The following summarizes the update data in the database and then two common ways:
(1) persistent objects to load, modify the properties of the persistent object, and then it automatically updates the corresponding Session underlying data in the database cache while cleaning.
The following code to update the persistent object by JPA API:

//使用JPA API
tx = entityManager.getTransaction(); 
tx.begin(); //开始一个事务
Customer customer=(Customer)entityManager.find(Customer.class,
                                            Long.valueOf(1));
customer.setName("Jack"); //修改Customer持久化对象的name属性
tx.commit(); //清理持久化缓存,更新数据库中的相应数据

The following code to update persistent objects via Hibernate API:

//使用Hibernate API
tx = session.beginTransaction(); 
Customer customer=(Customer)session.get(Customer.class,
                                         Long.valueOf(1));
customer.setName("Jack"); //修改Customer持久化对象的name属性
tx.commit(); //清理持久化缓存,更新数据库中的相应数据

(2) modify properties of an object free, and then converted into the free target persistent object.
The following code to update the update the corresponding data in the database by the merge JPA API EntityManager () method:

//使用JPA API
Customer customer=…  //假定customer为游离对象
customer.setName("Jack"); //修改Customer游离对象的name属性

tx = entityManager.getTransaction(); 
tx.begin(); //开始一个事务

//计划执行一条SQL update语句
Customer mergedCustomer=entityManager.merge(customer);  
tx.commit();  //清理持久化缓存,更新数据库中的相应数据

The following code to update the corresponding data in the database by the Session in Hibernate API update () method:

//使用Hibernate API
Customer customer=…  //假定customer为游离对象
customer.setName("Jack"); //修改Customer游离对象的name属性

tx = session.beginTransaction(); 
session.update(customer);  //计划执行一条SQL update语句
tx.commit();  //清理持久化缓存,更新数据库中的相应数据

Guess you like

Origin blog.51cto.com/sunweiqin/2418795