[英]Spring-Data Jpa Inheritance: Keeping Entity Id's in Children Entity
我正在处理几个使用树状结构的实体,这些实体变得越来越复杂,所以我决定为它创建一个抽象类,所以代码更加可维护:
@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<>();
//...
然后我有两个扩展它的实体:
@Entity(name = "TreeStructureOne")
public class TreeStructureOne extends TreeStructure<TreeStructureOne>
{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@JsonProperty("TreeStructureOne_id")
private long id;
我基本上希望数据库完全不知道这个TreeStructure
抽象,并保存每个Entities表中的所有字段,并期望InheritanceType.TABLE_PER_CLASS
来处理它。 但似乎我需要至少在TreeStructure
实体中定义Id
,或者我得到:
Invocation of init method failed; nested exception is org.hibernate.AnnotationException: No identifier specified for entity: TreeStructure
我不想在抽象类中添加ID
,因为这会在数据库中生成三个表: HT_TREE_STRUCTURE
, HT_TREE_STRUCTURE_ONE
和HT_TREE_STRUCTURE_TWO
,每个HT_TREE_STRUCTURE_TWO
都有一个字段ID
。
那有什么解决方案吗?
由于TreeStructure
不是@Entity
只使用@MappedSuperclass
@MappedSuperclass
public abstract class TreeStructure<T extends TreeStructure> {
而不是父类的@Entity
和@Inheritance
。
您可以在Oracle JEE API文档中找到@MappedSuperclass
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.