JPA Introductory Example (using JPA's Hibernate implementation version)

Source: Internet
Author: User

http://blog.csdn.net/hmk2011/article/details/6289151 (1), JPA Introduction:

JPA is all called the Java Persistence API, and the Java Persistence API is the Java Persistence interface proposed by Sun Company in the Java EE 5 specification. JPA draws on the benefits of the current Java persistence technology and is designed to standardize and simplify the persistence of Java objects. Using JPA persistence objects is not dependent on an ORM framework.

Why use Jap?
Before we say why we need to use JPA, we need to understand why we use ORM technology.

ORM is object-relation-mapping, which is the object relation mapping technology, is the core of object persistence. ORM is the encapsulation of JDBC, which solves the various problems of JDBC:

A) cumbersome code issues

With the JDBC API programming access to the database, the code is large, especially in the field of a large number of tables, the code seems cumbersome, cumbersome, error prone. For example: PreparedStatement pstmt=con.preparestatment ("INSERT into account value (?,?,?,?,?,?,?,?,?)");

ORM establishes a mapping between Java objects and database objects, and programmers do not need to write complex SQL statements to manipulate Java objects directly, thereby greatly reducing the amount of code and also allowing programmers to focus more on the implementation of business logic.

b) Database object connectivity Issues

Relational data objects, there are various relationships, including 1 to 1, 1 to many, many to 1, many-to-many, cascade and so on. When database objects are updated with JDBC programming, these relationships must be handled with great care to ensure that there is no error in maintaining these relationships, a process that is time-consuming and laborious.

ORM establishes the relationship between Java objects and database objects, and also automatically creates the relationships of Java objects based on the relationship between database objects, and provides a complete and effective mechanism to maintain these relationships.

c) System Architecture issues

JDBC belongs to the data access layer, but when you use JDBC programming, you must know what database the background is in, what tables it has, what fields are in the tables, what types of fields are, what is the relationship between the tables, what indexes are created, and more about the background database.

Using ORM Technology, the database layer can be completely hidden, presented to programmers only Java objects, programmers only need to call the Java object based on business logic needs getter and setter method, can implement the background database operations, programmers do not have to know what database background, What tables, what fields, and tables are related to tables.

D) Performance issues

With JDBC programming, there are times when there are inefficiencies.

Pstmt =conn.preparestatement ("INSERT into user_info values (?,?)");
for (int i=0; i<1000; i++) {
Pstmt.setint (1,i);
Pstmt.setstring (2, "User" +i.tostring ());
Pstmt.executeupdate ();
}

The above program will send 1000 SQL statement execution requests to the background database, and the operation is less efficient.

Using ORM Technology, the ORM Framework will automatically delay the sending of SQL requests to the background database according to the needs of the specific database operation, and ORM can synthesize the database access operations according to the actual situation, and minimize unnecessary requests for database operations.

JPA is one of the most popular ORM technologies, so he has all the features of ORM technology, and of course he has some advantages:

1 standardisation
JPA is one of the Java EE standards published by the JCP organization, so any framework that claims to be compliant with the JPA standards follows the same architecture and provides the same access APIs, which ensures that JPA-based enterprise applications can run under different JPA frameworks with minimal modifications.
2 support for container-level features
The JPA framework supports container-level transactions such as big data sets, transactions, concurrency, and so on, which makes JPA beyond the limits of a simple persistence framework and plays a greater role in enterprise applications.
3 easy to use, easy to integrate
One of the main goals of JPA is to provide a simpler programming model: creating entities in the JPA framework is as simple as creating Java classes without any constraints or restrictions, only using Javax.persistence.Entity, the JPA framework and interface are also very simple, there are not too many special rules and design patterns of requirements, developers can easily grasp. JPA is designed on a non-intrusive basis, so it can be easily integrated with other frameworks or containers.
4 query capability comparable to JDBC
JPA's query language is object-oriented rather than database-oriented, and it constructs query statements in object-oriented natural syntax, which can be regarded as the equivalent of Hibernate hql. JPA defines a unique JPQL (Java persistence query Language), JPQL is an extension of EJB QL, a query language for entities that manipulate objects as entities rather than relational database tables, and can support batch updates and modifications, JOIN, GROUP by, have, and so on, usually only SQL can provide advanced query features, and even can support subqueries.
5 Support for advanced object-oriented features
The ability of JPA to support advanced object-oriented features such as inheritance between classes, polymorphism, and complex relationships between classes allows developers to maximize the use of object-oriented models to design enterprise applications without needing to handle the persistence of these features in relational databases on their own.

(2), specific examples

JPA Introductory Example (using JPA's Hibernate implementation version)

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.