让我们假设我们有以下情况:我们想要继承类 Articles 的所有值,除了一个它的名字之外。我们怎样才能实现它?我知道,如果我们想继承文章中的所有内容,只需编写
public class Fruits extends Articles{ ... }
但是我们如何才能设法只继承类文章的特定属性,即。除了一个和一个属性之外的每个属性都保持不变?
编辑:
@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,那么我还有其他方法可以解决这个问题吗?