[英]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.