Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Raw Hibernate vs GORM for Hibernate Correlated Subqueries
// Raw Hibernate
DetachedCriteria employeeCriteria = DetachedCriteria.forClass(Employee.class);
employeeCriteria.createAlias("region", "region").setFetchMode("region", FetchMode.JOIN);
employeeCriteria.add(forName("region.continent").in("APAC","EMEA"));
employeeCriteria.setProjection(distinct(property("employeeId")));
Criteria salesCriteria = sessionFactory.getCurrentSession().createCriteria(Sale.class)
salesCriteria.add(forName("employeeId").in(employeeCriteria));
salesCriteria.add(Restrictions.gt("total",100000));
salesCriteria.setProjection(distinct(property("employeeId")));
List<Long> employeeIds = salesCriteria.list();
// GORM for Hibernate
def employees = Employee.where {
region.continent in ['APAC', "EMEA"]
}.id()
def results = Sale.where {
employee in employees && total > 100000
}.employee.list()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment