使用Spring数据JPA中的规范进行内部连接和分组
我正在尝试获取EmployType为clerk且加入日期为最近日期的员工详细信息 SQL Server 2008中的查询如下所示:使用Spring数据JPA中的规范进行内部连接和分组,spring,spring-data,spring-data-jpa,criteria,querydsl,Spring,Spring Data,Spring Data Jpa,Criteria,Querydsl,我正在尝试获取EmployType为clerk且加入日期为最近日期的员工详细信息 SQL Server 2008中的查询如下所示: select * from employee jj inner join ( select max(join_date) as jdate, empltype as emptype from employee where empltype='clerk'
select
*
from
employee jj
inner join
(
select
max(join_date) as jdate,
empltype as emptype
from
employee
where
empltype='clerk'
group by empltype
) mm
on jj.join_date=mm.jdate and jj.empltype=mm.emptype;
我使用SpringData JPA作为持久层,使用QueryDSL、规范和谓词来获取数据
我试图在QueryDSL或规范中转换上述查询,但无法正确地钩住它们
员工实体:
int seqid;(sequence id)
String empltype:
Date joindate;
String role;
Predicate toPredicate(Root<employee> root,CriteriaQuery <?> query,CriteriaBuilder cb)
{
Predicate pred=null;
// Returning the rows matching the joining date
pred=cb.equal(root<Emplyoee_>.get("joindate"));
//**//
}
指定类中的谓词方法:
int seqid;(sequence id)
String empltype:
Date joindate;
String role;
Predicate toPredicate(Root<employee> root,CriteriaQuery <?> query,CriteriaBuilder cb)
{
Predicate pred=null;
// Returning the rows matching the joining date
pred=cb.equal(root<Emplyoee_>.get("joindate"));
//**//
}
谓词toPredicate(根根、CriteriaQuery查询、CriteriaBuilder cb)
{
谓词pred=null;
//返回与加入日期匹配的行
pred=cb.equal(root.get(“joindate”);
//**//
}
要将about SQL查询转换为JPA谓词,应使用//**/编写哪段代码。任何其他返回页面的Spring数据JPA impl,比如@Query、NamedQuery或QueryDSL也适用于我
提前感谢我在记事本上写了这篇文章,它还没有经过测试,但我想你正在寻找类似的东西
QEmployee e1 = new QEmployee("e1");
QEmployee e2 = new QEmployee("e2");
PathBuilder<Object[]> eAlias = new PathBuilder<Object[]>(Object[].class, "eAlias");
JPASubQuery subQuery = JPASubQuery().from(e2)
.groupBy(e2.empltype)
.where(e2.empltype.eq('clerk'))
.list(e2.join_date.max().as("jdate"), e2.emptype)
jpaQuery.from(e1)
.innerJoin(subQuery, eAlias)
.on(e1.join_date.eq(eAlias.get("jdate")), e1.emptype.eq(eAlias.get("emptype")))
.list(qEmployee);
QEmployee e1=新的QEmployee(“e1”);
QEmployee e2=新QEmployee(“e2”);
PathBuilder eAlias=新的PathBuilder(对象[]。类,“eAlias”);
JPASubQuery子查询=JPASubQuery()。来自(e2)
.groupBy(e2.emplype)
.where(e2.emplype.eq('clerk'))
.list(e2.join_date.max().as(“jdate”),e2.emptype)
jpaQuery.from(e1)
.innerJoin(子查询,eAlias)
.on(e1.join_date.eq(eAlias.get(“jdate”)),e1.emptype.eq(eAlias.get(“emptype”))
.名单(qEmployee);
一个员工FindTopByEmployTypeOrderByJoinDateDesc(字符串employeeType)
就足够了吗?