Assuming that your entities model is:
@Entity
class Enterprise {
...
@OneToMany
List<Product> products;
}
@Entity
class Product {
...
String status;
}
The following criteria should work:
CriteriaQuery<Enterprise> criteria = builder.createQuery(Enterprise.class);
Root<Author> root = criteria.from( Enterprise.class );
Join<Object, Object> productsJoin = root.join( "products" );
criteria.where( builder.equal( productsJoin.get("status"), "ACTIVE" ) );
List<Enterprise> result = session.createCriteria(criteria).getResultList();
It's the same as the HQL query:
from Enterprise e join e.products p
where p.status = 'ACTIVE'
If you want to load the association eagerly you can replace root.join
with root.fetch
:
CriteriaQuery<Enterprise> criteria = builder.createQuery(Enterprise.class);
Root<Author> root = criteria.from( Enterprise.class );
Join<Object, Object> productsJoin = (Join<Object, Object>)root.fetch( "products" );
criteria.where( builder.equal( productsJoin.get("status"), "ACTIVE" ) );
List<Enterprise> result = session.createCriteria(criteria).getResultList();
Here's the equivalent HQL query:
from Enterprise e join fetch e.products p
where p.status = 'ACTIVE'
You can find more examples in this article or in the Hibernate ORM documentation.