休眠:自我加入混乱? [英] Hibernate : self join confusion?

查看:103
本文介绍了休眠:自我加入混乱?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有一个类别表。其中前5个是主类别,
其他类别是子类别。




我需要抓取前5个主类别的子类别,所以我已经找到了SQL查询

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

查询加入同一个表本身,并得到下面给出的结果


结果


如何将SQL查询转换为HQL并返回上述数据image to user in
standard json format?



FetchSubCategory



  import java.io.Serializable; 
import java.util.Set;
import javax.persistence。*;

@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;

@ManyToOne(cascade = {CascadeType.ALL})
@JoinColumn(name =parent_category_id)
private FetchSubCategory parent;

@OneToMany(mappedBy =parent)
private设置< FetchSubCategory>子类别;

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 Set< FetchSubCategory> getSubCategory(){
return subCategory;
}

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

}




方法



  public Set< FetchSubCategory> fetchSubCategory()抛出SQLException,ClassNotFoundException,IOException {
Set< FetchSubCategory> groupList = null;
尝试{
Session session = sessionFactory.getCurrentSession();
Query query = session.createQuery(SELECT m.categoryName AS'Parent',e.categoryName AS'Sub'FROM FetchSubCategory e INNER JOIN FetchSubCategory m ORDER BY Parent);

groupList =(Set< FetchSubCategory>)query.list();
} catch(Exception e){
e.printStackTrace();
}

返回groupList;
}

任何人都可以改正我的错误并告诉我如何获取如上图所示的结果? 将解决您的问题



  @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
@JsonIgnore
@JoinColumn(name =parent_category_id)
private FetchSubCategory mainCategory;

@JsonSerialize(include = JsonSerialize.Inclusion.NON_EMPTY)//避免空json arrays.objects
@OneToMany(mappedBy =mainCategory,fetch = FetchType.EAGER)
私人列表< FetchSubCategory>子类别;

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 getMainCategory(){
return mainCategory;
}

public void setMainCategory(FetchSubCategory mainCategory){
this.mainCategory = mainCategory;
}

公开列表< FetchSubCategory> getSubCategory(){
return subCategory;
}

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




获取您的子类别




  public List< FetchSubCategory> fetchSubCategory()抛出SQLException,ClassNotFoundException,IOException {
List< FetchSubCategory> groupList = null;
尝试{
Session session = sessionFactory.getCurrentSession();
Query query = session.createQuery(select distinct FROM FROM FetchSubCategory e INNER JOIN e.subCategory m ORDER BY m.mainCategory);
groupList = query.list();
} catch(Exception e){
e.printStackTrace();
}

返回groupList;
}


I have a category table.In which first 5 are main category and others are sub category.

I need to fetch the sub categories of first 5 main category so i have found the sql query

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

The query is joining the same table itself.and am getting the result given below

Result

How can i convert the SQL query to HQL and return the data like above image to user in standard json format ?

FetchSubCategory

import java.io.Serializable;
import java.util.Set;
import javax.persistence.*;

@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;

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

    @OneToMany(mappedBy = "parent")
    private Set<FetchSubCategory> 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 Set<FetchSubCategory> getSubCategory() {
        return subCategory;
    }

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

}

Method

public Set<FetchSubCategory> fetchSubCategory() throws SQLException, ClassNotFoundException, IOException {
        Set<FetchSubCategory> groupList = null;
        try {
            Session session = sessionFactory.getCurrentSession();
            Query query = session.createQuery("SELECT m.categoryName AS 'Parent', e.categoryName AS 'Sub' FROM FetchSubCategory e INNER JOIN FetchSubCategory m ORDER BY Parent");

            groupList = (Set<FetchSubCategory>) query.list();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return groupList;
    }

Can any one please correct my mistake and tell me how to fetch result like above image?

解决方案

This stuff will solve your problem

@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
    @JsonIgnore
    @JoinColumn(name = "parent_category_id")
    private FetchSubCategory mainCategory;

    @JsonSerialize(include = JsonSerialize.Inclusion.NON_EMPTY)//Avoiding empty json arrays.objects
    @OneToMany(mappedBy = "mainCategory", fetch = FetchType.EAGER)
    private List<FetchSubCategory> 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 getMainCategory() {
        return mainCategory;
    }

    public void setMainCategory(FetchSubCategory mainCategory) {
        this.mainCategory = mainCategory;
    }

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

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

Get your sub categories

public List<FetchSubCategory> fetchSubCategory() throws SQLException, ClassNotFoundException, IOException {
        List<FetchSubCategory> groupList = null;
        try {
            Session session = sessionFactory.getCurrentSession();
            Query query = session.createQuery("select distinct e FROM FetchSubCategory e INNER JOIN e.subCategory m ORDER BY m.mainCategory");
            groupList = query.list();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return groupList;
    }

这篇关于休眠:自我加入混乱?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆