2010-11-05 41 views
0

我使用的是OpenJPA 1.2.x(JPA1)。问题是我无法继续使用JPQL查询树结构。OpenJPA 1.2.x使用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方法:

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

我的目标是让全科树从根开始。 我试过这种方法:

这是真的吗?我正在使用DB2。并将在未来使用它。 JPA query for getting the whole tree

这似乎是不工作: http://www.tikalk.com/java/load-a-tree-with-jpa-and-hibernate

我试图重复,但我得到计算器错误,而(在所有没有超过200个节点)遍历树。 调试输出显示root有自己作为孩子,所以它是一个圆形的结构...

接下来我该怎么做?

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标题[{}] children.size()[{}]”,dep.getLabel() ,(dep.getChildren()== null?“NULL”:dep.getChildren()。size()));

总是输出:“#printNode标题[所有部门] children.size()[19]” 好像根( “所有部门”)具有19个孩子。这是真的,我在我的数据库中检查过它。 但每个孩子都是根! 所以它是无限的结构...?根不会生孩子?它取自己?

+0

认为你的意思是OpenJPA 1.2.x.那就是JPA1.0 – DataNucleus 2010-11-06 07:59:56

+0

我想说1.0和1.2有很大的不同。例如1.0.x不支持JPQL中的ORDER BY聚合函数(COUNT,e.t.c.)。我认为我们必须记住实施提供者的实际版本。 – Sergey 2010-11-06 12:40:41

回答

1

我试过重复,但我遍历树(根本没有超过200个节点)时,我得到stackoverflow错误。调试输出显示根本身就是一个孩子,所以它是一个圆形链接结构...

那么你的数据很可能是错的。仔细检查根没有父母。

0

我想你在做什么是正确的,应该工作。您正在将完整的树结构加载到持久性上下文中,然后获取对根节点的引用。你的树遍历可能有问题吗?这将是唯一可能导致StackOverflowError。你能发布你的代码吗?

+0

这里是简单的助手,它接受Department作为树的根,并“打印”树的html视图。当我用树解决问题时,我会转向一些模板引擎。 – Sergey 2010-11-06 12:42:01

0

请参阅我的DB struccture:。

ID PARENT_ID LABEL 
0  0  All Departments 
1000 0   Central office 

这就是为什么JPA没有工作:(

见第一行这是树的根好像JPA读它作为孩子引用到它本身(PARENT_ID确实存在的表,所以加入可以执行)

我已经改变DB值:

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

现在,它的工作原理!是啊! :)