The relationship between JPA and hibernate

Source: Internet
Author: User

The JPA Java Persistence API is a standard ORM interface for Java EE 5 and is part of the EJB3 specification.

Hibernate, today's popular ORM Framework, is an implementation of JPA, but its functionality is a superset of JPA.

the relationship between JPA and hibernate can simply be understood as JPA is the standard interface, andhibernate is implemented. So how does hibernate achieve this relationship with JPA?

Hibernate is mainly achieved through three components, and hibernate-annotation,Hibernate-entitymanager and Hibernate-core.

Hibernate-annotation is the basis for hibernate to support the annotation mode configuration, which includes the standard JPA annotation and the annotation of Hibernate's own special features.

Hibernate-core is the core implementation of Hibernate and provides all the core functions of hibernate.

Hibernate-entitymanager implements the standard JPA, which can be seen as an adapter between Hibernate-core and JPA, which does not directly provide ORM functionality, but rather encapsulates the hibernate-core, making hibernate complies with the JPA specification.

Maven dependencies:

<dependency>            <groupId>org.hibernate</groupId>            <artifactId> Hibernate-entitymanager</artifactid>            <version>4.2.4.Final</version></dependency> <dependency>            <groupId>org.hibernate</groupId>            <artifactid>hibernate-core</ artifactid>            <version>5.2.2.Final</version></dependency><dependency>            < groupid>org.hibernate</groupid>            <artifactId>hibernate-annotations</artifactId>            <version>3.5.6-Final</version></dependency>

The following highlights the main class and implementation of the Hibernate-entitymanager package.

Hibernatepersistence.java, implements the JPA Persistenceprovider interface, which provides createentitymanagerfactory and createcontainerentitymanagerfactory two method to create a Entitymanagerfactory object, both of which are the buildentitymanagerfactory methods of the called Ejb3configuration object. To parse the JPA configuration file Persistence.xml, and create the Entitymanagerfactory object.

The implementation of the Entitymanagerfactory object is the Entitymanagerfactoryimpl class, which has one of the most important private properties 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 is passed into the method.

The implementation of the Entitymanager object is the Entitymanagerimpl class, which inherits from the Abstractentitymanagerimpl class, In the Abstractentitymanager class, there is an abstract method getsession to get Hibernate's Session object, which is exactly supported by this session object. The Entitymanagerimpl class implements all the methods of the JPA Entitymanager interface and completes the actual ORM operation.

In addition, theQueryimpl class in the Hibernate-entitymanager package implements the JPA query interface with the support of the Entitymanagerimpl The Transactionimpl uses Entitymanagerimpl support to implement the JPA Entitytransaction interface.

At this point,hibernate has completed all of the support work for JPA through the Hibernate-entitymanager package.

Finally, add a question that I am surprised by:

The Getsingleresult () method of the Query object in JPA, which throws noresultexception when the query does not reach the result, and throws nonuniqueresultexception when the query is to multiple results , and Noresultexception and nonuniqueresultexception are all runtimeexception.

There are two problems with this:

1, I think the Getsingleresult method should allow the query can not be the result of the situation exists, when it returns NULL, there is no need to throw an exception;

2, even if you need to query the results or query to throw an exception, should not throw runtimeexception, because this means that the code is not required to display the Try-catch block to catch these exceptions, it will not cause the user to pay attention to these two exceptions.

At present, I use the method to solve this problem

try{     Object o = Query.getsingleresult ();} catch (Noresultexception ex) {     return null;} catch (Nonuniqueresultexception ex) {     o = queryobject.getresultlist ();    Return ((List) O). Get (0);}

The relationship between JPA and hibernate

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.