我有它產生的結果,我需要一個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?
分類表格
實際結果執行查詢
映射類
後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;
}
我以前問有關這個問題,我沒有得到答案,我完全卡住。
從您的SQL查詢我可以看到你只想parent.id,父.name和child.name,但在你的HQL嘗試中,你期望FetchSubCategory對象。你真的想要什麼? – luksch
可能重複的[休眠:自我加入混亂?](http://stackoverflow.com/questions/31668522/hibernate-self-join- confusion) – kryger
我還沒有得到任何答案,這就是爲什麼我問了很少有點解釋更多..不要這樣做,這與我有關。 – Stella