jpa(Java Persistence API)

JPA is referred to as the Java Persistence API, the Chinese name of the Java Persistence API, is JDK 5.0 annotations or XML description of the object - mapping between relational tables, and run the entity object persistence to the database.

Sun introduced the new JPA ORM specification for two reasons: First, simplify the existing Java EE and Java SE application development work; Second, Sun hopes the integration of ORM technology, the world normalized.

 

origin

JPA EJB 3.0 software developed by the expert group, as part of JSR-220 implementation. But it is not limited to EJB 3.0, Web application you can even use desktop application. JPA's mission is to provide persistent POJO standards, we can see, after several years of practice and exploration, can run independently from the container, facilitate the development and testing of the concept has gained a. Hibernate3.2 +, TopLink 10.1.3 and OpenJPA provides implementation of JPA.

JPA's overall thinking and existing Hibernate, TopLink, JDO and other ORM frameworks broadly consistent. In general, JPA includes the following three aspects of the technology:

ORM mapping metadata

XML annotation and JPA support JDK5.0 form two kinds of metadata, the metadata describes the mapping relationships between the objects and the table, whereby the framework entity object persistent database table;

API

For operating entity object, perform CRUD operations, the framework in the background alternative we have completed everything, developers freed from the tedious JDBC and SQL code.

Query Language

This is the persistence of a very important operation aspect, rather than object-oriented database query language for query data, to avoid the tight coupling process SQL statements.

(This section is summarized in 360 words)

=======================================================================================================================

 

What is 1.1 JPA

JPA (Java Persistence API) Java Persistence API. Sun's Java ORM is a program developed by the official, is the norm, standard, sun and did not realize the company's own

 

Concerns: ORM, standard concepts (keywords)

 

What is 1.1.1 ORM

 

ORM (Object Relational Mapping) object-relational mapping.

Q: ORM what's the use?

Prior to operation of the database, first data table associated with the entity class. Then by an object operating entity classes (CRUD) database tables, this is the ORM behavior! 

So: ORM is implemented using a design object operation database! !

By this statement, we know the role of the JPA is through the operation of the database objects, without writing a single sql statement.

 

1.2 JPA implementer

Since we say JPA is a set of standards, it means that it is only a theory ORM implements the interface. No code implementation. 

So we must have a specific implementers can achieve complete ORM operation function! 

  

JPA framework on the mainstream market (implementors) are: 

Hibernate (JBoos), EclipseTop (Eclipse community), OpenJPA (Apache Foundation). 

  

Hibernate is one of many in which the implementors among the best performance. So, we have this teaching also choose Hibernate framework as the JPA's speakers framework. 

Reminder: learning a JPA framework, other frameworks are the same use

What is the role of 1.3JPA (issue)

JPA is a set of standard ORM, since JPA ORM is born, then the role of the JPA is implemented using object operation database without writing SQL !!!. 

 

Question: sql database is operational, and that with object manipulation, SQL generated by whom? 

Answer: JPA implementation framework 

 

The benefits of using JPA 1.4

Using the JPA, it can be used directly manipulate the database objects, SQL generated by the frame according to the mapping relationship. Developers do not have to write. In doing so, developers do not need to write SQL statements.

  

Question: so what good is it? 

 

A: SQL syntax of different database is different, if not need to write SQL statements. To mask the differences of various SQL databases. So, the code can be written in a code that is compatible with multiple databases! ! ! !

 

 

1.5 Description mapping annotations

annotation

Explanation

@Entity

声明该实体类是一个JPA标准的实体类

@Table

指定实体类关联的表,注意如果不写表名,默认使用类名对应表名。

@Column

指定实体类属性对应的表字段,如果属性和字段一致,可以不写

@Id

声明属性是一个OID,对应的一定是数据库的主键字段

@GenerateValue

声明属性(Object ID)的主键生成策略

@SequenceGenerate

使用SEQUENCE策略时,用于设置策略的参数

@TableGenerate

使用TABLE主键策略时,用于设置策略的参数

@JoinTable

关联查询时,表与表是多对多的关系时,指定多对多关联表中间表的参数。

@JoinColumn

关联查询时,表与表是一对一、一对多、多对一以及多对多的关系时,声明表关联的外键字段作为连接表的条件。必须配合关联表的注解一起使用 <key>

@OneToMany

关联表注解,表示对应的实体和本类是一对多的关系

@ManyToOne

关联表注解,表示对应的实体和本类是多对一的关系

@ManyToMany

关联表注解,表示对应的实体和本类是多对多的关系

@OneToOne

关联表注解,表示对应的实体和本类是一对一的关系

 

 

1.6 JPA常用API说明

API

说明

Persistence

用于读取配置文件,获得实体管理工厂

EntityManagerFactory

用于管理数据库的连接,获得操作对象实体管理类

EntityManager

实体管理类,用于操作数据库表,操作对象

EntityTransaction

用于管理事务。开始,提交,回滚

TypeQuery

用于操作JPQL的查询的

Query

用于操作JPQL的查询接口,执行没有返回数据的JPQL(增删改)

CriteriaBuilder

用户使用标准查询接口 Criteria查询接口

Guess you like

Origin www.cnblogs.com/weishenme/p/11305380.html