我在想如果我使用像这样的地图Map<String,List<Entity>>
or Map<Long,List<Entity>>
它将帮助我做一些事情,但我找不到任何示例来绘制此类地图。
我的问题是,是否可以在 JPA 标准中进行这种映射,以及我应该使用哪种注释?
答案显然是否定的,根据link http://en.wikibooks.org/wiki/Java_Persistence/Relationships#Nested_Collections.2C_Maps_and_Matrices在对原始帖子的评论中,我还可以根据我的尝试确认它在 Eclipselink 中不起作用。
我尝试做一个嵌套的 Map> ,其中 key 是 ISO2 语言代码,这将有助于仅检索满足当前语言上下文的子关系的特定子集。
像这样的事情:
public class ProductEntity {
@MapKey(name = "language")
@OneToMany(mappedBy = "product", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY)
private Map<String, List<AttributeEntity>> attributes;
}
public class AttributeEntity {
private String language;
@ManyToOne
@JoinColumn(name="product_id")
private ProductEntity product;
}
当您需要检索特定语言的属性(例如来自 REST Accept-Language 标头)时,这将很有用,因为您可以getAttributes().get(currentLanguage)
而不是编写 NamedQueries 或进行流过滤。
至少对于 Eclipselink,当我尝试这样做时,我遇到了一个无意义的错误:
IllegalArgumentException:对象:
[com.my.project.AttributeEntity@745f] 不是已知的实体类型。
也许 Hibernate 有一些技巧来支持这一点,但我没有测试它。我的猜测是——不。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)