我想用 JPA 实现一些树状结构。我有一个“文件夹”实体和一个“测试”实体。文件夹可以包含文件夹和测试。测试不包含任何内容。
test 和 folder 都有一个 Node 超类,如下所示:
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public class Node implements TreeNode, Serializable{
private Long id;
String description;
String name;
@ManyToOne
Node parent;
...getters, setters and other stuff that doesnt matter...
}
这是文件夹类:
@Entity
public class Folder extends Node{
@LazyCollection(LazyCollectionOption.FALSE)
@OneToMany(cascade=CascadeType.ALL, **mappedBy="parent"**)
List<Folder> folders;
@LazyCollection(LazyCollectionOption.FALSE)
@OneToMany(cascade=CascadeType.ALL, **mappedBy="parent"**)
List<Test> tests;
...
}
所以主要问题是 mappedBy 属性,它与祖先中未覆盖的超类属性有关,因为我遇到了这样的异常:
Exception while preparing the app : mappedBy reference an unknown target entity property: my.test.model.Folder.parent in my.test.model.Folder.folders
Folder 类的“文件夹”和“测试”属性可能存在一些棘手的映射,我需要一些帮助。
编辑:我使用 targetEntity=Node.class 指定了文件夹类的文件夹和测试属性:
@LazyCollection(LazyCollectionOption.FALSE)
@OneToMany(cascade=CascadeType.ALL, mappedBy="parent",targetEntity=Node.class)
List<Folder> folders;
@LazyCollection(LazyCollectionOption.FALSE)
@OneToMany(cascade=CascadeType.ALL, mappedBy="parent",targetEntity=Node.class)
List<Test> tests;
它得到了工作。但工作不正常。现在,当我需要分别获取它们时,测试和文件夹都映射到这两个属性(我不知道为什么我没有得到异常)。
所以我仍在寻找合适的映射来实现这一目标。我会给予任何帮助。