0

I have the following code:

Session session = openSession(FlushMode.MANUAL);
Criteria criteria = session.createCriteria(MyClass.class);
UserProfile profile = UserProfile.getInstance();
Transaction tx = null;
try {
    criteria.add(Restrictions.eq("cond1", cond1));
    criteria.add(Restrictions.eq("cond2", cond2));
    criteria.setFirstResult(0);
    criteria.setMaxResults(1);
    criteria.setFetchMode("items", FetchMode.JOIN);
    tx = session.beginTransaction();
    List list = criteria.list();
    ..
}
... 

Here I need just the first entry hence using

criteria.setFirstResult(0);
criteria.setMaxResults(1);

Now I have a Join defined on "items":

criteria.setFetchMode("items", FetchMode.JOIN);

How do criteria work? Will it fetch all the data matching the restrictions and do a join and then order it and return the first row?

Or does it first match restrictions, order it and fetch the first result and then do a join for that single row?

If it is former, How best can this be optimized so that the join operation won't be performed on all the rows?

rajesh
  • 3,189
  • 4
  • 30
  • 56
  • have you found and read the following question already? https://stackoverflow.com/questions/54442919/hibernate-criteria-distinct-entities-fetch-join-and-maxresults it seems to include the answer to your question – PaulD Dec 03 '21 at 10:49

0 Answers0