What is JPA?
The JPA full name Java Persistence API is a set of classes and methods for storing data in a database. JPA describes the mapping of objects-relational tables through JDK 5.0 annotations or XML, and persists entity objects for the run-time into the database .
JPA Providers
JPA is an open source API, Enterprise operators Oracle, Redhat, Eclipse, etc., offering a variety of unique JPA products, including: Hiberate, Eclipselink, Toplink, Spring Data JPA, etc.
JPA Architecture
JPA shows how to define plain oriented Java Object (POJO) as an entity and how to manage the relationship between the entity.
Class-Level schemas
JPA's class-level architecture contains several core components
> entitymanagerfactory: Create and manage multiple Entitymanager instances
> Entitymanager: interface, managing the operation of objects (create, update, delete, Query)
> Entity: Persisted object, stored as record in the database
> entitytransaction: One-on-one with Entitymanager
> Persistence: Contains a static method that gets the Entitymanagerfactory instance
> Query: An interface that operators must implement to obtain a relational object that satisfies Creteria (relational object)
Orm
Many applications currently use relational data inventory data. Recently, suppliers turned to object databases to reduce the pressure of data maintenance. The core of object-relational technology is mapping orm.xml files.
Because XML does not require compatibility, we can easily modify the data source.
ORM Architecture
An ORM programmatically converts an object type into a relationship type. The main feature is to map object to the data in the database. In the process of mapping, we must consider the relationship between data, data types and data.
Stage One
Also known as the object data phase, which includes the Pojo class, the service interface, and the class. It is the primary business layer that contains the business logic operations and attributes. For example, we consider the schema of the employ database
> Employee Pojo class contains attributes: ID, name, salary, and destination, as well as setter and getter methods for these properties
The > Employee Dao/service class contains service methods such as creating an employee, finding an employee, and deleting an employee.
Phase II
aka Mapping or persistance stage, including JPA provider, mapping File (orm.xml), JPA Loader, and Object Grid
> JPA provider: Products provided by operators, including JPA flavor (javax.persistence). such as Eclipselink, Toplink, Hibernate and so on.
> Mapping File: Orm.xml includes Pojo classes to map data in a relational database
> JPA LOADER:JPA Loader cache memory similar to loading relational data.
> Oject Grid: Stores temporary locations for relational data.
Stage Three
Relational data stages, including relational data related to business logic. The modified data is present in the cache memory in the grid format before committing.
The above shows how ORM can store data in a database in three phases.
Mapping.xml
Mapping.xml instructs the JPA vendor to map the entity class to the database table if it worsens.
Annotations
General XML is used to configure a specific build or define a direct relationship between two different builds. In our case, we need to match the properties of the Pojo class with the entity tags in the file when we write the Mapping.xml file. This requires a certain amount of maintenance overhead.
Another scenario is that, in the class definition, we can use annotations to complete the configuration. Annotations are used for class, properties, and before methods. All JPA annotations are defined in the Javax.persistence package.
Entity Relationship
Entity can be considered as a relational table, so the relationships between entity classes are:
> @ManyToOne
> @OneToMany
> @OneToOne
> @ManyToMany
@ManyToOne relationship
Example: the relationship between employee and department is many to one. Department key as the foreign key of the employment. Label within employee
Generate two tables for employee and department, where employment contains department key
@OneToMany relationship
TableA and TableB are a one-to-many relationship, so a record in TableA TableB 0 or more records. such as the relationship between department and employee. Label on department
Department_employee, department, employee
@OneToOne relationship
For example, an employee belongs to only one department. Labeling method with @manytoone, on employee.
Generate two tables for employee and department, where employment contains department key
@ManyToMany relationship
Like the relationship between the class and the teacher. Both sides need to be labeled.
Generate three tables: Teacher_class, Teacher, class
Criteria API
The Criteria API is used to define query, which is another option for JPQA query. String based JPQL query and JPA criteria based query are the same in terms of performance and efficiency.
Criteria Query structure A simple example
Entitymanager em = ...; Criteriabuilder cb = Em.getcriteriabuilder (); criteriaquery<entity class> CQ = cb.createquery (Entity.class); Root<entity> from = Cq.from (Entity.class); Cq.select (Entity); typedquery<entity> q = em.createquery (CQ); List<entity> AllItems = Q.getresultlist ();
The example above shows the basic steps for creating a criteria.
> Entitymanager: Creating Criteriabuilder objects
> criteriaquery: Create a Query object, properties can be modified
> Criteriaquery.from: Set query root
> criteriaquery.select: Set result list type
> typedquery<t>: Ready to execute Query and indicate the type of query result
> typedquery.getresultlist: Executes query, returns a set of results, exists in list
Reference
Http://www.tutorialspoint.com/jpa/jpa_entity_managers.htm
Getting Started with JPA