JPA中JPQL的子查询
@Entity
public class Employee {
private Integer empId;
private String empAddress;
private Integer empAge;
private String empGender;
private String empName;
private Double empSalary;
private Date empStartDate;
private Integer empStatus;
// private String deptId;
private Department dept;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
public Integer getEmpId() {
return empId;
}
public void setEmpId(Integer empId) {
this.empId = empId;
}
public String getEmpAddress() {
return empAddress;
}
public void setEmpAddress(String empAddress) {
this.empAddress = empAddress;
}
public Integer getEmpAge() {
return empAge;
}
public void setEmpAge(Integer empAge) {
this.empAge = empAge;
}
public String getEmpGender() {
return empGender;
}
public void setEmpGender(String empGender) {
this.empGender = empGender;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public Double getEmpSalary() {
return empSalary;
}
public void setEmpSalary(Double empSalary) {
this.empSalary = empSalary;
}
public Date getEmpStartDate() {
return empStartDate;
}
public void setEmpStartDate(Date empStartDate) {
this.empStartDate = empStartDate;
}
public Integer getEmpStatus() {
return empStatus;
}
public void setEmpStatus(Integer empStatus) {
this.empStatus = empStatus;
}
@ManyToOne
@JoinColumn(name="deptId")
public Department getDept() {
return dept;
}
public void setDept(Department dept) {
this.dept = dept;
}
@Override
public String toString() {
return "Employee [empId=" + empId + ", empAddress=" + empAddress + ", empAge=" + empAge + ", empGender="
+ empGender + ", empName=" + empName + ", empSalary=" + empSalary + ", empStartDate=" + empStartDate
+ ", empStatus=" + empStatus + "]";
}
public Employee(Integer empId, String empAddress, Integer empAge, String empGender, String empName,
Double empSalary, Date empStartDate, Integer empStatus) {
super();
this.empId = empId;
this.empAddress = empAddress;
this.empAge = empAge;
this.empGender = empGender;
this.empName = empName;
this.empSalary = empSalary;
this.empStartDate = empStartDate;
this.empStatus = empStatus;
}
public Employee() {
}
public Employee(Integer empId, String empName, Double empSalary) {
super();
// System.out.println("调用了三个参数的有参构造方法");
this.empId = empId;
this.empName = empName;
this.empSalary = empSalary;
}
}
@Entity
public class Department {
private Integer deptId;
private String deptName;
private String deptManager;
private Set emps = new HashSet();
@OneToMany(mappedBy="dept",fetch=FetchType.LAZY)
// @JoinColumn(name="deptId")
public Set getEmps() {
return emps;
}
public void setEmps(Set emps) {
this.emps = emps;
}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public Integer getDeptId() {
return deptId;
}
public void setDeptId(Integer deptId) {
this.deptId = deptId;
}
public String getDeptName() {
return deptName;
}
public void setDeptName(String deptName) {
this.deptName = deptName;
}
public String getDeptManager() {
return deptManager;
}
public void setDeptManager(String deptManager) {
this.deptManager = deptManager;
}
@Override
public String toString() {
return "Department [deptId=" + deptId + ", deptName=" + deptName + ", deptManager=" + deptManager + "]";
}
public Department(Integer deptId, String deptName, String deptManager) {
super();
this.deptId = deptId;
this.deptName = deptName;
this.deptManager = deptManager;
}
public Department() {
super();
}
}
/**
* 测试子查询功能
*
* @author boge
*
*/
public class SubQueryTest {
EntityManagerFactory emf;
EntityManager em;
EntityTransaction transaction;
/* 查询不是李总管辖部门下的员工信息
* 外层查询:员工信息
* 内层查询:不是李总管辖部门下的
*
* 步骤:
* 1、查询不是李总管辖的部门=>dept(1,2)
* select d.deptId from Department where d.deptManger !="李总"
* 2、根据查到的部门,查询该部门下的员工信息
* select e from Employee where e.dept.deptId IN (1,2)
*/
@Test
public void test4(){
String jpql="select e from Employee e "
+ "where e.dept.deptId IN "
+ "(select d.deptId from Department d where d.deptManager !='李总')";
Query query = em.createQuery(jpql);
List resultList = query.getResultList();
for (Employee emp : resultList) {
System.out.println(emp.getEmpName()+":"+emp.getDept().getDeptId());
}
}
// 查询没有用户详情的用户信息
/* 查询年龄大于人事部所有员工的年龄的员工信息
* 外层查询:查询员工信息
* 内层查询:条件 该员工信息的年龄大于人事部所有员工的年龄
* 第一步:查询人事部的员工年龄 10 20 30 40 50
* select emp from Employee emp where emp.dept.deptId = 4
* 第二步:去参与到外层查询
* e.empAge > ALL(10 20 30 40 50)
*/
@Test
public void test2(){
String jpql = "select e from Employee e "
+ "where e.empAge > "
+ "ALL(select emp.empAge from Employee emp where emp.dept.deptId = 4)";
Query query = em.createQuery(jpql);
List resultList = query.getResultList();
for (Object object : resultList) {
System.out.println(object);
}
}
/**
* 查询员工信息,条件是该员工的年龄,大于4号人事部的任意一个员工的年龄就可以
* (10 20 30 40 50)
*/
// 查询年龄大于人事部任意一个员工的年龄的员工信息
@Test
public void test3(){
String jpql = "select e from Employee e "
+ "where e.empAge > "
+ "ANY(select emp.empAge from Employee emp where emp.dept.deptId = 4)";
Query query = em.createQuery(jpql);
List resultList = query.getResultList();
for (Object object : resultList) {
System.out.println(object);
}
}
/* 查询部门编号大于3的部门员工信息
* 查询的员工信息,条件:员工的部门编号大于3
*
*/
@Test
public void test1(){
String jpql = "SELECT e from Employee e where e.dept.deptId>2";
Query query = em.createQuery(jpql);
List resultList = query.getResultList();
for (Object object : resultList) {
System.out.println(object);
}
}
@Before
public void before() {
// 1、获取EntityMangerFactory
emf = Persistence.createEntityManagerFactory("jpa-03");
// 2、获取EntityManger
// 重点对象,所有增删改查操作,都是通过它的方法进行的
em = emf.createEntityManager();
transaction = em.getTransaction();
transaction.begin();
}
@After
public void after() {
transaction.commit();
em.close();
}
}
org.hibernate.ejb.HibernatePersistence
com.kmu.entity.Employee
com.kmu.entity.Department