Hibernate多个子查询

时间:2015-12-17 11:23:07

标签: java hibernate

我想对此查询使用Criteria: SELECT * FROM table1 WHERE field1 in(   SELECT field2 FROM table2 WHERE field3 in(     SELECT field4 FROM table3 where field5 =%VAR%   ) ) 在阅读了Hibernate子查询detachedCriteria之后,我试过这个: 公共列表<表1> findByParam(String param){   logger.info(“查找所有Table1,其中param是'{}'”,param);   Session session = this.getSessionFactory()。getCurrentSession();   尝试{     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”) )。在(field4List));     //这一行有NullpointerException     列表与LT;表1> 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()提交();     返回结果;   } catch(RuntimeException re){     logger.error(“查找全部失败”,重新);     抛出   } finally {     尝试{       if(session.isOpen()){         session.close();       }     } catch(例外e){       logger.warn(“会话有问题”,e);     }   } } 但结果是执行查询时出现NullPointerEXception。 公共列表<表1> findByParam(String param){   logger.info(“查找所有Table1,其中param是'{}'”,param);   Session session = this.getSessionFactory()。getCurrentSession();   尝试{     session.beginTransaction();     DetachedCriteria field2List = DetachedCriteria.forClass(Table2.class,“t2”)。setProjection(Projections.distinct(Projections.property(“t2.id.field2”)));     //示例没有例外。不知道为什么?     列表与LT;表1> 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()提交();     返回结果;   } catch(RuntimeException re){     logger.error(“查找全部失败”,重新);     抛出   } finally {     尝试{       if(session.isOpen()){         session.close();       }     } catch(例外e){       logger.warn(“会话有问题”,e);     }   } } 执行上述查询时没有NullPointerEXception。 有人可以帮我修复第一个吗? 并告诉我为什么第二个没有遇到异常?

2 个答案:

答案 0 :(得分:1)

  

阅读Hibernate subquery detachedCriteria之后,我已经   试过这个:

但是在此示例中使用了Subqueries.propertyIn("myPersistedObjectId", exampleSubquery),但是在您的查询Subqueries中,密钥根本不存在。请检查您的示例并使用Subqueries.propertyIn(...),您的代码应该是这样的(我没有检查该代码):

DetachedCriteria field2List = DetachedCriteria.forClass(Table2.class, "t2").
  setProjection(Projections.distinct(Projections.property("t2.id.field2"))).
  add(Subqueries.propertyIn("t2.id.field3",field4List));

List<Table1> results = session.createCriteria(Table1.class, "t1").
  add(Subqueries.propertyIn("t1.id.field1",field2List)).list();

答案 1 :(得分:1)

不知道Table1,Table2和Table3有哪些字段,查询不应该是这样的:

DetachedCriteria field4List = DetachedCriteria.forClass(Table3.class, "t3").setProjection(Projections.distinct(Projections.property("t3.field4"))).add(Property.forName("t3.field5").eq(param));

DetachedCriteria field2List = DetachedCriteria.forClass(Table2.class, "t2").setProjection(Projections.distinct(Projections.property("t2.field2"))).add(Property.forName("t2.field3").in(field4List));

// NullpointerException on this line
List<Table1> results = session.createCriteria(Table1.class, "t1").add(Property.forName("t1.field1").in(field2List)).list();

我用t * .field *

替换了所有t * .id.field *次数

如果没有,发布这3个实体会有所帮助。