2
實例類:如何在Hibernate中爲子查詢的結果行提供別名?
public Class Order{
private Integer id;
private Customer customer;
}
public Class Customer{
private Integer id;
private String name;
}
例子查詢,我想在PostgreSQL的語法來執行:
SELECT order_alias.id
FROM (select * from order where customer is not null) order_alias
WHERE order_alias.customer < 10;
我怎樣才能做到這一點的休眠?
我問這個問題的原因是因爲當customer屬性爲null時,Hibernate似乎會拋出一個「Unknown entity:null」錯誤。
這就是我試圖用標準做:
DetachedCriteria dc = DetachedCriteria.forClass(Order.class, "o");
dc.add(Restrictions.lt("o.customer.id", 10);
dc.setProjection(Projections.property("o.id");
Criteria query = Session.createCriteria(OtherClass.class, "oc");
query.add(Subqueries.propertyIn("oc.id", dc);
List<OtherClass> listOC = query.list();
而且我得到這似乎是從線2
可以請你添加一個交代了'createAlias'在這方面?我想給完整的子查詢結果集作爲主查詢中使用的別名 –