JPA的命名查询实际上就是给查询语句起个名字,执行查询的时候就是直接使用起的这个名字,避免重复写JPQL语句,使查询在代码中得到更多的重用。我不怎么喜欢使用命名查询,因为我不想把查询语句写在实体中,使得实体看起来变得复杂臃肿。
1、使用@NamedQuery注解在实体类中定义命名查询。
@NamedQuery(name="findAllUser",query="SELECT u FROM User u")
@NamedQuery中的属性name指定命名查询的名称,query属性指定命名查询的语句。
如果要定义多个命名查询,需要使用@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、定义好命名查询后,可以使用EntityManager的createNamedQuery方法传入命名查询的名称创建查询。例如:createNamedQuery("findAllUser");
3、一个简单的例子。
简单的User实体:
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 {
/**
* 主键
*/
@Id
@GeneratedValue
private Long id;
/**
* 名字
*/
@Column(name="name")
private String name;
/**
* 密码
*/
@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;
}
}
简单的测试:
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() {
//根据在persistence.xml中配置的persistence-unit name 创建EntityManagerFactory
emf = Persistence.createEntityManagerFactory("myJPA");
}
@After
public void after() {
//关闭EntityManagerFactory
if(null != emf) {
emf.close();
}
}
@Test
public void testNamedQuery1() {
EntityManager em = emf.createEntityManager();
List<User> users = em.createNamedQuery("findAllUser").getResultList();//根据User实体中定义的命名查询
}
@Test
public void testNamedQuery2() {
EntityManager em = emf.createEntityManager();
Query query = em.createNamedQuery("findUserWithId");//根据User实体中定义的命名查询
query.setParameter(1, 2L);
List<User> users = query.getResultList();[size=medium][/size]
}
@Test
public void testNamedQuery3() {
EntityManager em = emf.createEntityManager();
Query query = em.createNamedQuery("findUserWithName");//根据User实体中定义的命名查询
query.setParameter("name", "李坏");
List<User> users = query.getResultList();
}
}
1、使用@NamedQuery注解在实体类中定义命名查询。
@NamedQuery(name="findAllUser",query="SELECT u FROM User u")
@NamedQuery中的属性name指定命名查询的名称,query属性指定命名查询的语句。
如果要定义多个命名查询,需要使用@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、定义好命名查询后,可以使用EntityManager的createNamedQuery方法传入命名查询的名称创建查询。例如:createNamedQuery("findAllUser");
3、一个简单的例子。
简单的User实体:
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 {
/**
* 主键
*/
@Id
@GeneratedValue
private Long id;
/**
* 名字
*/
@Column(name="name")
private String name;
/**
* 密码
*/
@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;
}
}
简单的测试:
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() {
//根据在persistence.xml中配置的persistence-unit name 创建EntityManagerFactory
emf = Persistence.createEntityManagerFactory("myJPA");
}
@After
public void after() {
//关闭EntityManagerFactory
if(null != emf) {
emf.close();
}
}
@Test
public void testNamedQuery1() {
EntityManager em = emf.createEntityManager();
List<User> users = em.createNamedQuery("findAllUser").getResultList();//根据User实体中定义的命名查询
}
@Test
public void testNamedQuery2() {
EntityManager em = emf.createEntityManager();
Query query = em.createNamedQuery("findUserWithId");//根据User实体中定义的命名查询
query.setParameter(1, 2L);
List<User> users = query.getResultList();[size=medium][/size]
}
@Test
public void testNamedQuery3() {
EntityManager em = emf.createEntityManager();
Query query = em.createNamedQuery("findUserWithName");//根据User实体中定义的命名查询
query.setParameter("name", "李坏");
List<User> users = query.getResultList();
}
}