Hibernate - 如何映射 EnumSet

发布于 2024-08-24 12:04:32 字数 414 浏览 5 评论 0原文

我有一个颜色枚举

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

有木有妳兜一样 2024-08-31 12:04:32

我使用 EnumSet 映射 线程中的解决方案,该线程依赖于 <代码><元素列>。您只需要一个带有 id 和字符串的表来映射集合(此处为MYENTITY_COLOR)。映射看起来像这样(EnumUserType 是来自 的映射Java 5 EnumUserType):

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <typedef name="color" class="com.stackoverflow.q2402869.EnumUserType">
        <param name="enumClassName">com.stackoverflow.q2402869.Color</param>
    </typedef>
    <class name="com.stackoverflow.q2402869.MyEntity" entity-name="MyEntity" table="MYENTITY">
        <id name="id" type="java.lang.Long">
            <column name="ID" />
            <generator class="assigned" />
        </id>
        <set name="colors" table="MYENTITY_COLORS">
            <key column="ID" not-null="true"/>
            <element type="color" column="COLOR"/>
        </set>
    </class>
</hibernate-mapping>

查询可能如下所示:

select distinct e from MyEntity e join e.colors colors where colors IN ('WHITE', 'GREEN')

整个解决方案非常适合加载、保存和查询(归功于 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 (the EnumUserType is the one from Java 5 EnumUserType):

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <typedef name="color" class="com.stackoverflow.q2402869.EnumUserType">
        <param name="enumClassName">com.stackoverflow.q2402869.Color</param>
    </typedef>
    <class name="com.stackoverflow.q2402869.MyEntity" entity-name="MyEntity" table="MYENTITY">
        <id name="id" type="java.lang.Long">
            <column name="ID" />
            <generator class="assigned" />
        </id>
        <set name="colors" table="MYENTITY_COLORS">
            <key column="ID" not-null="true"/>
            <element type="color" column="COLOR"/>
        </set>
    </class>
</hibernate-mapping>

Query might look like this:

select distinct e from MyEntity e join e.colors colors where colors IN ('WHITE', 'GREEN')

The whole solution works well for loads, saves and queries (credits to jasonab).

深爱不及久伴 2024-08-31 12:04:32

看来您需要使用 @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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文