In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article is about how to carry out the naming query of JPQL in JPA, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.
The named query of JPA is actually a name for the query statement, which is directly used when executing the query, which avoids repeating the JPQL statement and makes the query more reused in the code. I don't really like using named queries because I don't want to write queries in entities that make them look complex and bloated.
1. Use the @ NamedQuery annotation to define named queries in entity classes.
NamedQuery (name= "findAllUser", query= "SELECT u FROM User u")
The attribute name in @ NamedQuery specifies the name of the named query, and the query attribute specifies the statement of the named query.
If you want to define multiple named queries, you need to use @ NamedQueries.
@ NamedQueries ({
NamedQuery (name= "findAllUser", query= "SELECT u FROM User u")
@ NamedQuery (name= "findUserWithId", query= "SELECT u FROM User u WHERE u.id =? 1")
@ NamedQuery (name= "findUserWithName", query= "SELECT u FROM User u WHERE u.name =: name")
})
2. After defining a named query, you can use the createNamedQuery method of EntityManager to pass in the name of the named query to create the query.
For example: createNamedQuery ("findAllUser")
3. A simple example.
A simple User entity:
Package com.cndatacom.jpa.entity; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.NamedQueries; import javax.persistence.NamedQuery; import javax.persistence.Table Entity @ Table (name= "t_user") @ NamedQueries ({@ NamedQuery (name= "findAllUser", query= "SELECT u FROM User u"), @ NamedQuery (name= "findUserWithId", query= "SELECT u FROM User u WHERE u.id =? 1"), @ NamedQuery (name= "findUserWithName") Query= "SELECT u FROM User u WHERE u.name =: name")}) public class User {/ * primary key * / @ Id @ GeneratedValue private Long id / * name * / @ Column (name= "name") private String name; / * password * / @ Column (name= "password") private String password; public Long getId () {return id;} public void setId (Long id) {this.id = id } public String getName () {return name;} public void setName (String name) {this.name = name;} public String getPassword () {return password;} public void setPassword (String password) {this.password = password;}}
A simple test:
Package com.cndatacom.jpa.test; import java.util.List; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.Persistence; import javax.persistence.Query; import org.junit.After; import org.junit.Before; import org.junit.Test; import com.cndatacom.jpa.entity.User; public class TestNamedQuery {EntityManagerFactory emf = null @ Before public void before () {/ / create EntityManagerFactory emf = Persistence.createEntityManagerFactory ("myJPA") based on the persistence-unit name configured in persistence.xml;} @ After public void after () {/ / close EntityManagerFactory if (null! = emf) {emf.close () } @ Test public void testNamedQuery1 () {EntityManager em = emf.createEntityManager (); List users = em.createNamedQuery ("findAllUser"). GetResultList (); / / according to the named query defined in the User entity} @ Test public void testNamedQuery2 () {EntityManager em = emf.createEntityManager (); Query query = em.createNamedQuery ("findUserWithId") / / based on the named query query.setParameter (1,2L) defined in the User entity; List users = query.getResultList ();} @ Test public void testNamedQuery3 () {EntityManager em = emf.createEntityManager (); Query query = em.createNamedQuery ("findUserWithName"); / / the named query query.setParameter ("name", "Li Bao") defined in the User entity List users = query.getResultList ();}} above is how to query the naming of JPQL in JPA. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
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.