Relationship between JPA and hibernate 1

Source: Internet
Author: User

 

JPA Java persistence API is a standard ORM interface of Java ee 5 and part of ejb3 specifications.

Hibernate, a popular ORM framework today, is an implementation of JPA, but its function is the superset of JPA.

The relationship between JPA and hibernate can be simply understood as JPA is a standard interface, and Hibernate is an implementation. So how does hibernate work with JPA?
. Hibernate is mainly implemented through three components, and hibernate-annotation, Hibernate-
Entitymanager and hibernate-core.

Hibernate-annotation is the basis for hibernate to support annotation configuration. It includes the standard JPA annotation and Annotation of hibernate's special functions.

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

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, but encapsulates hibernate-core, makes hibernate conform to JPA specifications.

The following describes the main classes and implementations of the hibernate-entitymanager package.

Hibernatepersistence. Java implements the JPA persistenceprovider interface, which provides
Createentitymanagerfactory and createcontainerentitymanagerfactory
Entitymanagerfactory object. Both methods are called at the bottom of the ejb3configuration object.
Buildentitymanagerfactory method to parse the JPA configuration file persistence. XML, and create
Entitymanagerfactory object.

The implementation of entitymanagerfactory object is the entitymanagerfactoryimpl class, which has the most important private
The property is sessionfactory, 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.

The implementation of entitymanager objects is the entitymanagerimpl class, which inherits from abstractentitymanagerimpl
Class, there is an abstract method getsession in the javasactentitymanager class to obtain the session object of hibernate, exactly in this
With the actual support of the session object, the entitymanagerimpl class implements all methods of the JPA entitymanager interface and completes the actual ORM operation.
.

In addition, the queryimpl class in the hibernate-entitymanager package uses entitymanagerimpl to implement the JPA
The query interface; transactionimpl implements the entitytransaction interface of JPA using the support of entitymanagerimpl.

So far, Hibernate has completed all support for JPA through the hibernate-entitymanager package.

Finally, let me add a question that surprised me:

The getsingleresult () method of the query object in JPA. If the query results cannot be queried, noresultexception is thrown. When multiple ends are queried
If the returned result is nonuniqueresultexception, And the noresultexception and
Nonuniqueresultexception is a runtimeexception.

There are two problems:

1. I think the getsingleresult method should allow the query results to exist. In this case, it returns NULL, and there is no need to throw an exception;

2. A runtimeexception should not be thrown even if no results are found or multiple results are queried, this means that you do not need to use the try-Catch Block to capture these exceptions for code display, so you will not pay attention to these two exceptions.

To solve this problem, I am using

Try {object o = query. getsingleresult ();} catch (noresultexception
Ex) {return NULL;} catch (nonuniqueresultexception ex) {o =
Queryobject. getresultlist (); Return (list) O). Get (0 );}

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.