I want to use Criteria for this query :
SELECT * FROM table1 WHERE field1 in (
  SELECT field2 FROM table2 WHERE field3 in (
    SELECT field4 FROM table3 where field5 = %VAR%
  )
)
After reading Hibernate subquery detachedCriteria, I've tried this :
public List<Table1> findByParam(String param) {
  logger.info("finding all Table1 where param is '{}'", param);
  Session session = this.getSessionFactory().getCurrentSession();
  try {
    session.beginTransaction();
    DetachedCriteria field4List = DetachedCriteria.forClass(Table3.class, "t3").setProjection(Projections.distinct(Projections.property("t3.id.field4"))).add(Property.forName("t3.id.field5").eq(param));
    DetachedCriteria field2List = DetachedCriteria.forClass(Table2.class, "t2").setProjection(Projections.distinct(Projections.property("t2.id.field2"))).add(Property.forName("t2.id.field3").in(field4List));
    // NullpointerException on this line
    List<Table1> results = session.createCriteria(Table1.class, "t1").add(Property.forName("t1.id.field1").in(field2List)).list();
    logger.info("find all successful, result size: {}", results.size());
    session.getTransaction().commit();
    return results;
  } catch (RuntimeException re) {
    logger.error("find all failed", re);
    throw re;
  } finally {
    try {
      if (session.isOpen()) {
        session.close();
      }
    } catch (Exception e) {
      logger.warn("Something wrong with session", e);
    }
  }
}
But result is a NullPointerEXception on executing query.
public List<Table1> findByParam(String param) {
  logger.info("finding all Table1 where param is '{}'", param);
  Session session = this.getSessionFactory().getCurrentSession();
  try {
    session.beginTransaction();
    DetachedCriteria field2List = DetachedCriteria.forClass(Table2.class, "t2").setProjection(Projections.distinct(Projections.property("t2.id.field2")));
    // No Exception for the example. Don't know why ?
    List<Table1> results = session.createCriteria(Table1.class, "t1").add(Property.forName("t1.id.field1").in(field2List)).list();
    logger.info("find all successful, result size: {}", results.size());
    session.getTransaction().commit();
    return results;
  } catch (RuntimeException re) {
    logger.error("find all failed", re);
    throw re;
  } finally {
    try {
      if (session.isOpen()) {
        session.close();
      }
    } catch (Exception e) {
      logger.warn("Something wrong with session", e);
    }
  }
}
No NullPointerEXception on executing this above query.
Could someone help me fix the first ? And tell me why the second one does not run to an Exception ?