OpenJPA 1.2.x selects tree structure using JPQL

I am using OpenJPA 1.2.x (JPA1). The problem is that I cannot handle the processing of the tree structure using JPQL.

Please see my essence:

@NamedQueries(
         { 
           @NamedQuery(
             name="Department.getFullTree",
             query="SELECT dep FROM Department dep LEFT JOIN fetch dep.children"
           )
         }
        )
@Entity
public class Department {

    public static final Long ROOT_ID = 0L;

    @Id
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="DEPARTMENT_SEQ")
    @SequenceGenerator(name="DEPARTMENT_SEQ", sequenceName="DEPARTMENT_SEQ", allocationSize=1)
    @Column(name="ID")
    private Long id;

    @Column(name="PARENT_ID")
    private Long parentId;

    @ManyToOne(targetEntity = Department.class, fetch = FetchType.EAGER)
    @JoinColumn(name = "PARENT_ID")
    private Department parent;

    @Column(name="LABEL")
    private String label;

    @OneToMany(mappedBy = "parent", 
               targetEntity = Department.class, 
               fetch=FetchType.LAZY, 
               cascade = {CascadeType.PERSIST, CascadeType.ALL})
    private List<Department> children;

And my beanless stateless way:

public Department getFullTree(){
    em.createNamedQuery("Department.getFullTree").getResultList();
    Department root = em.find(Department.class, Department.ROOT_ID); 
    return root;
}

My goal is to get a complete departmental tree, starting with root. I tried this approach:

It's true? I am using DB2. And will use it in the future. JPA request to get the whole tree

It doesn't seem to work: http://www.tikalk.com/java/load-a-tree-with-jpa-and-hibernate

I tried to repeat, but I get a stackoverflow error when traversing a tree (no more than 200 nodes). The debug output showed that the root itself is a child, so it is associated with a circular structure ...

?

UPD: :

public class TreeTagHelper {

    private static final Logger LOG = LoggerFactory.getLogger(TreeTagHelper.class);

    private Department root;
    private JspWriter out;

    public TreeTagHelper(Department root, JspWriter out){
        LOG.trace("#init");
        this.root = root;
        this.out = out;
    }

    public void printTree() throws Exception{
        LOG.trace("#printTree -> start");
        out.println("<ul id=\"tree-root\"> ");      
        for(Department dep : root.getChildren()){
            printNode(dep, out);
        }       
        closeUL(out);
        LOG.trace("#printTree -> end");
    }

    public static void printNode(Department dep, JspWriter out) throws Exception{
        LOG.trace("#printNode title[{}] children.size()[{}]",dep.getLabel(), (dep.getChildren() == null ? "NULL" : dep.getChildren().size()) );
        openLI(out);
        out.print("<span id=\"tree_node_"+dep.getId()+"\" class=\"ekp-tree-node\" onclick=\"ekpCommon.tree.toggleBullet(this)\">"+dep.getLabel()+"</span>");
        if(dep.getChildren()!=null){
            openUL(out);
            for(Department child : dep.getChildren()){
                printNode(child, out);
            }
            closeUL(out);
        }

        closeLI(out);
    }

    public static void openUL(JspWriter  out) throws Exception{
        out.println("<ul>");
    }

    public static void closeUL(JspWriter out) throws Exception{
        out.println("</ul>");       
    }

    public static void openLI(JspWriter out) throws Exception{
        out.println("<li>");
    }

    public static void closeLI(JspWriter out) throws Exception{
        out.println("</li>");       
    }

LOG.trace( "# printNode title [{}] children.size() [{}]", dep.getLabel(), (dep.getChildren() == null? "NULL": dep.getChildren().size()));

: "# printNode title [ ] children.size() [19]" , ( " " ) 19 . , . - ! ...??? ? ?

0
3

, stackoverflow ( 200 ). , , ...

, , . , .

+1

, . , node. , - ? , StackOverflowError. ?

0

See my DB structure:

ID    PARENT_ID    LABEL
0     0        All Departments
1000   0           Central office

This is why JPA doesn't work :(

See the first line. This is the root of the tree. It seems that the JPA reads it as a child, referencing itself (PARENT_ID exists in the table, so the JOIN can be executed).

I am changing DB values:

ID    PARENT_ID    LABEL
0     -1           All Departments
1000   0           Central office

Now it works! Yes!:)

0
source

All Articles