2015-07-28 128 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