I want to convert the following subquery to use hibernate subquery:
getCurrentSession().createQuery("from Employee where id in (select adminId from Department where adminId is not null)")
.list();
-
Employee:
@ManyToOne @JoinColumn(name = "fk_department_id", nullable = true) private Department department; -
Department:
@OneToMany(fetch = FetchType.EAGER) @JoinColumn(name = "fk_department_id") private Set<Employee> employees = new HashSet<Employee>(0);
Can anyone please provide me with an example of this convert, because i read some examples and i still cannot figure out how to do that.
The
setProjectioncall makes the subquery return theadminIdproperty only instead of the wholeDepartmententity. TheSubqueries.propertyIncreates a restriction: the propertyidof the searched employee must beinthe set of results returned by the subquery.