The API JPA introduction, extraction tools

1, Persistence objects

The main role of Persistence objects are used to obtain EntityManagerFactory object. CreateEntityManagerFactory by calling the static method of the class, create the configuration file EntityManagerFactory persistence unit name.

//1. 创建 EntitymanagerFactory
@Test
String unitName = "myJpa";
EntityManagerFactory factory= Persistence.createEntityManagerFactory(unitName);

2、EntityManagerFactory

EntityManagerFactory interface is mainly used to create EntityManager instances

// create the entity management class 
EntityManager em = factory.createEntityManager ();

Since EntityManagerFactory is a thread-safe objects (that is, multiple threads access the same EntityManagerFactory object does not have a security thread), and the creation of extremely EntityManagerFactory waste of resources, so when using JPA programming, we can optimize the creation of EntityManagerFactory, just need to do a project there is only one you can EntityManagerFactory

3、EntityManager

In JPA specification, EntityManager is complete the core operation of the object persistence. Entity class as an ordinary java object, only after calling EntityManager persist until it becomes a persistent object. EntityManager managed objects O / R mapping between a set of underlying data source entity class. It can be used to manage and update the Entity Bean, noted in a primary key lookup Entity Bean, you can also query entity JPQL statement.

We can get to complete the transaction, and database persistence operations by calling a method of EntityManager

Method Description:

    getTransaction: get the transaction object 
    persist: the save operation 
    merge: update 
    remove: delete 
    the Find / getReference: According id Query

4、EntityTransaction

In the JPA specification, EntityTransaction is the core of the object to complete the transaction operations for EntityTransaction undertake in our java code relatively simple function

begin: open the transaction 
commit: commit the transaction 
rollback: rolls back the transaction

5, the extraction tools JPAUtil

. 1  Package cn.itcast.utils;
 2  
. 3  Import the javax.persistence.EntityManager;
 . 4  Import javax.persistence.EntityManagerFactory;
 . 5  Import the javax.persistence.Persistence class is;
 . 6  
. 7  / ** 
. 8  * resolved entity manager factory waste of resources and consumption when problems
 9  * static code in the form of blocks, when the first access this program tools, create a public entity factory object manager
 10  *
 11  * getEntityManager first access method: after a block of code creates a static factory object , then call a method to create EntityManager object
 12  * getEntityManager second method: direct already created by a factory object, creating EntityManager object
 13 is   * / 
14  public  class JpaUtils {
 15 
16      Private  static the EntityManagerFactory Factory;
 . 17  
18 is      static   {
 . 19          // 1. load the configuration file, creating entityManagerFactory 
20 is          Factory = Persistence.createEntityManagerFactory ( "myJpa" );
 21 is      }
 22 is  
23 is      / ** 
24       * Gets EntityManager object
 25       * / 
26 is      public  static the EntityManager getEntityManager () {
 27          return factory.createEntityManager ();
 28      }
 29 }

 

Guess you like

Origin www.cnblogs.com/116970u/p/11589797.html