First, JPA
JPA (Java persistence Api) JPA describes the mapping between objects-database relational tables through JDK5.0 or XML-and persists entity objects for the run-time into the database. The JPA includes the following 3 areas of technology:
1. ORM mapping metadata, JPA supports XML and JDK 5.0 annotations in the form of two metadata, metadata describes the mapping between the object and the table, the framework to persist the entity object into the database table;
2, JPA API, used to manipulate entity objects, perform CRUD operations, the framework in the background to do all the things for us, developers from the cumbersome JDBC and SQL code freed.
3, Query Language, which is a very important aspect of persistent operation, through object-oriented rather than database-oriented query language query data, to avoid the program's SQL statements tightly coupled.
Ii. key employees working in the JPA
1, Entitymanagerfactory
Entitymanager is called the entity Manager, which 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 (the so-called data source, can be understood as a database, we can configure multiple data sources in the application server, while using different persistenceunit to map these data sources, which makes it easy to implement transactional operations across multiple databases! --jta do not explain)
2, Persistencecontext
Persistencecontext is called a persistence context, which typically contains data that is within the scope of the current transaction and is used to manage (Entity). 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 and other operations to manipulate the synchronization between Persistencecontext and database data!
3, Entitymanger
Entitymanager is an interface for applications to manipulate persisted data. is a helper class that is used to manipulate entity beans. It can be used to generate/delete persisted entity beans, find entity beans by primary key, or find entity 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. When the entity bean is detached from Entitymanager, he is not managed and Entitymanager cannot track any of his state changes. Entitymanager's fetch has been described earlier and can be injected dynamically by the EJB container through @persistencecontext annotations.
Third, analogy
These concepts work like Hibernate, and they are an implementation of persistence.
Entity Management
Hibernate session in order to be able to use the same session object in a request cycle, in Hibernate solution, put forward the concept of currentsession, hibernate in the current session, can be bound to the JTA transaction or to the current thread. In Hibernate, the session manages the data for all persisted objects. In EJB3, Entitymanager manages Persistencecontext,persistencecontext as a collection of managed persistent objects.
Life cycle
The JPA entity life cycle is divided into 4 states: New, managed, detached (free), deleted. In fact, similar to Hibernate's mapping entity, roughly corresponds to the life cycle in Hibernate: Free State (Transient)--Unmanaged, persistent (persistent)--Escrow, Free State (Detached)--Separate, But there is a difference in grain size.
Entitymanager in JPA