In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
How to conduct a simple query of JPQL in JPA, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can get something.
JPQL:Java persists the query language and constructs query statements with object-oriented query syntax.
JPA uses the javax.persistence.Query interface to represent a query instance, and the Query instance is built by EntityManager through the specified query statement.
The em in the following program is an instance of EntityManager, using injection or explicitly obtaining an EntityManager instance through an EntityManagerFactory instance.
1. A basic query:
SELECT u FROM User u retrieves all User. U is an alias for User. Note: keywords are not case-sensitive, for example, SELECT and select are the same, but entity names and entity fields are case-sensitive, for example, User and user are not the same.
String jpql = "SELECT u FROM User u"; / / u is the alias of User List users = em.createQuery (jpql). GetResultList (); / / List users = em.createQuery (jpql, User.class). GetResultList (); / / specify the returned class with User.class
2. Use location parameters to query:
SELECT u FROM User u WHERE u.name =? 1 retrieves the User of the specified name. The location parameter format is:? + location number, for example:? 1 and? 2. Note:? There can be no space between the position number and the position number, it cannot be written as? 1, and the position number can be 0 or a positive integer. Question mark? Cannot be written in the state of Chinese input method?
String jpql = "SELECT u FROM User u WHERE u.name =? 1"; Query query = em.createQuery (jpql); query.setParameter (1, "leaf open"); / / set the value List users = query.getResultList () to the parameter numbered 1.
3. Use name parameters to query:
SELECT u FROM User u WHERE u.name =: the name retrieval parameter specifies the User of the name. The name parameter format is:: + parameter name, such as:: name and: id. Note: there can be no spaces between: and the parameter name and cannot be written as: name. Colon: can not be written in Chinese input method state:
String jpql = "SELECT u FROM User u WHERE u.name =: name"; Query query = em.createQuery (jpql); query.setParameter ("name", "Ye Kai"); / / set the value List users = query.getResultList () to the parameter "name"
You cannot use both location and name parameters in the same query statement, only one of them.
Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.