Search code examples
hibernatejoinhibernate-criteria

Hibernate criteria for a junction table - how to create?


I have simply 3 tables like:

product
.----------
id
name

category
.----------
id
name

product_category
.----------------------
p_id
c_id

I want to get a list of products where the products have category of id=3. I am confused about how to write a hibernate criteria for this. Any help would be appreciated.

Thanks


Solution

  • Criteria c = session.createCriteria(Product.class, "product");
    c.createAlias("product.categories", "category");
    c.add(Restrictions.eq("category.id", 3));