【问题标题】:Hibernate Annoation for EnumMap<Enum,String> (OneToMany)EnumMap<Enum,String> (OneToMany) 的休眠注解
【发布时间】:2013-11-13 13:35:19
【问题描述】:

我很难为 bean 属性找到正确的休眠注释。 它是一个 EnumMap 变量。 K 是 EnumType,K 是字符串。我看到许多讨论类似主题的帖子,但我没有看到任何使用字符串作为映射值的线程。我想这可能是这里的问题。我想将其映射为 OneToMany 关系。如果 EnumMap 的键值是表中的字符串值并且映射应该可以为空,那就太好了。 那可能吗?我在这里试过这个:

@OneToMany
@MapKeyEnumerated(EnumType.STRING)
private EnumMap<IDType, String> ids;

例如枚举的注释,但没有任何作用。我得到一个例外:

Initial SessionFactory creation failed.org.hibernate.AnnotationException: Illegal attempt to map a non collection as a @OneToMany, @ManyToMany or @CollectionOfElements: entities.persistent.IDContainer.ids

很高兴有任何想法。我有点失落... 非常感谢!

【问题讨论】:

  • 它不是 OneToMany,因为它不包含实体。所以它是一个元素集合。而且你必须使用 Map 作为类型,而不是 EnumMap。
  • 感谢您的建议。我已经试过这个版本了。但随后抛出另一个异常:无法确定类型:java.util.Map,列:[org.hibernate.mapping.Column(ids)]

标签: java hibernate enums annotations enum-map


【解决方案1】:

你可以使用@ElementCollection注解:

@ElementCollection
@MapKeyEnumerated(EnumType.STRING)
private Map<IDType, String> ids = new EnumMap<>(IDType.class);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-07
    • 2019-12-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多