选择Hibernate OneToOne关联不存在的实体 [英] Select Entity where Hibernate OneToOne association is not present

查看:334
本文介绍了选择Hibernate OneToOne关联不存在的实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个类,有一个双向的@OneToOne映射到对方。

 类别A {
@OneToOne(fetch = FetchType.Lazy,mappedBy =a)
私人B b;
}

类B {
@OneToOne(fetch = FetchType.Eager)
private A a;
}

我需要编写代码来检索B所没有的所有实例A的一个实例与它们相关联。我还需要为所有没有B的A写一个类似的查询。



我试过了:

  Criteria criteria = getSession()。createCriteria(B.class)
criteria.add(Restrictions.isNull(a)

但这似乎总是返回null。 p>这应该适用于两个方向:

  Criteria criteria = session.createCriteria(B.class,b); 
criteria.createAlias(ba,a,Criteria.LEFT_JOIN);
criteria.add(Restrictions.isNull(a.id));


I have two classes with a bi-directional @OneToOne mapping to each other.

Class A {
@OneToOne(fetch = FetchType.Lazy, mappedBy="a")
private B b;
}

Class B {
@OneToOne(fetch = FetchType.Eager)
private A a;
}

I need to write code to retrieve all instance of B which to do not have an instance of A associated with them. I also need to write a similar query for all A which have no B.

I have tried:

Criteria criteria = getSession().createCriteria(B.class)
criteria.add(Restrictions.isNull("a")

but this seems to always return null. Thoughts?

解决方案

This should work, for both directions:

Criteria criteria = session.createCriteria(B.class, "b");
criteria.createAlias("b.a", "a", Criteria.LEFT_JOIN);
criteria.add(Restrictions.isNull("a.id"));

这篇关于选择Hibernate OneToOne关联不存在的实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆