次のような状況があると仮定しましょう: クラス Articles のすべての値を、インスタンスの名前を除いて継承したいとします。どうすればそれを達成できますか?記事からすべてを継承したい場合は、次のように書くだけです。
public class Fruits extends Articles{ ... }
しかし、クラス Articles の特定の属性のみを継承するにはどうすればよいでしょうか。1つと1つの属性を除くすべての属性はそのままですか?
編集:
@Entity
@Table(name = "Article")
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class Article {
@Id @GeneratedValue(strategy=GenerationType.AUTO)
@Basic(optional = false)
@Column(name = "ART_ID")
private Long id;
@Basic(optional = false)
@Column(name = "ART_NAME")
private String name;
@Basic(optional = true)
@Column(name = "ART_COST")
private String cost;
// ...
}
@Entity
@Table(name="Fruits")
@AttributeOverrides({
@AttributeOverride(name="name", column=@Column(name="ART_NAME")),
@AttributeOverride(name="cost", column=@Column(name="ART_COST")),
})
// This is what is causing the issue. Fruits inherits something which is already defined in it's scope, and as the result can't declare exactly how to process it.
public class Fruits extends Article {
@Id @GeneratedValue(strategy=GenerationType.AUTO)
@Basic(optional = false)
@Column(name = "FRU_ID")
private Long fruitID;
@Column(name="FRU_FROZEN")
private String fruFrozen;
//...
}
エンティティ階層に複数のIDが発生するため、コードは機能しないと思います。これを解決できる他の方法はありますか?