2009-07-28 8 views
2

を休止状態:私はinvoicerecipientのpersidを持っているので、私は両方の次に取得する必要は私が休止状態のSQLクエリとの奇妙な問題を抱えているSQLクエリ

registration has one invoicerecipient 
registration has many attendees 

デシベルの関係は次のようにしている以下の関連する登録をケースするが、第2のケースのみが機能する。最初のケースがなぜ機能しないのか誰も知っていますか?

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 
+0

どちらの文も有効なJPAクエリ言語です。 http://stackoverflow.com/questions/1193483/hibernate-sql-query/1193647#1193647コメントを参照してください。 –

答えて

0

from部分にのみタイプを使用できます。 「​​」とは何ですか?

正しい解決策は、あなたが持っているオブジェクトから構造を歩くことである:

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

Hibernateはregistration.attendeesがコレクションであることを知っているので、それはあなたのために必要な副選択を生成します。

1

Aaron Digullaの答え(registration.attendees.id)に従って、Hibernateがwhere句で暗黙のコレクション参照を許可するかどうかわかりません。 JPA仕様では許可されていません。それは理にかなっている。 registration.getAttendees()。getId()はJava言語では不正です。

しかし、あなたがに応じて参照を比較することができます(registration.attendees)では

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

お知らせINNERに似てregistration.attendeesを登録しよう。だから登録には少なくとも1人の出席者が必要で、atts.id =:persidはAaron Digullaと同じタイプを共有するべきです。

について、

+0

ありがとう、それは、出席者のコレクションがこの時点で空であるため、動作しません。 – ddejmek

関連する問題