Hibernate fetching in criteria ignored

风格不统一 提交于 2019-12-01 11:46:54
Firo

as noted here you can not filter and eager fetch a collection at the same time. you can solve it with a correlated subquery:

DetachedCriteria subquery = DetachedCriteria.For(User.class)
    .createAlias("userLoginSession", "session")
    .add(Restrictions.eq("session.token", sessionToken))
    .setFirstResult(0)
    .setMaxResults(1)     // assuming token is unique otherwise this won't restrict users but loginsessions
    .setProjection(Projections.id());

Criteria crit = session.createCriteria(User.class)
    .add(Subqueries.propertyIn("id", subquery)
    .setFetchMode("userLoginSession", FetchMode.JOIN);

Note: this is text editor code of the top of my head. Methodnames may vary

Try:

 Criteria crt = session.createCriteria(User.class);
 crt.setFetchMode("sessions", FetchMode.JOIN);

Try following

Criteria crit = session.createCriteria(User.class);
crit.setFetchMode("session.userId", FetchMode.EAGER);
User myThingy = (User)crit.uniqueResult();

Try setting the jointype on the alias:

Criteria crit = session.createCriteria(User.class);
crit.createAlias("userLoginSession", "session", Criteria.INNER_JOIN);
crit.add(Restrictions.eq("session.token", sessionToken));
crit.setMaxResults(1);
crit.setFirstResult(0);
crit.setFetchSize(1);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!