从父级JPA中获取所有子级和子级 [英] Getting all children and subchildren from parent JPA

查看:289
本文介绍了从父级JPA中获取所有子级和子级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表TABLE,可以有相同类型的父表. 在Java中,孩子可以到达父级,但是父级没有子级列表.

I have a table TABLE that can have parents of the same type. In Java the child can reach the parent but the parent doesn't have a list of children.

在MySQL中,我能够创建以下查询,该查询为我提供了父级的子级和子级子,但是我无法将其转换为JPA Java.

In MySQL I was able to create the following query that gives me the children and subchildren of the parent, but I'm unable to translate this into JPA Java.

如何翻译此查询:

SELECT * 
FROM TABLE AS T1 
INNER JOIN 
  (SELECT id FROM TABLE WHERE TABLE.parentId = 966) AS T2 
  ON T2.id = T1.parentId OR T1.parentId = 966 
GROUP BY T1.id

使用标准构建器和标准查询进入Java语言后,我已经尝试过类似的方法:

Into java language, using Criteria builder and Criteria Query, I already tried something like this:

CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<TABLE> cq = cb.createQuery(TABLE.class);
    Root<TABLE> tABLE= cq.from(TABLE.class);

    Predicate result = cb.conjunction();

    Join<TABLE, TABLE> TABLEJoin = tABLE.join("parent", JoinType.INNER);
    result = cb.and(result, cb.equal(genericLocationJoin.get("parent"), location));
em.createQuery(cq.select(tAble).where(result)).getResultList();

但这只会给我直接的孩子,而不会给我子孩子.

But this only gives me the direct children, it doesn't give me the subchildren.

谢谢您的帮助.

实体:

@Entity
@Table(name = "table")
public final class TABLE {

    @Column(nullable = false, unique = true, length = 20)
    private String externalId;

    @Column(nullable = false, length = 50)
    private String name;

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "parentId", nullable = true)
    private TABLE parent;
}

推荐答案

您可以在域模型中通过使关系成为双向关系并编写递归方法来遍历树来处理此问题.这样做的优点之一是可以处理任何级别的孩子.

You can handle this in the domain model by making the relationship bi-directional and writing a recursive method to walk the tree. One advantage of this is that it will handle children to any level.

这看起来类似于以下内容,然后对于任何实例,您都可以这样做:

This would look something like the below and then for any instance you can do:

SomeEntity e = //;
e.getChildren(); //only direct children
e.getAllChildren(); //all children

实体:

@Entity
@Table(name = "some_entity")
public final class SomeEntity {

    @Column(nullable = false, unique = true, length = 20)
    private String externalId;

    @Column(nullable = false, length = 50)
    private String name;

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "parentId", nullable = true)
    private SomeEntity parent;

    @OneToMany(mappedBy = "parent")
    private List<SomeEntity> children; //or Set<>

    //returns direct children
    public List<SomeEntity> getChildren(){
        return children;
    } 

    //returns all children to any level
    public List<SomeEntity> getAllChildren(){
        getAllChildren(this);
    }

    //recursive function to walk the tree
    private List<SomeEntity> getAllChildren(SomeEntity parent){
        List<SomeEntity> allChidren = new ArrayList<>();

        for(SomeEntity child : children){
            allChildren.add(child);
            allChildren.addAll(getAllChildren(child);
        }

        return allChildren;
    }
}

这篇关于从父级JPA中获取所有子级和子级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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