Java Persistence API

Source: Internet
Author: User

First, Introduction

JPA (Java persistent api) Span lang= "ZH-CN" style= "Font-family:simsun" to enitity JPA entitymanager entity Span lang= "en-US" style= "Font-family:calibri" >JPA The implementation translates this operation into an operation on the underlying data table.

JPA orm Span lang= "ZH-CN" style= "Font-family:simsun" > frame, so use orm hiernate toplink

is a JPA and the ORM the relationship between frames


Second,JPASpecification Introduction

JPA The three most common components used during the operation are:

1

pojo ( simple Span lang= "en-US" style= "Font-family:calibri" >java objects ( plain old Java Objects " is actually the normal javabeans) Span lang= "ZH-CN" style= "Font-family:simsun" > mapping file or Annotation orm.xml map files or annotations to create correspondence between entities and underlying data tables.

2 entitymanger

The entity is simply a simple that has a mapping relationship to the underlying data table. java entitymanager JPA spec to convert this operation to operations on the underlying database, so entitymanger hibernate frame Span lang= "en-US" style= "Font-family:calibri" >session

3 , JPQL Enquiry

similar toHibernateprovidehqlQuery,JPAprovides aJPQLquery statements, simple and easy to use query statements, can be very easily retrieved to save the entity. JPAprovides aQueryinterface to execute the query,Entitymanageraccording to the existingJPQLto createQueryobject, and then by theQueryobject to execute the query.

Third,JPAentities in the specification

in the JPA The specification involves two commonly used concepts that persist the context ( Persistence Context ) and the Persistence unit ( Persistence Unit )

1 , Persistence context

A persistence context is a collection of entities that are combined in a managed state, in Enititymanager management, so the application can be Entittymanager interacting with the persistence context, when the application Cheng changes the entity state in the persistence context, Entitymanager will be based on the specified Flush Mode writes the state of the entity into the underlying database.

2 , Persistence Unit

The persistence unit is defined by the configuration file. I do not understand a lot, and then do a detailed introduction.

3 , the state of the entity

with the Hibernate similar, JPA entities in the specification also have several object states

( 1 ) NEW: Entity has new operation created, but not with Span lang= "en-US" style= "Font-family:calibri" >entitymanger The associated entity is considered to be in the new state, and the new state is not persisted to the database.

( 2 ) managed: The entity in the managed state has a corresponding record in the database, and has a persistent identity, the entity under the managed state can be just saved, or it can be just loaded, either, the managed entity is in Entitymanager under management, JPA JPA Responsible for converting this modification to a modification of the database record.

( 3 ) off-pipe: If an entity was once in a managed state, Then he has a persistent identity, but with it associated with the entitymanager is closed, the entity changes into a de-tube state, the entity's reference in the off-state is still valid, but the entity is separated from the entitymanager

( 4 ) is deleted: when Entitymanager called Remove method deletes the specified entity, the entity enters the deleted state, the deleted entity also has a persistence context associated with it, and its corresponding record is about to be deleted from the database.

Evolution diagram of the entity state:


about the JPA There 's a lot of knowledge, it's just fur, but with what I've learned before Hibernate Comparative Study, it is not so strange, the next blog will introduce JPA the mapping relationship.

Java Persistence API

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.