JPA (Java persistence API)

Source: Internet
Author: User

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.
Sun introduced the new jpa orm specification for two reasons: first, simplified the development of Object Persistence for existing Java EE and Java SE applications; second, sun wanted to integrate the ORM technology, to achieve global recovery.
JPA was developed by the EJB 3.0 software Expert Group as part of the JSR-220 implementation. But it is not limited to EJB 3.0. You can use it in Web applications or even desktop applications. The purpose of JPA is to provide persistence standards and specifications for pojo. It can be seen that after several years of practical exploration, the idea of being able to run independently from containers for convenient development and testing has become deeply rooted in the hearts of the people. Currently, Hibernate 3.2, toplink 10.1.3, and openjpa all provide JPA implementation.
The overall idea of JPA is basically the same as that of existing ORM frameworks such as Hibernate, toplink, and JDO. In general, JPA includes the following three technologies:
Orm ing metadata. JPA supports two types of metadata: XML and JDK 5.0 annotations. The metadata describes the ing between objects and tables. The framework then persists object to database tables;
JPA APIs are used to operate entity objects and perform crud operations. The framework completes all the tasks for us in the background. Developers start from tedious JDBC and SQL Code .
Query Language, which is an important aspect of persistent operations. data can be queried through object-oriented rather than database-oriented query languages to avoid Program SQL statements are tightly coupled.
Iii. 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 hibernate hql. JPA defines a unique jpql (Java persistence Query Language). jpql is an extension of ejb ql. It is a query language for entities, and the operation object is an entity, 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 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.
4. JPA suppliers
One of the goals of JPA is to develop an API that can be implemented by many vendors, and developers can code it to implement this API, rather than using the API unique to private providers. Therefore, developers only need to use the vendor-specific API to obtain the functions that the JPA specification does not solve but are required in the application. Use JPA APIs as much as possible. However, you can use APIs that are unique to suppliers when you need to publish the APIS but are not provided in the specifications.
1 hibernate
JPA requires providers to implement its functions, and Hibernate is a strong one in JPA providers. Currently, no one can find its right. In terms of functionality, JPA is now a subset of the hibernate function. Hibernate is compatible with JPA since 3.2. Hibernate3.2 is compatible with sun TCK's JPA (Java persistence API.
As long as you are familiar with hibernate or other ORM frameworks, you will find it very easy to get started when using JPA. For example, the state of an object is free, persistent, and free in hibernate. In JPA, there are new, managed, detached, and removed. People can see it at a Glance. These statuses all correspond one to one. Another example is the flush method, which corresponds to each other, and the other example is query = manager. createquery (SQL), which is written in hibernate as a session, and becomes a manager in JPA, so the cost from Hibernate to JPA should be very small.
Similarly, JDO is compatible with JPA. In the field of ORM, it seems that JPA is king, and norms are specifications. With the support of major vendors, JPA has become widely used.
2 spring
Spring + Hibernate is often referred to as the most popular framework combination of Java Web applications. However, the Web beans JSR passed in JCP wants to include JSF + EJB + JPA and JBoss Seam (excluding spring) some components and EJB 3 (currently a simplified Session Bean framework that provides basic interception and dependency injection functions) are standardized. Today's spring 2.0 provides a complete EJB container contract for JPA, allowing JPA to be used in the service layer managed by spring in any environment (including all Spring AOP and di enhancements ). At the same time, the debate on the next web application combination, EJB, spring + hibernate or spring + JPA, has long been filled with ears.
In spring 2.0.1, the official support for JPA has also contributed to the development of JPA. The advantage of JPA is that it can be separated from the container and become more concise.
3 openjpa
Openjpa is an open-source project provided by Apache. It implements the JPA standard in EJB 3.0 and provides developers with a powerful and simple persistent data management framework. Openjpa encapsulates operations that interact with relational databases, allowing developers to focus on writing business logic. Openjpa can be used as an independent persistence layer framework, and can be easily integrated with other Java EE application frameworks or containers that comply with the EJB 3.0 standard.
4 others
Currently, toplink and hibernate entitymanager are supported. Toplink was previously charged and is now open-source. Although openjpa is free of charge, more efforts are needed in terms of functions, performance, and popularity.
For EJB, entity beans have always been criticized because they are too complex and huge. The emergence of JPA largely separates complexity. This makes it easy to promote EJB.
All in all, the JPA specification focuses only on API behavior, and most performance-related optimizations are completed by various implementations. However, all reliable implementations should have some data cache as an option. Hopefully in the near future, JPA will become a real standard.
V. Summary
Ejbs 3.0 and JPA are undoubtedly the main selling points of Java ee 5. In some fields, they give Java Community It brings about a competitive advantage and makes Java different from competitors in other fields (because, it is undeniable that there are no standard-based methods in some fields currently ).
Over the past few years, Spring framework has been the main competitor of EJB in the enterprise field. The ejb3.0 specification solves many issues that promote the rise of spring. With its appearance, ejb3.0 undoubtedly provides a better development experience than spring-the most striking advantage is that it does not need configuration files.
JPA provides a standard or ing solution that is fully integrated into ejb3. 0 compatible containers. JPA's predecessors will continue to develop steadily, but the raw usage in business applications may be reduced. The Entity Manager that implements JPA compatibility seems likely to be the development direction of such technology.
The major issues of the Java EE series specifications have nothing to do with JPA. Java EE specifications involve integration between web and EJB containers. Spring still has major competitive advantages in this field. The JBoss Seam project tries to use a custom method to solve this problem. The Caucho Resin Application Server tries to expand container boundaries and supports @ EJB annotations in Web containers. We hope that Java EE 5.1 will solve the problem of layer integration and provide us with a comprehensive and standard dependency injection method.
In the near future, sun may treat JPA as a separate JSR, and JPA may also be part of Java SE. But these are not very important. What's important is that we can now use JPA in Java SE applications without leaving the container.
As a standard for Object persistence, JPA not only supports Java EE application server, but also can be directly used in Java SE. Developers will not have to make a hard choice among the various existing ORM frameworks. According to Sun's expectation, the halo of the existing ORM framework will gradually fade out and will not become attractive in the past.

Related Article

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.