eps*_*ero 5 java spring-data spring-data-jpa spring-boot
我正在处理几个具有树状结构的实体,它们变得越来越复杂,因此我决定为其创建一个抽象类,以便代码更易于维护:
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class TreeStructure<T extends TreeStructure>
{
@ManyToOne
protected T parent;
@OneToMany(mappedBy = "parent", fetch = FetchType.LAZY)
protected Set<T> children = new HashSet<>();
//...
Run Code Online (Sandbox Code Playgroud)
然后我有两个扩展它的实体:
@Entity(name = "TreeStructureOne")
public class TreeStructureOne extends TreeStructure<TreeStructureOne>
{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@JsonProperty("TreeStructureOne_id")
private long id;
Run Code Online (Sandbox Code Playgroud)
我基本上希望数据库完全不知道这种TreeStructure抽象,并保存每个实体表中的所有字段并期望InheritanceType.TABLE_PER_CLASS处理它。但看来我至少需要Id在实体中定义,否则我得到:TreeStructure
Invocation of init method failed; nested exception is org.hibernate.AnnotationException: No identifier specified for entity: TreeStructure
我不想将 an 添加ID到抽象类中,因为这会在数据库中生成三个表,名为:HT_TREE_STRUCTURE,HT_TREE_STRUCTURE_ONE并且每个表HT_TREE_STRUCTURE_TWO都有一个字段ID。
有什么解决办法吗?
因为TreeStructure不仅仅是@Entity用途@MappedSuperclass
@MappedSuperclass
public abstract class TreeStructure<T extends TreeStructure> {
Run Code Online (Sandbox Code Playgroud)
而不是@Entity和@Inheritance为父类。
@MappedSuperclass您可以在Oracle JEE API 文档中找到。
| 归档时间: |
|
| 查看次数: |
1976 次 |
| 最近记录: |