Choose Entity where Hibernate OneToOne is missing

I have two classes with bidirectional matching @OneToOne 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 copies B, for which there is no instance of A, associated with them. I also need to write a similar request for all A, which do not B.

I tried:

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

but it always returns null. Thoughts?

+5
source share
2 answers

This should work in both directions:

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

Hibernate has a documented issue that says "NULL request syntax will not work with a one-to-one association!"

In addition, there is a pending Jira in the same issue of JIRA

, , , JB Nizet, .

+1

All Articles