Using JPA in the project, the first time you see a face mask, this checks the way the JPA queries, and the concepts.
-
- Jpa
- Criteria Query
- Criteriabuilder Secure Query Creation factory
- Criteriaquery Security Query Subject sentence
- Root
- predicate filter conditions
- predicate multiple filter conditions
JPA
Concept
Creating repositories that use the Java Persistence API is a tedious process that takes a lot of time and requires a lot of boilerplate code. One recommended way is to use meta
meta-model
concept
in JPA, a standard query is based on the concept of a meta-model, which is defined for a managed entity for a specific persistence unit. These entities can be entity classes, The parent class of the embedded class or map. The class that provides the managed entity meta information is the Metamodel class.
simply means that the Metamodel is a "managed entity" of the entity class
- for instance:
entity class employee (defined in Com.demo.entities package)
@Entity@Tablepublic class Employee{ private int id; private String name; private int age; @OneToMany private List<Address> addresses; // Other code…}
The name of the standard meta-model class for the Employee class is Employee_
Import javax.annotation.Generated;Import Javax.persistence.metamodel.SingularAttribute;Import Javax.persistence.metamodel.ListAttribute;Import Javax.persistence.metamodel.StaticMetamodel; @StaticMetamodel (employee.class) public class employee_ {public static volatile SingularAttribute< Employee, integer> ID; public static volatile Singularattribute<employee, integer> age; public static volatile Singularattribute<employee, string> name; public static volatile Listattribute<employee, address> addresses;}
Each attribute of an employee is mapped in the corresponding Metamodel class using the following rules described in the JPA2 specification:
- The properties of the meta-model class are all static and public.
The properties of the meta-model class are all static and public. Each attribute of an employee is mapped in the corresponding Metamodel class using the following rules described in the JPA2 specification:
For collection types such as addess, a static property is defined listattribute< A, b> B, where list object B is the object defined in type B in class A. Other collection types can be setattribute, Mapattribute, or Collectionattribute types.
See this should have a question, this trouble, why use this metamodel? What's the benefit?
The benefits are certain, after all, what the standard JPA defines. I checked this online, the benefits are many:
Well, I'll find out for the time being.
Criteria Query
To better understand the criteria query, consider the Dept entity that owns the employee instance collection, and the code for the meta-model class for employee and dept is as follows:
All necessary Imports@staticmetamodel (Dept.class)PublicClass Dept_ {PublicStaticVolatile singularattribute<dept, integer> ID;PublicStaticVolatile listattribute<dept, employee> employeecollection;PublicStaticvolatile singularattribute<dept, string> name;} //all necessary Imports@staticmetamodel (employee.class) Public class employee_ {public Static volatile Singularattribute<employee, integer> ID; public static volatile Singularattribute<employee, integer> age; public static volatile Singularattribute<employee, string> name; public static volatile Singularattribute<employee, dept> DeptID;}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
The following code snippet shows a criteria query that is used to get all employees older than 24 years:
Criteriabuilder Criteriabuilder = em. Getcriteriabuilder (); criteriaquery<employee> criteriaquery = Criteriabuilder.createquery (Employee.class) .from (Employee.class" ; predicate condition = Criteriabuilder.gt (Employee . Get (Employee_.age), 24) .where (condition) .createquery (criteriaQuery) ; list<employee> result = Typedquery.getresultlist ()
Corresponding Sql:select * from employee WHERE age >
Criteriabuilder Secure Query Creation factory
Criteriabuilder security queries Create factories, create criteriaquery, create queries specific to specific conditions predicate , and so on.
Criteriabuilder is the beginning of a factory object, security query. Used to build JPA security queries. You can get criteriabuilder from the Entitymanager or Entitymanagerfactory class.
Like what:
CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
Criteriaquery Security Query Subject sentence
- It is obtained by calling Criteriabuilder, CreateQuery or Criteriabuilder.createtuplequery.
- Criteriabuilder is like a factory in Criteriaquery.
- The Criteriaquery object must function on the criteria query on the entity type or on the embedded type.
- The Criteriaquery object for the employee entity is created in the following way:
CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();CriteriaQuery<Employee> criteriaQuery = criteriaBuilder.createQuery(Employee.class);
Root
- Root defines the types that can appear in the FROM clause of a query
- The query root of the criteria query defines the entity type, which will get the desired result for future navigation, similar to the FROM clause in the SQL query.
- The root instance is also typed and defines the types that can appear in the FROM clause of the query.
- The query root instance can be obtained by passing in an entity type to the Abstractquery.from method.
- Criteria query, you can have multiple query roots.
- The query root object for an employee entity can be obtained using the following syntax:
Root<Employee> employee = criteriaQuery.from(Employee.class);
predicate filter conditions
- The filter condition is applied to the FROM clause of the SQL statement.
- In the criteria query, the query condition is applied to the Criteriaquery object through a predicate or expression instance.
- These conditions are applied to the Criteriaquery object using the Criteriaquery. Where method.
- predicate instances can also be obtained with the IsNull, Isnotnull, and in methods of the expression instance, and compound predicate statements can be constructed using the Criteriabuilder and, or Andnot methods.
- Criteriabuilder is also a factory that acts as an predicate instance, predicate objects by invoking the conditional method of Criteriabuilder (equal,notequal, GT, Ge,lt, Le,between, Like, etc.) to create.
- These conditions are applied to the Criteriaquery object using the Criteriaquery. Where method.
- The following code snippet shows an example of an predicate instance that checks for an employee older than 24 years:
Predicate condition = criteriaBuilder.gt(employee.get(Employee_.age), 24);criteriaQuery.where(condition);
The Employee_ meta-model class age attribute is called a path expression. If the age property is compared to string literals, the compiler throws an error, which is not possible in JPQL. is this the role of the Metamodel??
Predicate[] Multiple filter conditions
List<Predicate> predicatesList = new ArrayList<Predicate>();predicatesList.add(.....Pridicate....)criteriaQuery.where(predicatesList.toArray(new Predicate[predicatesList.size()]));
or statement (scary)
Predicateslist. Add (Criteriabuilder. Criteriabuilderequal (Root. Get (Repairorder_. Localrepairstatus), Localrepairstatus. Repairing), Criteriabuilder. Equal (Root. Get (Repairorder_. Localrepairstatus), Localrepairstatus. Diagnos ));
Ignore case (all caps)
Predicateslist. Add (Criteriabuilder. Criteriabuilder. Upper (Root. Get (Repairshop_. shopname)), StringUtils . Uppercase (StringUtilstrim (this. shopname)) + "%"));
Quote Original: http://blog.csdn.net/id_kong/article/details/70225032
Blog is to remember that they are easy to forget things, but also a summary of their work, the article can be reproduced, without copyright. Hope to do their own efforts to do better, we work together to improve!
If there is any problem, welcome to discuss together, code if there is a problem, you are welcome to the great God!
Getting Started with Java-jpa-criteriabuilder