0

I am converting to hibernate 5.2.4 and to use CriteriaQuery. Have a slight issue in testing for a null join on a file. This is what I was doing before which worked.

@Entity
public class MyClass {
 ....
 @ManyToOne(..)
 public MyDetail getMyDetail() { 
  return myDetail;  
 }
}

Criteria criteria = session.createCriteria(MyClass.class);
criteria.createAlias("myDetail", "md");
criteria.add(Restrictions.isNotNull("myDetail"));

I have been checking like this and sometimes it does not work:

andPredicate = builder.and(andPredicate, builder.isNotNull(
                    root.get(MyClass_.myDetail)));

Is there a correct way to do this? I have tried to create the join and test for a null but this also does not work:

Join<MyClass, MyDetail> myJoin = root.join(MyClass_.myDetail);

andPredicate = builder.and(andPredicate, myJoin.isNotNull());

Thanks.

code11
  • 1,986
  • 5
  • 29
  • 37
gregh
  • 171
  • 2
  • 8

1 Answers1

0

Worked it out, I need a matching

criteria.createAlias("myDetail", "md");

ie:

root.join(MyClass_.myDetail);

As the query did not have any selections on the relation so I was not doing the join.

Seems also that I do not have to check either whether the join is null. Just doing the join is enough.

gregh
  • 171
  • 2
  • 8