Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/386.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何迭代树数据?_Java_Hibernate_Spring Mvc - Fatal编程技术网

Java 如何迭代树数据?

Java 如何迭代树数据?,java,hibernate,spring-mvc,Java,Hibernate,Spring Mvc,我有一个数据结构,比如 当我使用 List<TextualReq> textualReqList = session.createQuery("from TextualReq where parent is null").list(); List textualReqList=session.createQuery(“来自TextualReq,其中父项为null”).List(); 此处TextualReq对象为 @Id @GeneratedValue( gen

我有一个数据结构,比如

当我使用

List<TextualReq> textualReqList = session.createQuery("from TextualReq where parent is null").list();
List textualReqList=session.createQuery(“来自TextualReq,其中父项为null”).List();
此处TextualReq对象为

  @Id
       @GeneratedValue( generator = "increment" )
       @GenericGenerator( name = "increment", strategy = "increment" )
       @Column(name="ID")
       private int                    id;

       @ManyToOne
       @JoinColumn(name="parent")
       private TextualReq                parent;
       @OneToMany( mappedBy = "parent", cascade = CascadeType.ALL, fetch = FetchType.EAGER )
       @Column(name="children")
       private Set<TextualReq> children = new HashSet<TextualReq>();
       @Column(name="data")
       private String                  data;
@Id
@生成值(生成器=“增量”)
@GenericGenerator(name=“increment”,strategy=“increment”)
@列(name=“ID”)
私有int-id;
@许多酮
@JoinColumn(name=“parent”)
私人TextualReq父母;
@OneToMany(mappedBy=“parent”,cascade=CascadeType.ALL,fetch=FetchType.EAGER)
@列(name=“children”)
private Set children=new HashSet();
@列(name=“data”)
私有字符串数据;
我将在“textualReqList”中获得两条记录。我需要迭代并显示这些数据,比如

我想这应该够了

public void displayData(){
    display(null);
}
public void display(TextualReq textualReq){
    List<TextualReq> textualReqList = null;

    String parentId = "is null"; 
    if(textualReq!=null){
        parentId = "= "+textualReq.Id;
        System.out.println(textualReq.data);
        System.out.print(" ");
    }
    textualReqList = session.createQuery("select * from TextualReq where parent "+parentId).list();
    if(textualReqList==null)
        return;
    for(int i=0;i<textualReqList.size();i++){
        display(textualReqList.get(i));
    }
}
public void displayData(){
显示(空);
}
公共空白显示(TextualReq TextualReq){
列表文本ualreqlist=null;
字符串parentId=“为空”;
if(textualReq!=null){
parentId=“=”+textualReq.Id;
System.out.println(textualReq.data);
系统输出打印(“”);
}
textualReqList=session.createQuery(“从TextualReq where parent中选择*”+parentId).list();
if(textualReqList==null)
返回;

对于(int i=0;i我建议将
toString()
方法添加到TextualReq类:

public String toString() {
    return toStringWithPrefix("");
}

private String toStringWithPrefix(String prefix) {
    StringBuilder childrenAsString = new StringBuilder();
    for (TextualReq child : children) {
        childrenAsString.append(child.toStringWithPrefix(prefix + '\t'));
    }
    return String.format("%s%s%n%s", prefix, data, childrenAsString);
}
然后,您可以迭代textualReqList并打印每个项目:

for (TextualReq req : textualReqList) {
    System.out.print(req);
}

我认为您的查询不正确。首先,它没有select。其次,它将只提供两条父项为NULL的记录。对于这些记录,它将只提供两条父项记录。当我传递父项(NULL)时对于递归函数,它将返回子项。@P.Jairaj对于HQL,您不需要指定
选择
@v.ladynev哦。好的,我不知道。谢谢。但它仍然只返回2条记录!您的查询仅对
parentId
等于
null
是正确的。最好使用查询参数
:parentId
.