我使用的是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个孩子。这是真的,我在我的数据库中检查过它。 但每个孩子都是根! 所以它是无限的结构...?根不会生孩子?它取自己?
认为你的意思是OpenJPA 1.2.x.那就是JPA1.0 – DataNucleus 2010-11-06 07:59:56
我想说1.0和1.2有很大的不同。例如1.0.x不支持JPQL中的ORDER BY聚合函数(COUNT,e.t.c.)。我认为我们必须记住实施提供者的实际版本。 – Sergey 2010-11-06 12:40:41