可以,但是您必须将其称为常见集合之一-
List或
Set。
所以:
private List matches = new Matches();
为什么?例如,因为Hibernate对您的集合进行代理以启用延迟加载。所以它创建
PersistentList,
PersistentSet并
PersistentBag,这是
List但不是
Matches。因此,如果您想向该集合中添加其他方法,那么就可以了。
查看本文以获取更多详细信息。
但是,您有解决方案。不使用继承,使用组合。例如,您可以向您的实体添加一个方法
getMatchesCollection()(除了传统的getter之外),该方法类似于:
public Matches getMatchesCollection() { return new Matches(matches); }您的
Matches课程看起来像(使用google-collections ‘
ForwardingList):
public class Matches extends ForwardingList { private List<Match> matches; public Matches(List<Match> matches) { this.matches = matches; } public List<Match> delegate() { return matches; } // define your additional methods}如果您不能使用Google Collections,只需定义
ForwardingList自己-调用底层的所有方法
List
如果不需要任何其他方法来对该结构进行操作,则不要定义自定义集合。



