2016-07-14 20 views
0

我正在学习Hibernate,并且我有一个关于基本HQL连接语法的问题。我正在关注这个tutorial。说我有一个产品和类别的实体,在hibernate HQL中指定连接条件“ON列”?

@Entity 
@Table(name = "CATEGORY") 
public class Category { 

    private long id; 
    private String name; 

    private Set<Product> products; 

    public Category() { 
    } 

    public Category(String name) { 
     this.name = name; 
    } 

    @Id 
    @Column(name = "CATEGORY_ID") 
    @GeneratedValue 
    public long getId() { 
     return id; 
    } 


    @OneToMany(mappedBy = "category", cascade = CascadeType.ALL) 
    public Set<Product> getProducts() { 
     return products; 
    } 

    // other getters and setters 
} 

@Entity 
@Table(name = "PRODUCT") 
public class Product { 
    private long id; 
    private String name; 
    private String description; 
    private float price; 

    private Category category; 

    public Product() { 
    } 

    public Product(String name, String description, float price, 
      Category category) { 
     this.name = name; 
     this.description = description; 
     this.price = price; 
     this.category = category; 
    } 

    @Id 
    @Column(name = "PRODUCT_ID") 
    @GeneratedValue 
    public long getId() { 
     return id; 
    } 

    @ManyToOne 
    @JoinColumn(name = "CATEGORY_ID") 
    public Category getCategory() { 
     return category; 
    } 

    // other getters and setters 
} 

所以我要加入类别和产品,我会像这样在SQL

select * from Category A inner join Product B on A.id=B.category_id, 

在HQL,似乎我们放弃了“开”的条件,上述查询的HQL为

String hql = "from Product p inner join p.category"; 

Query query = session.createQuery(hql); 

为什么在HQL中不需要?

回答