JPA,Querydsl4.X강좌
– 서브쿼리, JPA__EXPRESSION__s
서브 쿼리
Sub Query는 JPA__EXPRESSION__s를 만들어서 사용한다.
public List<Emp> selectEmpMaxSal() {
QEmp e = new QEmp("e");
List<Emp> emps = queryFactory.selectFrom(emp)
.where(emp.sal.eq(
JPA__EXPRESSION__s.select(e.sal.max()).from(e)))
.fetch();
return emps;
}
public List<Emp> selectEmpMaxSalOfDept() {
QEmp e = new QEmp("e");
List<Emp> emps = queryFactory.selectFrom(emp)
.where(emp.sal.eq(
JPA__EXPRESSION__s
.select(e.sal.max()).from(e) .where(emp.dept.deptno.eq(e.dept.deptno))
))
.fetch();
return emps;
}
public List<Emp> selectEmpGreaterThanAvgSal() {
QEmp e = new QEmp("e");
List<Emp> emps = queryFactory.selectFrom(emp)
.where(emp.sal.gt(
JPA__EXPRESSION__s
.select(e.sal.avg()).from(e) .where(emp.dept.deptno.eq(e.dept.deptno))
))
.fetch();
return emps;
}
public List<Emp> selectEmpEqualsEmpno(Long empno) {
QEmp e = new QEmp("e");
List<Emp> emps = queryFactory.selectFrom(emp)
.where(emp.sal.eq(
JPA__EXPRESSION__s
.select(e.sal).from(e) .where(e.empno.eq(empno))
))
.where(emp.empno.ne(empno))
.fetch();
return emps;
}
public List<Emp> selectEmpMaxSalTop3() {
List<Emp> emps = queryFactory.selectFrom(emp)
.orderBy(emp.sal.desc())
.limit(3)
.fetch();
return emps;
}
public List<String> selectDeptExistsEmp() {
List<String> depts = queryFactory.select(dept.dname).from(dept)
.where(JPA__EXPRESSION__s
.selectFrom(emp)
.where(emp.dept.deptno.eq(dept.deptno)).exists()
)
.fetch();
return depts;
}
서브 쿼리
Sub Query는 JPA__EXPRESSION__s를 만들어서 사용한다.
public List<Emp> selectEmpMaxSal() {
QEmp e = new QEmp("e");
List<Emp> emps = queryFactory.selectFrom(emp)
.where(emp.sal.eq(
JPA__EXPRESSION__s.select(e.sal.max()).from(e)))
.fetch();
return emps;
}
public List<Emp> selectEmpMaxSalOfDept() {
QEmp e = new QEmp("e");
List<Emp> emps = queryFactory.selectFrom(emp)
.where(emp.sal.eq(
JPA__EXPRESSION__s
.select(e.sal.max()).from(e) .where(emp.dept.deptno.eq(e.dept.deptno))
))
.fetch();
return emps;
}
public List<Emp> selectEmpGreaterThanAvgSal() {
QEmp e = new QEmp("e");
List<Emp> emps = queryFactory.selectFrom(emp)
.where(emp.sal.gt(
JPA__EXPRESSION__s
.select(e.sal.avg()).from(e) .where(emp.dept.deptno.eq(e.dept.deptno))
))
.fetch();
return emps;
}
public List<Emp> selectEmpEqualsEmpno(Long empno) {
QEmp e = new QEmp("e");
List<Emp> emps = queryFactory.selectFrom(emp)
.where(emp.sal.eq(
JPA__EXPRESSION__s
.select(e.sal).from(e) .where(e.empno.eq(empno))
))
.where(emp.empno.ne(empno))
.fetch();
return emps;
}
public List<Emp> selectEmpMaxSalTop3() {
List<Emp> emps = queryFactory.selectFrom(emp)
.orderBy(emp.sal.desc())
.limit(3)
.fetch();
return emps;
}
public List<String> selectDeptExistsEmp() {
List<String> depts = queryFactory.select(dept.dname).from(dept)
.where(JPA__EXPRESSION__s
.selectFrom(emp)
.where(emp.dept.deptno.eq(dept.deptno)).exists()
)
.fetch();
return depts;
}
댓글 없음:
댓글 쓰기