I created an inner enum "UserType" inside my class "User", which is used to determine if an instance of a User is a BASIC, a DEPARTMENT_EXCLUSIVE, or a SUPERUSER. Here's a snippet of the code:
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;
}
}
Now, I want to save the instantiated object to my MySQL database using an ORM. I'm using Hibernate. Here's my Hibernate mapping file snippet inside the class tag:
<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>
I ran some tests and an exception was throw, telling that the enum couldn't be found. Here's the test code:
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();
And here's the snippet of the exception stack trace :
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
I tried making the enum as an outer one and it worked well, but I really want to place this inside the "User" class as an inner one due to a design choice, since it's only usable on that class. Is it possible to do that way? If yes, how?
As mentioned in my comments, try declaring the enum class in mapping file as:
com.fileManagement.dataDesign.User$UserType
Generally if we want to access any inner classes in Hibernate then we need to use the $
symbol.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With