【发布时间】:2016-06-02 10:50:24
【问题描述】:
我有以下关系 OneToMany:
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, mappedBy = "dashboardBox", fetch=FetchType.LAZY)
public List<DashboardCell> getCells() {
return cells;
}
和其他类ManyToOne:
@ManyToOne(optional = false, fetch=FetchType.LAZY)
@JoinColumn(name="DASHBOARD_BOX_ID")
public DashboardBox getDashboardBox() {
return dashboardBox;
}
我正在尝试查询没有单元格的框(因为我在单元格列表中有 LAZY)。我试过这样:
public List<DashboardBox> findAll(Users user) {
Session session = sessionFactory.getCurrentSession();
Criteria criteria = session.createCriteria(DashboardBox.class)
.add(Restrictions.eq("userId", user.getId()));
return criteria.list();
}
也是这样的:
public List<DashboardBox> findAll(Users user) {
Session session = sessionFactory.getCurrentSession();
Criteria criteria = session.createCriteria(DashboardBox.class)
.add(Restrictions.eq("userId", user.getId()))
.setFetchMode("cells", FetchMode.SELECT);
return criteria.list();
}
我想要实现的是在查询 DashboardBox'es 时没有单元格列表。
任何人都有想法并可以解释我在使用 Criteria 时如何实现 LAZY 加载?我知道如果我会使用 HQL,我会拥有它,但我有兴趣使用 Criteria's。
谢谢
【问题讨论】:
-
什么不起作用?您的预期结果和实际结果是什么?
-
我已经更新了问题。我想要实现的是在查询 DashboardBox'es 时没有单元格列表。我尝试使用namedQuery,但它仍然不起作用,所以我认为模型中的注释存在问题。
标签: java hibernate hibernate-criteria