Ebean / Play Framework 2.1问题获取父/子树结构

时间:2013-10-25 22:40:22

标签: orm playframework playframework-2.1 ebean

我在尝试获取树结构时遇到了问题(一个表中的父/子元素)。

使用下面的类,从根节点开始,我可以使用getSubNodes()并接收其子节点。但是,如果我然后在子节点上调用getSubNodes(),子节点在db中有自己的子节点,则getSubNodes()将返回一个空列表(db数据包含多个树级别)

@Entity
@Table(name = "navigation_items")
public class NavigationItem extends Model {

    public static NavigationItemFinder find = new NavigationItemFinder();
    private static final long serialVersionUID = 1L;

    @Id
    private UUID id;

    @Column (unique=true)
    private String key;

    @ManyToOne
    private NavigationItem parentNode;

    @OneToMany (mappedBy="parentNode", cascade=CascadeType.ALL)
    private List<NavigationItem> subNodes;

    @Column
    @Required
    private String title;

    @Column
    private String subtitle;

    @ManyToOne
    private PageBlock page;

...

    public List<NavigationItem> getSubNodes() {
        return subNodes; // <- This crazily but provenly only returns results on the first level of the hierarchy...

    }

但是,如果我不使用getter返回subnodes属性,而是使用finder查询,则所有内容都按预期工作,并且可以检索完整的树:

    List<NavigationItem> result = find.where().eq("parentNode", this).findList();       
    Logger.debug("NavItem " + getTitle() + "[" + getId() +"]" + ": returning # subnodes: " + result.size());        

我在这里遗漏了什么,或者这可能是(其他)ebean bug?

1 个答案:

答案 0 :(得分:0)

试试这个 @OneToMany(cascade = CascadeType.REFRESH,optional = false) @JoinColumn(name =“currentTablecolumnName”,referencedColumnName =“ID”,nullable = false)