答案 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个实体会有所帮助。