如何在 Querydsl 中创建子查询
Posted
技术标签:
【中文标题】如何在 Querydsl 中创建子查询【英文标题】:How to create subquery in Querydsl 【发布时间】:2018-08-30 06:26:37 【问题描述】:我是 Querydsl 的新手。我必须将以下查询转换为 Querydsl。我尝试如下,但我没有得到结果。
谁能告诉我我在查询中遗漏了什么或做错了什么?
select * from room as room
where room.nroom_id not in(
select rdm.nroom_id from roomdepartmentmapping as rdm)
我试过这样
JPAQuery<Tuple> query = new JPAQuery<Tuple>(em);
query.from(room)
.where(room.nRoomId.notIn
(query.select(roomDepartmentMapping.nRoomId)
.from(roomDepartmentMapping)
)
);
控制台
at com.querydsl.core.support.SerializerBase.visit(SerializerBase.java:31) ~[querydsl-core-4.1.4.jar:na]
at com.querydsl.core.types.OperationImpl.accept(OperationImpl.java:83) ~[querydsl-core-4.1.4.jar:na]
at com.querydsl.core.support.SerializerBase.handle(SerializerBase.java:92) ~[querydsl-core-4.1.4.jar:na]
at com.querydsl.jpa.JPQLSerializer.serialize(JPQLSerializer.java:220) ~[querydsl-jpa-4.1.4.jar:na]
at com.querydsl.jpa.JPQLSerializer.visit(JPQLSerializer.java:358) ~[querydsl-jpa-4.1.4.jar:na]
at com.querydsl.jpa.JPQLSerializer.visit(JPQLSerializer.java:39) ~[querydsl-jpa-4.1.4.jar:na]
at com.querydsl.core.types.SubQueryExpressionImpl.accept(SubQueryExpressionImpl.java:57) ~[querydsl-core-4.1.4.jar:na]
at com.querydsl.core.support.SerializerBase.handle(SerializerBase.java:92) ~[querydsl-core-4.1.4.jar:na]
at com.querydsl.jpa.JPQLSerializer.visitOperation(JPQLSerializer.java:403) ~[querydsl-jpa-4.1.4.jar:na]
at com.querydsl.core.support.SerializerBase.visit(SerializerBase.java:231) ~[querydsl-core-4.1.4.jar:na]
at com.querydsl.core.support.SerializerBase.visit(SerializerBase.java:31) ~[querydsl-core-4.1.4.jar:na]
at com.querydsl.core.types.OperationImpl.accept(OperationImpl.java:83) ~[querydsl-core-4.1.4.jar:na]
at com.querydsl.core.support.SerializerBase.handle(SerializerBase.java:92) ~[querydsl-core-4.1.4.jar:na]
at com.querydsl.jpa.JPQLSerializer.serialize(JPQLSerializer.java:220) ~[querydsl-jpa-4.1.4.jar:na]
at com.querydsl.jpa.JPQLSerializer.visit(JPQLSerializer.java:358) ~[querydsl-jpa-4.1.4.jar:na]
at com.querydsl.jpa.JPQLSerializer.visit(JPQLSerializer.java:39) ~[querydsl-jpa-4.1.4.jar:na]
at com.querydsl.core.types.SubQueryExpressionImpl.accept(SubQueryExpressionImpl.java:57) ~[querydsl-core-4.1.4.jar:na]
at com.querydsl.core.support.SerializerBase.handle(SerializerBase.java:92) ~[querydsl-core-4.1.4.jar:na]
at com.querydsl.core.support.SerializerBase.visitOperation(SerializerBase.java:270) ~[querydsl-core-4.1.4.jar:na]
at com.querydsl.jpa.JPQLSerializer.visitOperation(JPQLSerializer.java:403) ~[querydsl-jpa-4.1.4.jar:na]
at com.querydsl.core.support.SerializerBase.visit(SerializerBase.java:231) ~[querydsl-core-4.1.4.jar:na]
at com.querydsl.jpa.JPQLSerializer.visit(JPQLSerializer.java:39) ~[querydsl-jpa-4.1.4.jar:na]
at com.querydsl.core.types.SubQueryExpressionImpl.accept(SubQueryExpressionImpl.java:57) ~[querydsl-core-4.1.4.jar:na]
【问题讨论】:
【参考方案1】:需要JPASubQuery
的实例,而不是使用封闭查询的实例。您可以使用new JPASubQuery()
或便捷方法JPAExpressions.select
。您的查询应如下所示:
JPAQuery<Tuple> query = new JPAQuery<Tuple>(em);
query.from(room)
.where(room.nRoomId.notIn
(JPAExpressions.select(roomDepartmentMapping.nRoomId)
.from(roomDepartmentMapping)
)
);
【讨论】:
坦克它的工作。你能检查一下这个https://***.com/questions/52111072/spring-with-querydsl-null-pointer-exception
以上是关于如何在 Querydsl 中创建子查询的主要内容,如果未能解决你的问题,请参考以下文章
如何在 MaterializeCSS 下拉菜单中创建子菜单?