2015-07-28 86 views
2

我有它產生的結果,我需要一個SQL查詢:將複雜的SQL轉換爲HQL?

SELECT m.category_id,m.category_name, 
     e.category_name 
FROM category e 
INNER JOIN category m ON m.category_id = e.parent_category_id 
ORDER BY m.category_name 

我怎樣才能將它轉換爲HQL?

分類表格

enter image description here

實際結果執行查詢

enter image description here

映射類

import java.io.Serializable; 
import java.util.List; 
import javax.persistence.*; 
import org.hibernate.annotations.NotFound; 
import org.hibernate.annotations.NotFoundAction; 

@Entity 
@Table(name = "category") 
public class FetchSubCategory implements Serializable { 

    private static final long serialVersionUID = 1L; 

    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    @Column(name = "category_id") 
    private Integer categoryId; 

    @Column(name = "category_name") 
    private String categoryName; 

    @NotFound(action = NotFoundAction.IGNORE) 
    @ManyToOne(cascade = {CascadeType.ALL}) 
    @JoinColumn(name = "parent_category_id") 
    private FetchSubCategory parent; 

    @OneToMany(fetch = FetchType.EAGER, mappedBy = "parent", cascade = CascadeType.ALL) 
    private List<FetchSubCategory> subCategory; 

    public List<FetchSubCategory> getSubCategory() { 
     return subCategory; 
    } 

    public void setSubCategory(List<FetchSubCategory> subCategory) { 
     this.subCategory = subCategory; 
    } 

    public Integer getCategoryId() { 
     return categoryId; 
    } 

    public void setCategoryId(Integer categoryId) { 
     this.categoryId = categoryId; 
    } 

    public String getCategoryName() { 
     return categoryName; 
    } 

    public void setCategoryName(String categoryName) { 
     this.categoryName = categoryName; 
    } 

    public FetchSubCategory getParent() { 
     return parent; 
    } 

    public void setParent(FetchSubCategory parent) { 
     this.parent = parent; 
    } 

} 

取出方法

public List<FetchSubCategory> fetchSubCategory() throws SQLException, ClassNotFoundException, IOException { 
     List<FetchSubCategory> groupList = null; 
     try { 
      Session session = sessionFactory.getCurrentSession(); 

      Query query = session.createQuery("FROM FetchSubCategory e INNER JOIN e.subCategory m ORDER BY m.parent"); 
      groupList = query.list(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

     return groupList; 
    } 

我以前問有關這個問題,我沒有得到答案,我完全卡住。

+0

從您的SQL查詢我可以看到你只想parent.id,父.name和child.name,但在你的HQL嘗試中,你期望FetchSubCategory對象。你真的想要什麼? – luksch

+0

可能重複的[休眠:自我加入混亂?](http://stackoverflow.com/questions/31668522/hibernate-self-join- confusion) – kryger

+0

我還沒有得到任何答案,這就是爲什麼我問了很少有點解釋更多..不要這樣做,這與我有關。 – Stella

回答

0

爲什麼不使用hibernate配置,使用xml或註釋。

示例 - @OneToMany

您可以在「實際結果」表的主鍵,然後可以在兩個表之間的配置「一對多」的關係 - 有了這個,你可以用一個簡單的HQL。示例 -

select actualResult from ActualResult actualResult。這將從「實際結果」和類別表中的相關數據中提取數據。

+1

實際結果不是表格,它是我顯示的結果.. – Stella

+0

我弄錯了 - 我的錯。 –

1

的HQL您的問題將是

select fsc.categoryId,fsc.categoryName,fsc.parent.categoryName from FetchSubCategory fsc 

where fsc.categoryId=fsc.parent.categoryId 

order by fsc.categoryName 

要獲得更詳細的HQL的工作,請遵循怎樣本文link

+0

這不會顯示任何結果 – user2137817