Advantages and disadvantages of JPA and Hibernate

Source: Internet
Author: User
Tags new set glassfish java se

Http://developer.51cto.com/art/200906/130500.htm JPA (Java Persistence API) is the Java Persistence specification proposed by Sun. It provides Java developers with an object/relational mapping tool to manage relational data in Java applications. This article will compare the pros and cons of JPA and hibernate.

Java Persistence API

Since EJB technology can begin to be applied, there is always doubt about its usability in practical applications. In my opinion, the two most important causes of this phenomenon are complexity and resource-intensive nature. As a result, frameworks (such as Spring and Hibernate) that are simpler than EJBS and have smaller resource spaces are then popular. To illustrate this point, we note that the direction of the EJB 3.0 specification has been a major change from the previous one. As part of JSR 220, the specification provides features such as Plain old Java Object (POJO) support, Dependency injection (Dependency injection), and annotations. A new set of Api:java persistence APIs (JPA) is now introduced to allow developers to manage relational data in Java EE (even SE) applications. In addition, Sun claims that the Java persistence API shows some of the best ideas for Hibernate, TopLink (both of which are discussed later), JDO, and the EJB framework.

Currently, the GlassFish project provides a reference for implementing JPA in the GlassFish application server as part of the TopLink Essential. You can find the JPA reference implementation on the GlassFish community page. Do not confuse TopLink Essentials and TopLink, the former is now a relational mapping tool owned by Oracle Corporation. I'll discuss the TopLink framework later in this article.

Let's discuss some scenarios where you should consider applying JPA as a persistence framework.

When to consider JPA as a persistence framework

You choose to choose from popular frameworks (such as Hibernate, TopLink, and EJB) to apply a standard-based framework that has "good" functionality.

You need a lightweight persistence framework that does not require the services provided by the EJB's container.

You need a persistence framework that can be used in standard or Enterprise Java applications.

When to consider JPA alternatives

The version of Java that you use determines whether you can actually apply JPA. JPA is part of the EJB 3.0 specification, which is part of the Java EE 5 version. If you have not updated to Java EE 5, you cannot use JPA.

Your application requires services that JPA cannot provide, such as those provided by the EJB container, in which case you rely more on EJBS.

Before concluding the discussion of this framework, let's list some of the advantages and disadvantages of using JPA as a persistence framework.

What are the advantages of JPA?

JPA is standards-based. A growing number of providers are looking to provide JPA implementations in the near future.

It provides the best features in Hibernate and TopLink.

It can be used with Java SE and Java EE applications, using EJB containers, or not.

What are the drawbacks of JPA?

Because it is very new, the JPA specification may need to go through important developments before it becomes stable.

JPA is a specification rather than a product. You need a provider to provide an implementation to gain the benefits of these standards-based APIs.

Hibernate

Hibernate is an object persistence framework that simplifies object-relational mapping between Java applications and underlying relational databases. The method is to provide transparent persistence of the POJO as a "mediation" layer to provide automatic persistence and to load objects from Java applications to database tables. With Hibernate, it is as easy to save the object state to the database and load the object state from the database as the method in calling the Java object. You do not need to manage the underlying data operations from your application code; The Hibernate framework does all the intermediate steps for you.

Let's discuss some scenarios where you'll consider applying Hibernate as a persistence framework, and those that you'll be looking for alternatives.

When to use Hibernate as a persistence framework

You are looking for a simple persistence framework that is easy to learn and use. Before you can actually start persisting your Java objects to the target database, you only need to know a few mapping profiles.

You are looking for a very common and flexible persistence framework. Hibernate is very flexible: it can be used regardless of whether or not an application server is available, whether or not a relational database system is available.

You do not want to pay for the acquisition and maintenance costs. Hibernate is open source and free of charge.

The Hibernate framework is well worth the application because it is very simple and flexible, but also very powerful. However, you may want to consider applying additional frameworks in some of the following scenarios.

When to consider Hibernate's alternative framework

You don't want to have another frame. Despite its simplicity, the Hibernate framework still has its own learning curve, maintenance/update cycle, and so on.

You need the services provided by the container, such as those provided by the EJB, in which case your choice is limited to EJBS.

If you are using or planning to use Hibernate as your persistence framework, here are some of its advantages and disadvantages.

What are the advantages of hibernate?

Hibernate is easy to learn and use. As I mentioned above, before you can use it, you only need to know a few simple, self-describing configuration files.

It's very flexible. You can use Hibernate in any application architecture that requires a persistence service. You can use it in standard Java applications, enterprise Java applications through servlets and/or Enterprise Java beans. It can also be well integrated with the Spring framework.

It can be scaled up well because it is designed to work from the bottom up to the clustered environment. The performance of the latest version of Hibernate has also been enhanced with Lazy initialization technology and optimized Java reflection through the CGLIB runtime byte code generation library.

What are the drawbacks of hibernate?

Hibernate is another framework that has its own application and maintenance cycles.

Despite the positive community support, there are times when the lack of a dedicated provider of this product makes it unconvincing to use this framework.

Advantages and disadvantages of 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.