2016-07-14 45 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中不需要?

回答