I have got many OneToMany relationships that needs to be loaded eagerly in most cases like this:
@OneToMany(fetch = FetchType.EAGER, mappedBy = "bar")
@Fetch(FetchMode.SELECT)
@BatchSize(size = 10)
However, I need, in a very few cases, to change it in hibernate criteria to lazy load (still select and batch). But I do not know how to do it. When I try solution from here -
criteria.setFetchMode("propertyName", FetchMode.SELECT);
it still loads eagerly.
My criteria looks something like this now:
Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Bar.class);
criteria.add(Restrictions.eq("valid", true));
criteria.add(Restrictions.idEq(barId));
Bar result = (Bar) criteria.uniqueResult();
Here is entity, just for illustration purposes...
@Entity
@Table(name = "BAR", schema = "UD")
public class Bar {
private List<Foo> foos;
@OneToMany(fetch = FetchType.EAGER, mappedBy = "bar")
@Fetch(FetchMode.SELECT)
@BatchSize(size = 10)
public List<Foo> getFoos() {
return foos;
}
}
I am using Hibernate 5.1.10.Final