[英]How to use QueryDsl with @PrimaryKeyJoinColumn?
I want to create a querydsl Predicate
to use it with spring-jpa
CrudRepository
.我想创建一个 querydsl Predicate
以将其与spring-jpa
CrudRepository
使用。
Problem: I have an optional @PrimaryKeyJoinColumn
that references to the child entity by @Id
PK.问题:我有一个可选的@PrimaryKeyJoinColumn
,它通过@Id
PK 引用子实体。 And I want to select only the rows where the child table does not contain a reference with the main row.我只想 select 子表不包含对主行的引用的行。
In this example: I only want to select bookings in status = 2, and where no editor reference exists inside the child table.在这个例子中:我只想要状态 = 2 的 select 预订,并且子表中不存在编辑器引用。
BooleanBuilder booleanBuilder = new BooleanBuilder(predicate)
.and(QBooking.booking.status.eq(2))
.and(QBooking.booking.editor.id.isNull());
dao.findAll(booleanBuilder.getValue()); //dao is QuerydslPredicateExecutor<Booking>
class Booking {
@Id
long id;
String status;
//joins the table by their PK id
@PrimaryKeyJoinColumn
public Editor editor;
}
class Editor {
@Id
long id;
...
}
The sql generated from it is:从中生成的sql为:
select * from booking
CROSS JOIN editor
WHERE booking.id = editor.id
and booking.status = ?
and editor.id is null;
But this is wrong (as of course this never returns any result): What I'm looking for is:但这是错误的(当然这永远不会返回任何结果):我正在寻找的是:
select * from booking
CROSS JOIN editor
WHERE (booking.id = editor.id OR editor.id is null)
and booking.status = ?;
How can I achieve the later sql statement with querydsl?怎么用querydsl实现后面的sql语句呢?
You should be able to workaround the issue by using QBooking.booking.editor.isNull()
instead, which is perfectly valid to do in JPQL.您应该能够通过使用QBooking.booking.editor.isNull()
来解决这个问题,这在 JPQL 中是完全有效的。 However, ID dereference should have just worked here, so you're probably looking at a bug in your ORM implementation.但是,ID 取消引用应该在这里起作用,因此您可能正在查看 ORM 实现中的错误。 Probably upgrading to the latest version of Hibernate will resolve your issue as well.升级到最新版本的 Hibernate 可能也会解决您的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.