java - JPA LinkedHashMap 注释

标签 java jpa annotations linkedhashmap

我是 JPA 的新手,无法找到一个好的谷歌解决方案。我想为字符串的 LinkedHashMap 设置 JPA 注释。这里有一个转折点,我有一个抽象类,我将其映射为mappedSuperClass。这样这个父类(super class)的每个实现都有字符串的LinkedHashMap。这是我的例子:

@MappedSuperclass
public abstract class AbstractCustomerType implements Serializable
{

@ElementCollection  //is this right?
protected LinkedHashMap<String, String> customerData;
}

那么我的数据库中可以期待什么? AbstractCustomerType 的每个实现都有自己的 customerData 连接表吗?在这种情况下,如果我有 10 个 CustomerTypes,我会有 10 个 customerData 表吗?我该如何对此进行注释,以便我只获得一个在所有 customerType 之间共享的 customerData 表?

最佳答案

这根本行不通。持久集合必须使用接口(interface)类型来定义类型,而不是使用具体类型:Map、Set 或 List,但不能使用 HashMap、LinkedHashMap、HashSet 或 ArrayList。

参见 the specification 的第 23 页:

Collection-valued persistent fields and properties must be defined in terms of one of the following collection-valued interfaces regardless of whether the entity class otherwise adheres to the JavaBeans method conventions noted above and whether field or property access is used: java.util.Collection, java.util.Set, java.util.List, java.util.Map.

关于java - JPA LinkedHashMap 注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18110511/

相关文章:

java - Libgdx 着色器设置 NullPointerException

postgresql - Hibernate:重复键值违反唯一约束

java - 为什么分离实体被视为 transient ?

java - 如何修复 org.springframework.beans.factory.BeanCreationException

java - Hibernate @UniqueConstraint 目的

java - 如何在Java中获取当前日期和其他日期(java.util.Date)之间的差异(以年为单位)

java - Watir-webdriver 结合 java

java - 执行基本 if else 语句时出现编译错误

java - 将 LocalDate.MAX 转换为日期

java - 如何在 ContainerRequestFilter 中检索请求的匹配资源