HQL how to join three table

2019-05-26 06:44发布

问题:

I Have this classes:

 @Entity
 public class Category {
    private Long Id;
    private String name;
    private String description;
    private List<Product> products;
}

@Entity
public class Inventory {

    private Long id;
    private Product product;
    private int quantity;
}

@Entity
public class Product {
    private Long productId;
    private String name;
}

I want to get the Inventory given the id in the category. i'm trying to use this

return session.createQuery("select i from Inventory i, Category c join c.Products p outer join i.product = p WHERE c.Id=?")
                .setParameter(0, categoryId).list();

I'm really confused, please help. Thanks.

回答1:

Alright nevermind, I found out how to do it

Select i from Inventory i,Category c INNER JOIN i.product ip INNER JOIN c.products cp where ip = cp and c.id=?

So I was actually wondering how to relate the Category to the joins, I found the answer under Polymorphic queries in HQL documentation



标签: java mysql sql hql