Relationship between Hibernate and JPA

Source: Internet
Author: User

Hibernate analysis:

Hibernate is an open-source code object-link ing framework that encapsulates JDBC objects in a lightweight manner. It allows Java programmers to operate databases as they wish.

Core interfaces of hibernate:

Session: executes the crud of the Persistent object, which is thread insecure.

Sessionfactory: initializes hibernate, acts as a proxy for data storage sources, creates session objects, and uses the factory mode.

Transaction: Responsible for transaction-related operations.

Query and criteria: Responsible for transaction-related operations.

Configuration: configures and starts hibernate, and creates the sessionfactory object. During hibernate's startup, first locates the ing document location, reads the document, and creates the sessionfactory.

JPA: javapersistence API analysis:

The purpose of JPA is to provide persistence standards for pojo. The JPA framework supports large data volumes. For transactions, concurrencies, and other container-level transactions, JPA is designed based on non-intrusive principles, so it is very integrated with frameworks or containers.

Technology provided by JPA

(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 keeps entity objects in 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, and developers can remove them from tedious JDBC and SQL code.

(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.

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, this 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 greater 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 or restrictions. You only need to use javax. persistence. entity. The JPA framework and interfaces are also very simple. developers can easily master them without many special rules and design patterns. JPA is designed based on non-intrusive principles, so it can be easily integrated with other frameworks or containers.

(4) comparable to the query capability of JDBC

JPA's query language is object-oriented rather than database-oriented. It constructs query statements with object-oriented natural syntax and can be seen as the equivalent of hibernatehql. JPA defines a unique jpql (Java persistence Query Language). jpql is an extension of ejbql. It is a query language for a needle to an object, and the operation object is an object, instead of relational database tables, it also supports advanced query features that can be provided only by SQL, such as batch update and modification, join, group by, and having, and even 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 use object-oriented models to design enterprise applications to the maximum extent, without the need to handle the persistence of these features in relational databases.

Functional relationship:

In terms of functionality, JPA is now a subset of the hibernate function.

For example:

(1) The State of an object. In hibernate, there are three types: free, persistent, and free. In JPA, there are new, managed, detached, and removed, and these statuses all correspond one to one.

(2) The flush method corresponds to each other,

(3) query = manager. createquery (SQL), which is written as session in hibernate, and changed to manager in JPA

Another link:

JPA is a standard interface, while Hibernate is an implementation.

Hibernate is implemented through three components, including 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 does not directly provide the ORM function, instead, it encapsulates hibernate-core so that hibernate complies with JPA specifications.

Hibernate and JPA classes and Implementations

(1) hibernatepersistence. java implements the JPA persistenceprovider interface and provides createentitymanagerfactory and handler methods to create the entitymanagerfactory object. Both methods call the buildentitymanagerfactory method of the ejb3configuration object to parse the JPA configuration file persistence. XML, and create

Entitymanagerfactory object.

(2) The implementation of entitymanagerfactory object is the entitymanagerfactoryimpl class, which has one of the most important private attributes and is sessionfactory, one of the core objects of hibernate. The most important method of this class is createentitymanager, which returns the entitymnagaer object, and the sessionfactory attribute also passes in this method.

(3) The implementation of the entitymanager object is the entitymanagerimpl class, which inherits from the abstractentitymanagerimpl class and has an abstract method getsession in the abstractentitymanager class to obtain the hibernate session object, with the actual support of this session object, the entitymanagerimpl class implements all methods of the JPA entitymanager interface and completes the actual ORM operations.

(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.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.