本文介绍了OpenJPA 1.2.x使用JPQL选择树结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用OpenJPA 1.2.x(JPA1).问题是我无法使用JPQL来查询树结构.

I'm using OpenJPA 1.2.x (JPA1). The problem is that I can't get on with querying tree structure using JPQL.

请参见我的实体

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

还有我的无状态bean方法:

And my stateless bean method:

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

我的目标是从根开始获得完整的部门树.我已经尝试过这种方法:

My aim is to get full department tree starting from root.I've tried this approach:

是真的吗?我正在使用DB2.并将在将来使用.用于获取整棵树的JPA查询

Is that true? I'm using DB2. And will use it in future. JPA query for getting the whole tree

这似乎不起作用: http://www.tikalk.com/java/用jpa和休眠加载树

我尝试重复,但是遍历树时出现stackoverflow错误(根本不超过200个节点).调试输出显示root本身具有孩子身份,因此它是圆形链接结构...

I've tried to repeat, but I get stackoverflow error while traversing tree (don't have more than 200 nodes at all).Debug output showed that root has itself as a child, so it's round-linked structure...

接下来我要尝试什么?

UPD:这是我的遍历代码:

UPD:Here is my traverse code:

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()));

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

始终输出:#printNode标题[所有部门] children.size()[19]" 似乎根(所有部门")有19个孩子.是的,我在数据库中检查了它.但是每个孩子都是根!所以它是无限的结构... ???根不生孩子?它会自我获取吗?

always outputs: "#printNode title[All departments] children.size()[19]"Seems like the root("All departments") has 19 children. It's true, I checked it in my DB.But each children is the root! So it's infinite structure...??? Root doesn't get children? It fetches itself?

推荐答案

那么您的数据很可能是错误的.仔细检查根是否没有父级.

Then your data are very likely wrong. Double check that the root has no parent.

这篇关于OpenJPA 1.2.x使用JPQL选择树结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 21:10