Mah*_*leh 9 hibernate detachedcriteria hibernate-criteria
我想将以下子查询转换为使用hibernate子查询:
getCurrentSession().createQuery("from Employee where id in (select adminId from Department where adminId is not null)")
.list();
Run Code Online (Sandbox Code Playgroud)
雇员:
@ManyToOne
@JoinColumn(name = "fk_department_id", nullable = true)
private Department department;
Run Code Online (Sandbox Code Playgroud)部门:
@OneToMany(fetch = FetchType.EAGER)
@JoinColumn(name = "fk_department_id")
private Set<Employee> employees = new HashSet<Employee>(0);
Run Code Online (Sandbox Code Playgroud)任何人都可以请给我一个这种转换的例子,因为我读了一些例子,我仍然无法弄清楚如何做到这一点.
JB *_*zet 21
Criteria c = session.createCriteria(Employee.class, "e");
DetachedCriteria dc = DetachedCriteria.forClass(Departemt.class, "d");
dc.add(Restrictions.isNotNull("d.adminId");
dc.setProjection(Projections.property("d.adminId"));
c.add(Subqueries.propertyIn("e.id", dc));
Run Code Online (Sandbox Code Playgroud)
该setProjection调用使子查询adminId仅返回属性而不是整个Department实体.将Subqueries.propertyIn创建一个限制:属性id搜索的员工必须是in一组由子查询返回的结果.
| 归档时间: |
|
| 查看次数: |
18902 次 |
| 最近记录: |