栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何在JPA中映射自定义集合?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何在JPA中映射自定义集合?

可以,但是您必须将其称为常见集合之一-

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

如果不需要任何其他方法来对该结构进行操作,则不要定义自定义集合。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/426549.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号