gpt4 book ai didi

java - 我们可以在 Hibernate 上持久化内部枚举吗?

转载 作者:可可西里 更新时间:2023-11-01 07:20:13 25 4
gpt4 key购买 nike

我在“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

我尝试将枚举作为外部枚举,但效果很好,但由于设计选择,我真的想将它作为内部枚举放在“用户”类中,因为它只能在该类中使用。有可能这样做吗?如果是,如何?

最佳答案

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

com.fileManagement.dataDesign.User$UserType

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

关于java - 我们可以在 Hibernate 上持久化内部枚举吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26064853/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com