Keyword for "JPA" Expression criteria query

Source: Internet
Author: User
Tags naming convention

1. Create a query by parsing the method name

When parsing a method name, the framework first intercepts the extra prefix of the method name, such as Find, FindBy, read, Readby, get, Getby, and then parses the rest. And if the last parameter of the method is a sort or pageable type, the relevant information is also extracted for sorting by the rules or paging the query.

When creating a query, we express it by using the property name in the method name, such as Findbyuseraddresszip (). When parsing the method, the framework first rejects the findBy, then parses the rest of the attributes, with detailed rules as follows (this assumes that the method is for the domain object AccountInfo type):

    • First Judge Useraddresszip (according to the POJO specification, the first letter to lowercase, the same as the same) is a property of AccountInfo, if so, the query based on the property, if not, continue the second step;
    • A string that starts with the first capital letter from right to left (ZIP), and then checks whether the remaining string is a property of AccountInfo, or, if it is, a query based on that property, and if not, repeats the second step, continuing from right to left, and finally assuming that the user is A property of the AccountInfo;
    • Then processing the remainder (addresszip), first determine whether the user's corresponding type has a addresszip attribute, if any, then the method is ultimately based on "AccountInfo.user.addressZip" the value of the query; otherwise continue to follow the steps The 2 rule is truncated from right to left, and ultimately indicates a query based on the value of "AccountInfo.user.address.zip".

When querying, it is often necessary to query on multiple properties at once, and the conditions of the query are various (greater than a certain value, in a range, etc.), and Spring Data JPA provides some keywords for expressing the criteria query, roughly as follows:

  • and---are equivalent to the AND keyword in SQL, such as Findbyusernameandpassword (String user, Striang pwd);
  • The or---is equivalent to the OR keyword in SQL, such as findbyusernameoraddress (string user, String addr);
  • The between---is equivalent to the between keyword in SQL, such as Findbysalarybetween (int max, int min);
  • The LessThan---is equivalent to "<" in SQL, such as Findbysalarylessthan (int max);
  • GreaterThan---is equivalent to ">" in SQL, such as Findbysalarygreaterthan (int min);
  • IsNull---is equivalent to "is null" in SQL, such as Findbyusernameisnull ();
  • Isnotnull---is equivalent to "is not NULL" in SQL, such as Findbyusernameisnotnull ();
  • Notnull---and isnotnull equivalence;
  • The like---is equivalent to the "as" in SQL, such as Findbyusernamelike (String user);
  • The notlike---is equivalent to "not like" in SQL, such as Findbyusernamenotlike (String user);
  • The order-by---is equivalent to the "FINDBYUSERNAMEORDERBYSALARYASC" in SQL, such as the String user;
  • The not---is equivalent to "in SQL"! = ", such as Findbyusernamenot (String user);
  • In---is equivalent to ' in ' in SQL, such as Findbyusernamein (Collection<string> userlist), the parameter of the method can be either a Collection type or an array or an indefinite length parameter;
  • The notin---is equivalent to "not in" SQL, such as Findbyusernamenotin (Collection<string> userlist), which can be either a Collection type or an array or not Fixed length parameters;

2. Create a query using @Query

The use of @Query annotations is simple, just annotate the annotation on the declared method, and provide a JP QL query statement as follows:

Public interface Userdao extends Repository<accountinfo, long> {  @Query ("Select a from AccountInfo a where A.ACC Ountid =? 1 ") Public  accountinfo Findbyaccountid (Long accountId);     @Query ("Select a from AccountInfo a where a.balance > 1") Public  page<accountinfo> Findbybalancegreaterthan (  Integer balance,pageable pageable);  

Many developers prefer to use named parameters instead of location numbers when creating JP QL, @Query also support this. In the JP QL statement, the parameter is specified in the format ": variable", and the method parameter corresponds to the named parameter in JP QL by using @Param in front of the parameter of the method, as shown in the following example:

Public interface Userdao extends Repository<accountinfo, long> {public  accountinfo save (accountinfo AccountInfo);  @Query ("From AccountInfo a WHERE A.accountid =: id") Public  AccountInfo Findbyaccountid (@Param ("id") Long accountId) ;    @Query ("From AccountInfo a where a.balance >: Balance") Public    page<accountinfo> Findbybalancegreaterthan (  @Param ("balance") Integer balance,pageable pageable);  

In addition, developers can use the @Query to perform an update operation, we need to use the @Query while using the @Modifying to identify the action as a modified query, so that the framework will eventually generate an updated operation, not a query. As shown below:

@Modifying  @Query ("Update AccountInfo a set a.salary =? 1 where A.salary <? 2")  

3. Create a query by invoking a JPA named query statement

A named query is a feature that JPA provides to separate query statements from the body of a method for common use by multiple methods. Spring Data JPA also provides good support for named queries. A user simply needs to define a query statement in the Orm.xml file or in code using the @NamedQuery (or @NamedNativeQuery) in accordance with the JPA specification, and the only thing to do is to give the statement a name that satisfies the " The naming convention for Domainclass.methodname () ". Assume that the following interfaces are defined:

Public interface Userdao extends Repository<accountinfo, long> {  ...  Public list<accountinfo> findTop5 ();  

If you want to create a named query for FINDTOP5 () and associate it with it, we only need to define the named query statement in the appropriate location and name it "ACCOUNTINFO.FINDTOP5", and the framework, when it resolves to the method during the creation of the proxy class, first looks for the name " ACCOUNTINFO.FINDTOP5 named query definition, if not found, attempts to parse the method name and create a query based on the method name.

Reference http://www.cnblogs.com/WangJinYang/p/4257383.html

Keyword for "JPA" Expression criteria query

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.