Entitymanager is an auxiliary class that is used to manipulate entity beans. He can be used to generate/delete persisted entity beans, find entity beans by primary key, or find beans that satisfy a condition through the EJB3 QL language. When an entity bean is managed by Entitymanager, Entitymanager tracks his state changes and synchronizes the changed values to the database when any decision is made to update the entity bean.
The entity manager is a very important part of JPA, and its specific functions and roles are as follows:
As you can see, the entity manager interacts primarily with the database in two ways:
One is the SQL script that is responsible for translating entity object operations in Java into database recognition.
The second is to execute the entity-oriented query JQL into SQL scripts and assemble the returned query results into entities.
(1) public void persist (Object entity)
Role: Saving records to database records
(2) Public <t>t merge (T entity)
Role: Update database records
(3) public void Remove (Object entity)
Action: Deletes an entity and removes the entity from the database.
(4) Public <T> T Find (class<t> entityclass,object PrimaryKey)
Function: Finds the entity object through the entity primary key.
(5) public void Flush ()
Function: Saves the persistent HA context entity to the database and synchronizes with the database.
(6) public void Refresh (Object entity)
Action: Refreshes the entity bean to get the latest object
(7) Public boolean contains (Object entity)
Role: detects whether the current entity is managed
(8) Find (*.class, ID): Returns null if the entity bean does not exist
Two types of Entitymanager objects
1. Container Hosting
There is no need to consider the release of Entitymanager connections, and the issues of transactions are given to the container to manage. Container-managed Entitymanager objects must run in the EJB container and not in the Web container and the J2SE environment. Entitymnager objects are obtained by injecting @persistencecontext annotations.
Container-managed Entitymanager code:
@PersistenceContext (unitname = "basic-entity")
protected Entitymanager em;
2. App hosting
programmers need to manually control the release and connection, and manually control transactions. This approach can be applied not only in the EJB container, but also from the EJB container, and integrates with any Java environment, such as the Web container, the J2SE environment, and so on. So in some ways, this is the basis for JPA to run independently of the EJB environment.
App managed Entitymanager code:
//The entity unit name owned by the currently logged-on user
protected String unitname = "Basic-entitytest";
//Create Entity Manager factory a
entitymanagerfactory emf = Persistence.createentitymanagerfactory (Unitname);
//Create entity manager a
entitymanager em = Emf.createentitymanager ();
Our company has recently done projects, the business needs to implement dynamic switching of data sources, that is, to implement dynamic switching of entity unit names, so the original container-managed Entitymanager to apply managed Entitymanager, but this causes JTA transactions can not be used, The JTA distributed transaction needs to be implemented manually, but so that the JTA distributed transaction is isolated from the EJB container and all the services of the EJB are not available.
JPA First Experience Series (iii) Introduction to entity manager