内部枚举可以在Hibernate中持久化吗?

时间:2014-09-26 17:12:28

标签: java mysql hibernate enums

我在“User”类中创建了一个内部枚举“UserType”,它将用于确定User的实例是BASIC,DEPARTMENT_EXCLUSIVE还是SUPERUSER。这是代码的片段:

public class User {

private String id, lastName, firstName, middleName, password;
private UserType userType;

public void setId(String id) {
    this.id = id;
}

public void setLastName(String lastName) {
    this.lastName = lastName;
}

public void setFirstName(String firstName) {
    this.firstName = firstName;
}

public void setMiddleName(String middleName) {
    this.middleName = middleName;
}

public void setPassword(String password) {
    this.password = Encryption.encrypt(password);
}

public void setUserType(UserType userType) {
    this.userType = userType;
}

public String getId() {
    return id;
}

public String getLastName() {
    return lastName;
}

public String getFirstName() {
    return firstName;
}

public String getMiddleName() {
    return middleName;
}

public String getPassword() {
    return password;
}

public UserType getUserType() {
    return userType;
}   

public enum UserType {

    BASIC, DEPARTMENT_HEAD, SUPERUSER;

}

}

现在,我使用ORM将实例化对象保存到MySQL数据库。我正在使用Hibernate。这是我在类标记中的Hibernate映射文件片段:

<id name="id" type="string" column="id"/>
    <property name="lastName" column="lastName" type="string" not-null="true"/>
    <property name="firstName" column="firstName" type="string"/>
    <property name="middleName" column="middleName" type="string"/>
    <property name="password" column="password" type="string" not-null="true"/>
    <property name="userType" column="userType" not-null="true">
        <type name="org.hibernate.type.EnumType">
            <param name="enumClass">com.fileManagement.dataDesign.User.UserType</param>
            <param name="type">12</param>
            <param name="useNamed">true</param>
        </type>
    </property>

我测试了这个,但是抛出一个异常,告诉我没有找到枚举。这是测试代码:

SessionFactory f = HibernateUtil.getSessionFactory();
    Session s = f.openSession();
    Transaction t = s.beginTransaction();
    User user = new User();
    user.setId("0090713");
    user.setLastName("Nocos");
    user.setFirstName("Warren");
    user.setMiddleName("Manlangit");
    user.setPassword("wang1234");
    user.setUserType(UserType.DEPARTMENT_HEAD);
    s.save(user);
    t.commit();
    s.close();
    f.close();

这是异常的片段:

Caused by: org.hibernate.HibernateException: Enum class not found
at org.hibernate.type.EnumType.setParameterValues(EnumType.java:244)
at org.hibernate.type.TypeFactory.injectParameters(TypeFactory.java:131)
at org.hibernate.type.TypeFactory.custom(TypeFactory.java:214)
... 12 more
Caused by: java.lang.ClassNotFoundException: com.fileManagement.dataDesign.User.UserType
at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:340)
at org.hibernate.internal.util.ReflectHelper.classForName(ReflectHelper.java:171)
at org.hibernate.type.EnumType.setParameterValues(EnumType.java:241)
... 14 more

我试图将UserType枚举放在另一个java文件中并且运行良好,但我真的想将它作为内部枚举放在“用户”类中,因为设计很重要,因为它只能在该类上使用。有可能这样做吗?如果是的话,怎么办呢?

1 个答案:

答案 0 :(得分:8)

正如我的评论中所提到的,尝试将映射文件中的枚举类声明为:

com.fileManagement.dataDesign.User$UserType

通常,如果我们想要访问Hibernate中的任何内部类,那么我们需要使用$符号。