JPA Basics

Reprinted from: http://www.cnblogs.com/holbrook/archive/2012/12/30/2839842.html

1. Overview of
JPA (Java Persistence API, Java Persistence API)
JPA maintains a Persistence Context (persistence context) , which maintains the entity's lifecycle in the persistence context. It mainly includes three aspects:
ORM metadata. JPA supports two forms of annotation or xml to describe object-relational mapping.
Entity manipulation API. Implement CRUD operations on entity objects.
query language. The object-oriented query language JPQL (Java Persistence Query Language) is agreed.


Comparison with Hibernate
JPA's main APIs are defined in the javax.persistence package. If you are familiar with Hibernate, you can easily make the corresponding:
org.hibernate javax.persistence Description
cfg.Configuration Persistence Read configuration information
SessionFactory EntityManagerFactory Factory class for creating session/entity managers
Session EntityManager Provides entity manipulation API, manages transactions, Create Query
Transaction EntityTransaction Manage Transaction
Query Query Execute Query


Second , entity life cycle
Entity life cycle is a very important concept in JPA, which describes the state transition of entity objects from creation to control, from deletion to free. The operation of the entity is mainly to change the state of the entity.

Status:
New, the newly created entity object has no primary key (identity) value
Managed, the object is in the Persistence Context (persistence context), managed by the EntityManager
Detached, the object has been freed from the Persistence Context, entered the Application Domain
Removed, the entity object Deleted

EntityManager provides a series of methods to manage the life cycle of entity objects, including:
persist, which converts newly created or deleted entities to Managed state and stores data in the database.
remove, delete the controlled entity
merge, change the free entity to the Managed state, and store the data in the database.

If transaction management is used, the commit/rollback of the transaction also changes the state of the entity.

3. Entity Relationship Mapping (ORM)
3.1 Basic Mapping
Object -side database-side annotation Optional annotation
Class Table @Entity @Table(name="tablename")
property column – @Column(name = "columnname")
property primary key @Id @GeneratedValue(strategy = GenerationType.XXX) For details, see ID generation strategy
property NONE @Transient

3.2 ID generation strategy
ID corresponds to the primary key of the database table, which is an important attribute to ensure uniqueness. JPA provides the following ID generation strategies
GeneratorType.AUTO, JPA automatically generates
GenerationType.IDENTITY, uses the self-increasing field of the database, needs the support of the database (such as SQL Server, MySQL, DB2, Derby, etc.)
GenerationType.SEQUENCE, uses the database The serial number needs the support of the database (such as Oracle)
GenerationType.TABLE, which uses the specified database table record ID to grow.
Need to define a TableGenerator:
@TableGenerator( name="myGenerator", table="GENERATORTABLE", pkColumnName = "ENTITYNAME" , pkColumnValue="MyEntity", valueColumnName = "PKVALUE", allocationSize=1 )
can be referenced in @GeneratedValue
@GeneratedValue(strategy = GenerationType.TABLE,generator="myGenerator")

3.
JPA defines four relationships: one-to-one, one-to-many, many-to-one, and many-to-many.
For a database, a foreign key to another table is usually recorded in one table;
corresponding to an entity object, the party holding the associated data is called the owning-side, and the other is called the inverse-side.




Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326802588&siteId=291194637