简体   繁体   English

休眠SQL查询

[英]hibernate sql query

i have a strange problem with a hibernate sql query: 我有一个休眠的SQL查询一个奇怪的问题:

The db relations are like follows: 数据库关系如下:

registration has one invoicerecipient
registration has many attendees

i have the persid of an invoicerecipient, so I should get in both following cases the associated registration, but only the second case works. 我有一个发票接收者的特权,因此在以下两种情况下我都应该获得相关的注册,但是只有第二种情况有效。 Does anybody know why the first case doesn't work? 有人知道为什么第一种情况不起作用吗?

select distinct registration from Registration registration, in(registration.attendees) atts where atts.id = :persid or registration.invoicerecipient.id = :persid

select distinct registration from Registration registration where registration.invoicerecipient.id = :persid

I don't know whether Hibernate allows implicit collection reference in where clause according to Aaron Digulla's answer (registration.attendees.id). 我不知道Hibernate是否根据Aaron Digulla的答案(registration.attendees.id)在where子句中允许隐式集合引用。 JPA specification does not allows it. JPA规范不允许这样做。 It makes sense. 这说得通。 registration.getAttendees().getId() is illegal in Java language. registration.getAttendees()。getId()在Java语言中是非法的。

But you can compare references according to: 但是您可以根据以下条件比较参考:

select distinct registration from Registration registration, in(registration.attendees) atts where atts = :anotherAttendee or registration.invoicerecipient.id = :persid

Notice IN(registration.attendees) is similar to INNER JOIN registration.attendees. 注意IN(registration.attendees)与INNER JOIN registration.attendees相似。 So a registration needs at least one attendee AND atts.id = :persid should share the same type as said by Aaron Digulla. 因此,注册至少需要一个参与者,并且atts.id =:persid应该与Aaron Digulla所说的类型相同。

regards, 问候,

You can only use types in the from part. 您只能在from部分中使用类型。 What is " , in(registration.attendees) atts " supposed to be? 什么是“ , in(registration.attendees) atts ”,应该是什么?

The correct solution is to walk the structure from the object you have: 正确的解决方案是从具有的对象中遍历结构:

select distinct registration
from Registration registration
where registration.attendees.id = :persid
   or registration.invoicerecipient.id = :persid

Hibernate knows that registration.attendees is a collection, so it will generate the necessary subselect for you. Hibernate知道registration.attendees是一个集合,因此它将为您生成必要的子选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM