当前位置: 首页 > 知识库问答 >
问题:

Hibernate:自连接混乱?

陆翰学
2023-03-14

我有一个类别表,其中前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,并以标准json格式向用户返回上述图像之类的数据?

获取子类别

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

}

方法

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

谁能纠正我的错误,告诉我如何获得像上面的图像的结果?

共有2个答案

雍俊远
2023-03-14

对于您的自我加入,以下内容适用于您。

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

    @OneToMany(mappedBy = "parent")
    private Set<FetchSubCategory> subCategory;

FetchSubCategory实体类,我们定义了两个属性:FetchSubCategory父类和集合

此外,注释@JoinColVIII上定义,使其成为关系所有者。定义了连接列,在我们的例子中是parent_category_id

宋飞文
2023-03-14

这东西会解决你的问题

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

获取您的子类别

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;
    }
 类似资料:
  • 问题内容: 我有一张category桌子,前五个是主要类别,其他是 子类别。 我需要获取前5个主要类别的子类别,所以我找到了sql查询 查询本身连接到同一张表,并得到下面给出的结果 结果 如何将SQL查询转换为HQL并 以标准json格式将上述图像之类的数据返回给用户? FetchSubCategory Method _任何人都可以纠正我的错误并告诉我如何获取上图所示的结果吗? 问题答案: 这些东

  • 问题内容: 我似乎无法hibernate使用c3p0进行连接池,它说 hibernate配置: 问题答案:

  • 以下代码: hibernate.cfg.xml: hibernateutil.java: 为了获得最好的帮助,当我尝试连接this Web服务时,请在Android Studio中查看my应用程序的日志: org.hibernate.exception.internal.standardsqlexceptionconverter.convert(standardsqlexceptionconver

  • 问题内容: 我想将2个使用hibernate注释的实体与一个自定义连接子句关联起来。该子句具有通常的FK / PK相等性,但FK为null时也是如此。在SQL中,这类似于: 从我阅读的内容中,我应该在所有者实体上使用@WhereJoinTable批注,但是我对如何指定此条件感到困惑……尤其是它的第一部分-指的是联接实体的ID。 有人有例子吗? 问题答案: 这是一个使用标准父/子范例的示例,我认为应

  • 其最终目标是能够公开on-prem SQL Server,以便它可以像使用Azure的“常规”SQL Server一样连接到。我正试图避免进行Azure数据同步,因为我不想在这种情况下“复制”数据,尽管如果这不起作用,我可能不得不退回到Azure数据同步。 到目前为止,我已经创建了到SQL Server的Azure混合连接,现在我正尝试通过混合连接连接到SQL Server。 我已经通过创建SAS

  • 我正在尝试删除一个父/子自连接实体,但无法这样做,这里是我的映射