【问题标题】:Mapping enum subtypes in Hibernate在 Hibernate 中映射枚举子类型
【发布时间】:2013-05-29 07:24:30
【问题描述】:

我正在使用 Java 6Hibernate 3.6.9 库开发 Java 应用程序。我想创建一个与特定类密切相关的枚举,所以我想将它定义为该类的一部分:

package org.mycompany.container;

public class Container{

    public enum ContainerType{
        SMALL,MEDIUM,BIG
    }

    private ContainerType _ContainerType;

    private Integer _Id;

    //Getter and setters

}

然后我想将我的容器类映射到一个 Hibernate 配置文件中(我不使用注释)。于是有了这个xml片(类的包在hibernate-mapping文件开头指定):

<class name="Container" table="tcontainer">
    <id name="_Id" column="id">
        <generator class="increment" />
    </id>
    <property name="_ContainerType" column="container_type">
        <type name="org.hibernate.type.EnumType">
            <param name="enumClass">org.mycompany.container.Container.ContainerType</param>
            <param name="type">12</param>
        </type>
    </property>
</class>

但是,Hibernate 似乎无法识别 ContainerType 类,我得到了一个 ClassNotFoundException。我已经多次将 enum 作为一个单独的类。

有什么解决办法吗?汇集您的想法。

【问题讨论】:

  • 你的 hbm 文件和你的类在同一个项目中吗?因为这可能是一个依赖问题

标签: java hibernate enums


【解决方案1】:

试试这个:

org.mycompany.container.Container$ContainerType

【讨论】:

  • 和这个“我已经多次这样做了,将枚举作为一个单独的类。”没用?
  • @fGo,当然可以,但是对于设计概念,我想将枚举保留在类中。
猜你喜欢
  • 2011-02-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-01
  • 1970-01-01
相关资源
最近更新 更多