Introduction to Persistent API (JPA) series (i) Entities

Source: Internet
Author: User
Tags jboss application server

The Java EE specification developed the Entitybean specification to solve the application persistence problem, but it has been criticized in the EJB specification because of the complexity of the design.

Subsequently, a large number of open-source ORM frameworks (such as Hibernate) appear. These ORM frameworks are simple, easy to use, and can be object-oriented to manipulate relational databases and provide powerful data query capabilities. Slowly, these ORM frameworks are constantly replacing Entitybean.

When Sun company developed the JAVAEE5 specification, Sun Company completely abandoned the original Entitybean, but introduced the new JPA specification · Sun's JPA, with entity as its core, maps entities to the underlying data tables, and then operates on them by using JPA's Entitymanager in the application, which the JPA implementation translates into operations on the underlying data table.

one thing to note: JPA is just a specification! Not any specific ORM framework! When using JPA for persistence in a program, the underlying must be implemented as a concrete ORM framework.

The entity of EJB3 is the product of fully absorbing ORM (object/relationmapping) thought.

Common ORM frameworks, such as: ENTITYEJB, Hibernate, IBATIS, TopLink, OJB

(i) Object Relational database mapping (ORM)

The full name of the ORM is object/relationmapping, object/relational database mapping · ORM can be understood as a specification that outlines the basic features of such frameworks and accomplishes the mapping of object-oriented programming languages to relational databases. when the ORM framework completes the mapping, it can take advantage of the simple usability of the object-oriented programming language and the technical advantages of the relational database. Therefore, we can consider the ORM framework as a bridge between applications and databases.

When we use an object-oriented programming language for application development. Object-oriented analysis, object-oriented design and object-oriented programming have been used since the beginning of the project, but it is a very bad feeling to go back to the relational database when the persistent layer database is accessed. So we need a tool that can wrap relational databases into object-oriented models. This tool is the ORM framework.

With the development of object-oriented thinking, the object-oriented database (not as the focus of this paper) appears.

The advantages of the object-oriented programming language:

      1. Object-oriented modeling, manipulation
      2. Polymorphism, inheritance.
      3. Abandon the incomprehensible process.
      4. Easy to use and easy to understand.

Relational database systems are more advantageous than object-oriented languages, such as:

      1. Large amounts of data lookup, sorting.
      2. Collection data connection operations, mappings
      3. concurrency, transactions for database access
      4. Database constraints, Isolation

Faced with the coexistence of object-oriented language and relational database system, the use of ORM becomes a necessity.

The purpose of the JPA implementation is to transform the operation of the entity into the operation of the database. This allows the application to manipulate entities in a pair-oriented manner, while the JPA implementation is responsible for translating the corresponding SQL (Structured Query Language) operations.

The JPA specification is essentially an ORM specification, note that it is not an ORM framework. Because JPA does not provide an ORM implementation. It just has a few specifications that provide some programming API interfaces, but the implementation is provided by the application server vendor. JBoss Application Server is implemented as JPA with Himate

The developer interfaces to the JPA specification, but the underlying JPA implementation can switch arbitrarily.

(ii) JPA mapping rules

    • The data table maps the entity class.
      • When you use this entity class to create instances, modify properties, delete entities, the system automatically converts to CRUD operations on this table
    • Row map entity object for a data table
    • property of the column (field) mapping entity for the data table


Based on this basic mapping approach, the JPA specification accomplishes the mapping between the object model and the relational model. Thus, in the JPA specification, the entity is just an intermediary, and the application only needs to manipulate the entity objects, and the ORM Framework is responsible for translating this into operations on the underlying database.

(This conversion is transparent to developers and developers need not care!) This allows developers to free themselves from the relational database schema and manipulate relational databases with object-oriented thinking. )

(iii) Introduction to the JPA specification

The JPA specification does not belong to the EJB specification, it is a completely separate set of specifications.

JPA can be used not only in Java EE applications based on EJB, but also in ordinary javase applications.

The 3 most common components that are used during JPA operations are as follows:

(1) Entity

is an ordinary Pojo, just adds a orm.xml mapping file or annotation to it.

Objective: To establish a correspondence relationship between the entity and the underlying data table.

(2) Entitymanager

An entity is simply a pojo that has a mapping relationship to the underlying data table, which itself does not have any persistence capability, and the JPA specification can convert this operation to the underlying database only when using Entitymanager to manipulate the entity. The Entitymanager of the JPA specification is somewhat similar to the session of the Hibernate framework.

Similar to the session in Hibernate, when a program needs to use JPA to add, delete, and update entities, the application needs to use the Entitymanager interface to complete • In addition, if the application needs to retrieve entities, The query object is created by Entitymanager according to JPQL.

(3) JPQL query

similar to the HQL query language provided by Hibernate, JPA provides a JPQL query language.

This query language is not simple, easy to use, it can be very convenient to retrieve the saved entities · JPA provides a query interface to execute queries, Entitymanager to create a query object based on its own jpql, and then run the queries by the query object.

Summarize:

The purpose of the JPA implementation is to transform the operation of the entity into the operation of the database. This allows the application to manipulate entities in a pair-oriented manner, while the JPA implementation is responsible for translating the corresponding SQL (Structured Query Language) operations.
JPA is just a specification! Not any specific ORM framework! When using JPA for persistence in a program, the underlying must be implemented as a concrete ORM framework.

Introduction to Persistent API (JPA) series (i) Entities

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.