Entitymanager As the name implies, Entity Manager, responsible for managing Entity objects, The operations of entities, including additions, deletions, modifications, and queries, are implemented through the entity manager. It is created by Entitymanagerfactory. Entitymanagerfactory, as a factory of Entitymanager, contains metadata information for the current o-r mappings, each entitymanagerfactory, which can be called a Persistence unit (persistenceunit), Each persistence unit can be considered a mapping of a data source
Persistencecontext, called persistent context, managed entity object (Entity) data. Each entitymanager will be associated with a persistencecontext. Persistencecontext stores the data of the entity object, while the relational database stores the records, Entitymanager is the intermediary who maintains this or mapping, it can load the data from the database into the Persistencecontext, Data can also be persisted from the Persistencecontext to the database, Entitymanager through Persist, merge, remove, refresh, flush Operation to manipulate the synchronization between Persistencecontext and database data!
1introducing the Entity manager
Dependency Injection Entitymanaer
in the EJB container, use Dependency injection to create the entitymanager.
Such as:
@PersistenceContext (unitname= "exam-entity") Protectedentitymanager em;
@PersistenceCOntexattribute that represents a calloutemis an entity managerEntitymanagerobjects,EJBcontainers are based onUnitnamethe value to initializeEntitymanger. Unitnamethe value isPersistence.xmlconfigured in theUnitin thenamevalue
Such as:
<persistence-unitname= "exam-entity" transaction-type= "JTA" >
2manipulating EntitiesEntity
The basic operation has
Persist
Find
Merge
Remove
CreateQuery
2.1AddEntity
The Persist method in the entity manager can add a record to the data
public void persist (object entity)
The instance code is as follows:
User user= new User (); User.setid (Uuid.randomuuid (). toString ()); User.setname ("Tian Teacher"); em.persist (user);
2.2Search by primary keyEntity
Find method to find the corresponding entity based on the primary key
Public <T> Find (class<t> entityclass,object primaryKey);
PrimaryKey the value of the primary key. For example: Find entity code with ID uuid
User User=em.find (User.class, "uuid");
2.3UpdateEntity
Merge method to update an entity to a database.
Public <T> t Merge (t entity);
among them, you can pass Find method is first found, then the value is reset, and the last update
User User=em.find (User.class, "uuid"); User.setname ("Miss Li"); Em.merge (user);
2.4DeleteEntity
Remove method to delete a record in the database
pubilc void Remove (Object entity);
is also through Find method to find out first and then delete
User User=em.find (User.class, "uuid"); em.remove (user);
Summary:
these are the JPA preliminary understanding, is the most basic additions and deletions of the search is no problem, of course, here is only preliminary understanding, and there is no design abstraction and other operations, the next step is the introduction of space.
JPA Entitmanager First Experience