Lets assume we have the following situation: We want to inherit all the values of the class Articles except one it's name for instance. How can we achieve it? I know that if we want to inherit everything from the Articles just write
public class Fruits extends Articles{ ... }
but how can we manage to inherit only specific attributes of the class Articles, ie. every attribute except one and one attribute leave intact?
EDIT:
@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;
//...
}
So, I think code won't work, because this will result in multiple IDs in the entity hierarchy, so is there any other way I can solve this?
You can't remove a member from Articles
When name is a member of Articles and Fruits IS A Articles, there could not be a way to remove name
You may hide some members from Articles using scope private
An other approach is to create a class " BaseArticles " without the member name .
Then derive both Articles AND Fruits from BaseArticles
public BaseArticles {
// HAS NO private String name;
...
}
public Article extends BaseArticles {
private String name;
...
}
public Fruits extends BaseArticles {
// WITHOUT private String name;
...
}
However, it is not simple but possible to deal with OO-inheritance using hibernate. There is an annotation but I do not know off hands
您可以将不需要的基类属性设为私有。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.