如何在 Querydsl 中创建子查询

How to create subquery in Querydsl

我是 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]

不需要使用封闭查询的实例,而是需要 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)
                     )
           );