如何在连接表中设置限制结果的条件?考虑这两个类(仅包括与此问题相关的字段):
// A customers account
public class PaymentAccount {
private int paymentaccountno;
private Set<PaymentAccountItem> accountitems;
}
// Some invoice item related to the account above
public class PaymentAccountItem {
private int paymentaccountitemno;
private boolean invoiced;
}
PaymentAccount和PaymentAccountItem之间存在一对多的关系,如下所示:
<class name="PaymentAccount" table="paymentaccount">
<id name="paymentaccountno">
<generator class="sequence">
<param name="sequence_name">paymentaccount_paymentaccountno_seq</param>
</generator>
</id>
<set name="accountitems" table="paymentaccountitem" inverse="false" lazy="true" fetch="select" cascade="all">
<key column="paymentaccountno" />
<one-to-many class="PaymentAccountItem" />
</set>
</class>
<class name="PaymentAccountItem" table="paymentaccountitem">
<id name="paymentaccountitemno">
<generator class="sequence">
<param name="sequence_name">paymentaccountitem_paymentaccountitemno_seq</param>
</generator>
</id>
<property name="invoiced" />
</class>
我想获得所有PaymentAccount对象的列表,但是(对于这种情况),accountitems-Set应该只包含“invoiced = false”的对象。
我是否自己编写SQL查询,我会做类似的事情:
SELECT * from paymentaccount pa, paymentaccountitem pai WHERE pa.paymentaccountno=pai.paymentaccountno AND pai.invoiced=false;
我如何让Hibernate为我做这件事(如果可能的话,最好让我的代码保持良好和无SQL)。
谢谢!
答案 0 :(得分:0)
您的查询结果不是实体,您可以尝试这样做:
Query query = session.createSQLQuery(
"SELECT * from paymentaccount pa, paymentaccountitem pai WHERE pa.paymentaccountno=pai.paymentaccountno AND pai.invoiced=false");
List result = query.list();