Hibernate - 如何映射 EnumSet
我有一个颜色枚举
public enum color { GREEN, WHITE, RED }
,并且有包含它的 MyEntity。
public class MyEntity {
private Set<Color> colors;
...
我已经有一个 UserType 来映射我的枚举。
您知道如何在 Hibernate hbm.xml 中映射一组枚举吗?
我需要 UserType 还是有最简单的方法?
谢谢
编辑:只是说一下,我正在寻找 hbm.xml 配置不是 @CollectionOfElements 注解
I've a Color Enum
public enum color { GREEN, WHITE, RED }
and I have MyEntity that contains it.
public class MyEntity {
private Set<Color> colors;
...
I already have a UserType to map my Enums.
Do you know how to map a Set of Enums in the Hibernate hbm.xml?
Do I need a UserType or there's an easiest way?
Thanks
edit: Just to remark, I'm looking for the hbm.xml configuration not the @CollectionOfElements Annotation
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我使用 EnumSet 映射 线程中的解决方案,该线程依赖于 <代码><元素列>。您只需要一个带有 id 和字符串的表来映射集合(此处为
MYENTITY_COLOR
)。映射看起来像这样(EnumUserType
是来自 的映射Java 5 EnumUserType):查询可能如下所示:
整个解决方案非常适合加载、保存和查询(归功于 jasonab)。
I use the solution from the EnumSet mapping thread which relies on the use of
<element column>
. You just need a table with an id and a string to map the collection (MYENTITY_COLOR
here). And the mapping looks like that (theEnumUserType
is the one from Java 5 EnumUserType):Query might look like this:
The whole solution works well for loads, saves and queries (credits to jasonab).
看来您需要使用 @CollectionOfElements 注释。该文档位于 http:// /docs.jboss.org/hibernate/stable/annotations/reference/en/html_single/#entity-hibspec-collection-extratype,章节“2.4.6.2.5”。元素或复合元素的集合'。该示例还映射一组枚举。
It seems you need to use the @CollectionOfElements annotation. The doc is at http://docs.jboss.org/hibernate/stable/annotations/reference/en/html_single/#entity-hibspec-collection-extratype, chapter '2.4.6.2.5. Collection of element or composite elements'. The example also maps a Set of Enum.