我在这个数据模型中使用JPA2 / hibernate:
class Stock {
@ManyToOne
private StockGroup stockGroup;
private boolean visible;
}
class StockGroup {
@OneToMany(mappedBy = "stockGroup")
private List<Stock> stocks;
}
我想检索包含股票的StockGroup,其中visible==true
我想出了这个错误的代码:
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<StockGroup> q = cb.createQuery(StockGroup.class);
Root<StockGroup> r = q.from(StockGroup.class);
Join<StockGroup, Stock> j = r.join(StockGroup_.stocks, JoinType.INNER);
Predicate p = cb.equal(j.get(Stock_.visible), true);
// This becomes a cartesian product :(
List<StockGroup> l = em.createQuery(q.where(p)).getResultList();
// Stocks are not filtered on visible :(
l.get(0).getStocks();
是否可以使用一个CriteriaQuery检索StockGroup和Stock对象,或者JPA是否只能一次填充一种类型?或者我可以在.getStocks()
懒散地填充时添加一些标准吗?
答案 0 :(得分:9)
这样做的诀窍是返回一个元组,其中包含Stock和StockGroup之间的老式连接,如下所示:
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Tuple> q = cb.createQuery(Tuple.class);
Root<Stock> sRoot = q.from(Stock.class);
Root<StockGroup> sgRoot = q.from(StockGroup.class);
q.select(cb.tuple(sRoot, sgRoot)).where(
cb.and(cb.equal(sRoot.get(Stock_.stockGroup), sgRoot),
cb.isTrue(sRoot.get(Stock_.visible))));
List<Tuple> l = em.createQuery(q).getResultList();
元组不是完全类型安全的,但你可以通过位置(或别名,如果你给你的选择表达式或根别名)来达到它:
for (Tuple t : l) {
Stock s = (Stock) t.get(0);
StockGroup sg = (StockGroup) t.get(1);
System.out.println("Stock is : " + s + " .... StockGroup: " + sg);
}
有good article on IBM DeveloperWorks on JPA2 Typesafe Queries。
祝你的JPA2努力好运!