Relationship between JPA and Hibernate
1. JPA
JPA Full name: Java persistence API
JPA uses JDK 5.0 annotations or XML to describe the ing relationship between objects and Relational Tables, and persistently stores object objects in the database at runtime.
The emergence of JPA?
There are two reasons for the emergence of JPA:
1. simplify the development of Object Persistence for existing Java EE and Java SE applications;
Second, Sun wants to integrate the ORM technology to unify the persistence field.
JPA was developed by the EJB 3.0 software Expert Group as part of the JSR-220 implementation. But it does not depend on EJB 3.0. You can
Used in desktop applications. JPA aims to provide pojo with persistence standards and specifications, which can run independently from containers and facilitate development and testing. It
The implementation frameworks include hibernate, toplink, and JDO.
1.1.jpa Technology
(1) ORM ing metadata
JPA supports two types of metadata: XML and JDK 5.0 annotations. The metadata describes the ing between objects and tables. Based on this, the Framework holds object
Extended to database tables;
(2) JPA API
It is used to operate entity objects and perform crud operations. The framework completes all things for us in the background. developers can solve the problem from tedious JDBC and SQL code.
Remove it.
(3) Query Language
You can use an object-oriented rather than a database-oriented query language to query data. This avoids the tight coupling of SQL statements.
1.2.advantages of JPA
(1) Standardization
JPA is one of the Java EE standards released by the JCP organization. Therefore, any framework that claims to comply with the JPA standards follows the same architecture and provides the same access.
API, which ensures that enterprise applications developed based on JPA can run under different JPA frameworks after a few modifications.
(2) Support for container-level features
The JPA framework supports large datasets, transactions, concurrency, and other container-level transactions. This makes JPA go beyond the limitations of the simple persistence framework and plays a more important role in enterprise applications.
.
(3) easy to use and easy to integrate
One of the main goals of JPA is to provide a simpler programming model: creating entities under the JPA framework is as simple as creating Java classes without any constraints.
And restrictions, you only need to use javax. Persistence. entity for annotation; JPA frameworks and interfaces are also very simple, there are not many special rules
Developers can easily master the requirements of the design model. JPA is designed based on non-intrusive principles, so it is easy to integrate with other frameworks or features.
Integration.
(4) comparable to the query capability of JDBC
JPA's query language is object-oriented rather than database-oriented. It constructs query statements using object-oriented natural syntax and can be viewed as hibernatehql.
. JPA defines a unique jpql (Java persistence Query Language). jpql is an extension of ejbql, which is a needle
A query language for objects. The operation object is an object rather than a relational database table. It also supports batch update and modification, join, and group operations.
By, having, and other advanced query features that can only be provided by SQL, and even support subqueries.
(5) support for advanced object-oriented features
JPA supports advanced object-oriented features, such as inheritance between classes, polymorphism, and complex relationships between classes. Such support allows developers to maximize
Use an object-oriented model to design enterprise applications without the need to handle the persistence of these features in relational databases.
2. hibernate
JPA requires providers to implement its functions. Hibernate is a strong JPA provider. In terms of functionality, JPA is
A subset of the hibernate function. Hibernate is compatible with JPA since 3.2. Hibernate3.2 obtains
JPA (Java persistence API) Compatibility certification.
For example:
(1) The State of an object. In hibernate, there are three types: free, persistent, and free. In JPA, there are new, managed, and detached,
Removed, and these statuses all correspond one by one.
(2) The flush method corresponds to each other,
(3) query = manager. createquery (SQL), which is written as session in hibernate, and changed
Manager
The relationship between JPA and hibernate can be simply understood as JPA is a standard interface, and Hibernate is an implementation. Then how does hibernate implement
This relationship of JPA. Hibernate is mainly implemented through three components, and hibernate-annotation, Hibernate-
Entitymanager and hibernate-core.
(1) hibernate-annotation is the basis for hibernate to support annotation configuration. It includes the standard JPA annotation and
Annotation of hibernate's special functions.
(2) hibernate-core is the core implementation of Hibernate and provides all the core functions of hibernate.
(3) hibernate-entitymanager implements the standard JPA and can regard it as an adapter between hibernate-core and JPA. It is not directly
Provides the ORM function, but encapsulates the hibernate-core, so that hibernate complies with the JPA specifications.
3. Main classes and implementations of the hibernate-entitymanager package
(1) hibernatepersistence. Java, implements the JPA persistenceprovider interface, and provides createentitymanagerfactory
And createcontainerentitymanagerfactory to create the entitymanagerfactory object.
Use the buildentitymanagerfactory method of the ejb3configuration object to parse the JPA configuration file persistence. xml and create
Entitymanagerfactory object.
(2) The implementation of the entitymanagerfactory object is the entitymanagerfactoryimpl class, which has one of the most important private attributes.
Sessionfactory is one of the core objects of hibernate. The most important method of this class is createentitymanager.
Entitymnagaer object, and sessionfactory attributes also pass in this method.
(3) The implementation of the entitymanager object is the entitymanagerimpl class, which inherits from the abstractentitymanagerimpl class and
The javasactentitymanager class has an abstract method getsession to obtain the session object of hibernate. It is exactly in this session
With the actual support of the object, the entitymanagerimpl class implements all the methods of the JPA entitymanager interface and completes the actual ORM operation.
.
(4) queryimpl class: The queryimpl class uses the support of entitymanagerimpl to implement the JPA query interface;
(5) transactionimpl uses entitymanagerimpl to implement the entitytransaction interface of JPA.