Hibernate criteria: Joining table without a mapped association

This is indeed possible with criteria: DetachedCriteria ownerCriteria = DetachedCriteria.forClass(Owner.class); ownerCriteria.setProjection(Property.forName(“id”)); ownerCriteria.add(Restrictions.eq(“ownername”, “bob”)); Criteria criteria = getSession().createCriteria(Pet.class); criteria.add(Property.forName(“ownerId”).in(ownerCriteria)); Update: This actually performs a sub-query instead of a join but it allows you to use Criteria on two entities that do not have a hibernate relationship defined.

JPA and Hibernate – Criteria vs. JPQL or HQL

I mostly prefer Criteria Queries for dynamic queries. For example it is much easier to add some ordering dynamically or leave some parts (e.g. restrictions) out depending on some parameter. On the other hand I’m using HQL for static and complex queries, because it’s much easier to understand/read HQL. Also, HQL is a bit more … Read more

What is the difference between JOIN and JOIN FETCH when using JPA and Hibernate

In this two queries, you are using JOIN to query all employees that have at least one department associated. But, the difference is: in the first query you are returning only the Employes for the Hibernate. In the second query, you are returning the Employes and all Departments associated. So, if you use the second … Read more