JPA Query Language-simple jpql Query

Source: Internet
Author: User

Jpql: Java persistent Query Language, which constructs query statements with object-oriented query syntax.

JPA uses the javax. Persistence. query interface to represent a query instance. The query instance is constructed by entitymanager using the specified query statement.

BelowProgramEm in is an instance of entitymanager. You can use injection or the entitymanagerfactory instance to explicitly obtain an entitymanager instance.

1. A basic query:

Select u from user u to retrieve all users. U is the alias of user. Note: keywords are case-insensitive. For example, select and select are the same, but Entity Names and object fields are case-sensitive. For example, user and user are different.

String jpql = "select u from user U"; // U is the user's alias list <user> Users = em. createquery (jpql ). getresultlist (); // list <user> Users = em. createquery (jpql, user. class ). getresultlist (); // use user. class specifies the returned class

2. query using location parameters:

Select u from user u where u. Name =? 1. Search for the user with the specified name as the parameter. The format of the location parameter is :? + Location number, for example :? 1 and? 2. Note :? There cannot be a space between it and the location number. Cannot it be written? 1. The position number can be 0 or a positive integer. Question mark? Cannot be written as a Chinese Input Method ?.

 
String jpql = "select u from user u where u. Name =? 1 "; query = em. createquery (jpql); query. setparameter (1, "Ye kai"); // set the value list for the parameter numbered 1 <user> Users = query. getresultlist ();

3. query by name parameters:

Select u from user u where u. Name =: Name: the user whose name is specified in the search parameter. The name parameter format is: + parameter name, for example: Name and: ID. Note: there must be no space between the parameter name and it cannot be written as: Name. Colon: cannot be written as a Chinese input method :.

 
String jpql = "select u from user u where u. name =: Name "; query = em. createquery (jpql); query. setparameter ("name", "Ye kai"); // set the value list for the parameter "name" <user> Users = query. getresultlist ();

In the same query statement, you cannot use both location and name parameters. You can only use one of them.

 

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.