简体   繁体   English

Hibernate标准在限制内部使用别名

[英]Hibernate criteria use alias inside restrictions

I have two classes where second class is property of first one. 我有两个类,其中第二类是第一类的属性。 For example 例如

Payout 赔率

class Payout{

int id;

Batch batch;

}

Batch 批量

class Batch{

int id;

Date closedDate;

}

I want to fetch payouts with batch is null or batch.closedDate is null ; 我想要获取支付批量为null or batch.closedDate is null ;

I wrote 我写

Criteria criteria = session.createCriteria(Payout.class, "payout")
.add(or(isNull("payout.batch"), and(isNotNull("payout.batch"), isNull("payout.batch.closedDate"))));

Here it doesn't work as there is no joining, but if I create alias on the top for batch then criteria is filtering only if payout has batch. 这里它不起作用,因为没有连接,但如果我在批处理的顶部创建别名,那么仅当支付有批处理时,条件才会过滤。 How can I write criteria without alias for property but filter with property's property? 如何在没有属性别名的情况下编写标准,但是使用属性的属性进行过滤?

After reading for some time I found the solution. 阅读一段时间后,我找到了解决方案。 Criteria criteria = session.createCriteria(Payout.class) .createAlias(Payout_.batch.getName(), "b", JoinType.LEFT_OUTER_JOIN) .add(or(isNull(Payout_.batch.getName()), isNull("b." + Batch_.closed.getName()))); As I told in the question normal join is default in createAlias function and it takes payouts that has batch. 正如我在问题中所说的那样,普通连接在createAlias函数中是默认的,它需要具有批量的支付。 But with JoinType.LEFT_OUTER_JOIN it looks for only batch that's id is equal to payout.batch.id if it exits. 但是使用JoinType.LEFT_OUTER_JOIN,如果它退出,它只查找id等于payout.batch.id的批处理。

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

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