818 Spring data JPA some interfaces and methods

Source: Internet
Author: User
Tags findone list of attributes


Spring JPA is divided into three main classes:
Org.springframework.data.jpa.repository.jparepository<t, id>
Org.springframework.data.jpa.repository.jpaspecificationexecutor<t>
Org.springframework.data.repository.crudrepository<t, id>
The implementation classes for these three classes are
Org.springframework.data.jpa.repository.support.simplejparepository<t, id> (this kind of awesome)
which
Jparepository is mainly findall findpage FindOne such a method
Jpaspecificationexecutor is mainly FindAll (spec) findpage (spec) FindOne (spec) method
spec--> query condition, need interface org.springframework.data.jpa.domain.specification<t> this interface
and achieve
Org.springframework.data.jpa.domain.Specification.toPredicate (ROOT&LT;T&GT;, Criteriaquery<?>, Criteriabuilder)
There are three parameters in this interface:
Root
Criteriaquery
Criteriabuilder
Where Root is an entity object of the query result, which is the main object returned by the query result, where one pair or many pairs is calculated from this object, the specific hierarchical relationship
Javax.persistence.tupleelement<x>
Javax.persistence.criteria.selection<x>
Javax.persistence.criteria.expression<t>
Javax.persistence.criteria.path<x>
Javax.persistence.criteria.from<z, x>
Javax.persistence.criteria.root<x>
These interfaces are mainly used to describe the correspondence between a database object and an entity object, and that is not much.
And then the JAVAX.PERSISTENCE.CRITERIA.CRITERIAQUERY&LT;T&GT, this is the JPA standard, mainly constructs the query condition, inside the method is all sorts of query way: DISTINCT, select, where, GroupBy, having, and the way to do these methods, presumably everyone knows that these are the basic methods of component SQL statements.
Next is Javax.persistence.criteria.CriteriaBuilder, which is primarily used for some function operations, but here we focus on two implementations of hibernate in the JPA standard:
1.and Org.hibernate.ejb.criteria.CriteriaBuilderImpl.and (predicate ...)
2.or org.hibernate.ejb.criteria.CriteriaBuilderImpl.or (predicate ...)
Both of these methods have a key interface: predicate (javax.persistence.criteria.Predicate);
This interface, the same as expression (javax.persistence.criteria.expression<boolean>) Sub-interface, can certainly also be a field-related expression, in the actual operation, This interface is also used as the core operating interface of various predicate, and the method is to join each condition as an and to make queries, or to splice each condition as or to query.
Then say how to generate a predicate interface, The core class generated is also the above Criteriabuilder (Javax.persistence.criteria.CriteriaBuilder), where many methods are returned to the predicate interface, which contains between, GT (greater than), LT (less than), not (non), and so on, put the required query conditions in an array (or with Java.util.ArrayList.toArray (t[])-->cb.and ( Predicatelist.toarray (New Predicate[predicates.size ()))), and then call the previous step and or or method to stitch, of course, if a query has both and and OR, In this way has not been specifically tried, remains to be verified.
Having said so much, first summarize
Specification's Topredicate This method declares the
Root: Which table to query
Criteriaquery: What fields are queried and what sort is
Criteriabuilder: What is the relationship between fields, how to generate a query condition, and how each query condition works
predicate (Expression): A detailed description of each individual query condition
These are the use of conditional queries, the perfect combination of SPRINGJPA and hibernate (if you only use SPRINGJPA, you don't know here)
Let's take a simple example:
/** Query name contains Icarus, or the phone number contains 188 of the user (if the return or to and, is the query name contains Icarus, and the phone number contains 188 users) */
query = Cb.createquery (user.class);//query is so out of the
list<predicate> predicatelist = new arraylist<> ();
Root = Query.from (User.class);
path<string> nameexp = root.get ("name");
predicate P1 = Cb.like (nameexp, "%icarus%");
path<string> phoneexp = root.get ("Phone");
predicate P2 = cb.like (phoneexp, "%188%");
Predicatelist.add (p1);
Predicatelist.add (p2);
Return Cb.or (Predicatelist.toarray (New Predicate[predicates.size ()));
If you want to refine an abstract class, you need to abstract the method of returning predicatelist, but you won't be able to provide either or and and dual options at this time.
Of course, judging by other parameters is also possible, but not very advantageous.
If you want to pass judgment, it is recommended to predicate the enumeration values and and or in this class
In Criteriabuilder, there are ASC, desc two methods, return order (here order in Criteriaquery, and the following Pagerequest said order, sort), and sum, count, AVG, Max, Min These methods, the aggregate function in theory is the return value will appear, here, for the moment really do not know what use, but certainly useful, but also look at the expert pointing.
The above is about the way SPRINGJPA is used when making conditional queries. Can not be said simple, because the method does not directly write the advantages of SQL, good is the cross-platform, I believe that hibernate database execution is very force
P.S. This design pattern in the past to do a Japanese project encountered, each query conditions are used a method of accumulation of means, on the one hand is the method to increase the query conditions, no tube implementation, can be used in different databases, for the Java development of programmers more friendly; Cumbersome and inefficient, these are beyond doubt, after all, it takes a long transition from HQL to SQL.
Not yet, go on.
Just said the conditional query in the specification, contains quite a lot of content, and then the query results related to the content.
The two methods of FindAll and FindAll (spec) are relatively simple, and the return object is list<t>
where T is specified in FindAll is the first generic object in the Jparepository,
The first generic object in Jpaspecificationexecutor is specified in FindAll (spec).
That is, the same entity class as the database (spec should also be corresponding to root, because specification also needs generics, it should be specified when writing specification)
FindOne and FindOne (spec) return objects are t,t also with FindAll
In addition to saying findall (sort) and FindAll (pageable), the return result is org.springframework.data.domain.page<t>
P.s. (Because FindAll (Spec,sort) and FindAll (spec,pageable) and the two differences are not very large, it is not the additional, understand it yourself)
Come out here again. Two parameters: Sort and pageable
Sort: The essence is a set of order (this order is not the order interface generated by ASC and DESC in the above criteriabuilder, but rather an internal public class of sort (which is understood to be a normal class), is an implementation class), The build of sort can use an order array, an order list, an array of attributes, and a list of attributes. The advantage of using order is that you can customize the sort direction (order is made up of attributes and orientations), and if you take attributes, the ASC ascending order is used by default
We're done with sort, we'll talk about pageable.
Pageable refers to the org.springframework.data.domain.Pageable, the interface has 4 methods,
Getpagenumber--> Get page numbers
GetPageSize--Paging size
GetOffset---offset
Getsort--Get sort information
For the generation of pageable, it is also very easy to use org.springframework.data.domain.PageRequest This implementation class, the construction method can be generated, there is no second implementation class. Set the sort, pagenumber, getpagesize.
The offset does not allow setting, which is automatically set at query time by calculating the page number and paging size.
For the method return value: Page, the implementation of the class org.springframework.data.domain.pageimpl<t> is also very simple, just on the basis of the list to add a total number of pages, the total size of a series of commonly used content, see the code can
The following is the official spring documentation for JPA, which is a good English language to read.
http://docs.spring.io/spring-data/jpa/docs/1.8.0.RELEASE/reference/html/
But most of the text is about application direction.

818 Spring data JPA some interfaces and methods

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.