2

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

使用下面的类,从根节点开始,我可以使用 getSubNodes() 并接收它的子节点。但是,如果我随后在 db 中有自己的子节点的子节点上调用 getSubNodes(),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 属性,而是使用查找器查询,则一切都按预期工作,并且可以检索完整的树:

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

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

4

1 回答 1

0

try this @OneToMany(cascade = CascadeType.REFRESH, optional = false) @JoinColumn(name = "currentTablecolumnName", referencedColumnName = "ID", nullable = false)

于 2013-10-26T14:23:24.470 回答