Jpa 如何使用QueryDsl组织此查询
我正在尝试使用QueryDsl调用SQL语句,Spring数据JPA无法涵盖该语句。 这是我想说的句子Jpa 如何使用QueryDsl组织此查询,jpa,querydsl,Jpa,Querydsl,我正在尝试使用QueryDsl调用SQL语句,Spring数据JPA无法涵盖该语句。 这是我想说的句子 SELECT timestamp, deviceId FROM EventData a INNER JOIN (SELECT max(timestamp) timestamp, deviceId FROM EventData GROUP BY deviceId) b USING (timestamp,deviceId) WHERE deviceId in ('1', '2'
SELECT timestamp, deviceId
FROM EventData a
INNER JOIN
(SELECT max(timestamp) timestamp, deviceId
FROM EventData
GROUP BY deviceId) b
USING (timestamp,deviceId)
WHERE deviceId in ('1', '2')
我确实喜欢以下内容:
@PersistenceContext
EntityManager em;
JPAQuery query = new JPAQuery(em);
QEventData eventData = new QEventData();
return
query.from(eventData)
.innerJoin(
query.from(eventData)
.groupBy(eventData.deviceId)
.list(eventData.deviceId, eventData.timestamp.max())
).on(eventData.deviceId.eq(?))
.where(eventData.deviceId.in("1", "2"))
.list(eventData);
我不知道我为innerJoin放了什么。我试图查询SL示例,但找不到正确的示例。哪里可以找到QueryDsl的好例子
谢谢,您的回答将不胜感激。这只是一个部分答案,但它在JUnit测试中对我有效。我使用的是SQL查询,而不是JPA。值得一试:
Connection connection = getConnection();
SQLTemplates dialect = new PostgresTemplates(); // TODO: use appropriate db template
QEventData c = new QEventData("c");
QEventData c2 = new QEventData("c2");
// Use this to hold alias for inner select
PathBuilder<Object[]> c2alias = new PathBuilder<Object[]>(Object[].class, "c2alias");
SQLQuery query = new SQLQuery(connection , dialect);
return query.from(c).innerJoin(
new SQLSubQuery().from(c2)
.groupBy(c2.deviceid)
.list(c2.device, c2.timestamp.max().as("timestamp")), c2alias
).on(c.deviceid.eq(c2alias.get("deviceid")),
c.timestamp.eq(c2alias.get("timestamp")))
.where(c.deviceid.in(1, 2))
.list(c.deviceid, c.timestamp);
Connection-Connection=getConnection();
SQLTemplates方言=新PostgresTemplates();//TODO:使用适当的数据库模板
QEventData c=新的QEventData(“c”);
QEventData c2=新QEventData(“c2”);
//使用此选项可保留内部选择的别名
PathBuilder c2alias=新的PathBuilder(对象[].class,“c2alias”);
SQLQuery=新的SQLQuery(连接,方言);
返回query.from(c).innerJoin(
新建SQLSubQuery()。来自(c2)
.groupBy(c2.deviceid)
.list(c2.device,c2.timestamp.max().as(“timestamp”)),c2alias
).on(c.deviceid.eq(c2alias.get(“deviceid”)),
c、 timestamp.eq(c2alias.get(“timestamp”))
.其中(c.deviceid.in(1,2))
.列表(c.设备ID,c.时间戳);
注意1:在list()函数中,必须显式列出要选择的字段,否则会出现错误,我认为这是由于使用了PathBuilder内部选择别名所致
注2:其他帖子指出这可能不适用于JPA,只适用于SQL:
注3:在我编写的SQL中,我从未见过实际使用过USING子句,对于那些不知道它的人来说:
注释4:如果上面与QueRDSL/JPA不起作用,那么考虑重构查询使用一个存在子句。
< P>这只是一个部分答案,但它对JUnit测试起了作用。我使用的是SQL查询,而不是JPA。值得一试: Connection connection = getConnection();
SQLTemplates dialect = new PostgresTemplates(); // TODO: use appropriate db template
QEventData c = new QEventData("c");
QEventData c2 = new QEventData("c2");
// Use this to hold alias for inner select
PathBuilder<Object[]> c2alias = new PathBuilder<Object[]>(Object[].class, "c2alias");
SQLQuery query = new SQLQuery(connection , dialect);
return query.from(c).innerJoin(
new SQLSubQuery().from(c2)
.groupBy(c2.deviceid)
.list(c2.device, c2.timestamp.max().as("timestamp")), c2alias
).on(c.deviceid.eq(c2alias.get("deviceid")),
c.timestamp.eq(c2alias.get("timestamp")))
.where(c.deviceid.in(1, 2))
.list(c.deviceid, c.timestamp);
Connection-Connection=getConnection();
SQLTemplates方言=新PostgresTemplates();//TODO:使用适当的数据库模板
QEventData c=新的QEventData(“c”);
QEventData c2=新QEventData(“c2”);
//使用此选项可保留内部选择的别名
PathBuilder c2alias=新的PathBuilder(对象[].class,“c2alias”);
SQLQuery=新的SQLQuery(连接,方言);
返回query.from(c).innerJoin(
新建SQLSubQuery()。来自(c2)
.groupBy(c2.deviceid)
.list(c2.device,c2.timestamp.max().as(“timestamp”)),c2alias
).on(c.deviceid.eq(c2alias.get(“deviceid”)),
c、 timestamp.eq(c2alias.get(“timestamp”))
.其中(c.deviceid.in(1,2))
.列表(c.设备ID,c.时间戳);
注意1:在list()函数中,必须显式列出要选择的字段,否则会出现错误,我认为这是由于使用了PathBuilder内部选择别名所致
注2:其他帖子指出这可能不适用于JPA,只适用于SQL:
注3:在我编写的SQL中,我从未见过实际使用过USING子句,对于那些不知道它的人来说:
注释4:如果上面与QueRDSL/JPA不起作用,那么考虑重构查询使用一个存在子句。< /P>第一个查询的目的是什么?我在理解上有问题。第一个问题的目的是什么?我很难理解它。