我在考虑是否使用像 Map<String,List<Entity>>
这样的 map 或 Map<Long,List<Entity>>
它会在一些事情上帮助我,但我找不到任何示例来绘制那种 map 。
我的问题是,是否可以在 JPA 标准中进行这种映射,我应该使用哪种注释?
最佳答案
答案显然是否定的,根据 link在对原始帖子的评论中,我还可以确认它在我尝试过的 Eclipselink 中不起作用。
我尝试做一个嵌套的 Map
像这样:
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 header )时,这会很有用,因为您可以只使用 getAttributes().get(currentLanguage)
而不是编写NamedQueries 或进行流过滤。
至少对于 Eclipselink,我在尝试此操作时遇到了一个荒谬的错误:
IllegalArgumentException: Object: [com.my.project.AttributeEntity@745f] is not a known Entity type.
也许 Hibernate 有一些技巧来支持这一点,但我没有测试它。我的猜测是 - 不。
关于java - 是否可以在 JPA 中映射 map<String,List<Entity>>?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9784101/