In the meantime I created a Role mapping with a property called RoleName that refers to a RoleName enum. I added unique="true" to prevent duplicate Role entries in the database.
Code:
<hibernate-mapping package="nl.marktmonitor.skillskompas.skillskompas.model">
<class name="Role" table="SM_ROLE">
<id name="roleId" type="integer" column="ROLE_ID">
<generator class="native" />
</id>
<!-- Enum mapping for rolename -->
<property name="roleName" column="ROLENAME" unique="true">
<type name="org.hibernate.type.EnumType">
<param name="enumClass">my.package.RoleName</param>
</type>
</property>
</class>
</hibernate-mapping>
Code:
public enum RoleName {
ADMIN,
USER,
ANOTHERROLENAME;
}
However, Hibernate doesn't store the actual rolename in the database, but the ORDINAL, which is imho not a good idea. If I for example switch the order of ADMIN, USER into USER, ADMIN in my enum, than all my users become admins.
Another problem is that there is no easy way to retrieve the RoleName using the ordinal. There is no such thing as an enum.get(int ordinal) method... I can only loop over the enum values and count to get the value I need.
I'd be happy to know if there is an alternative to this solution.
Thanks, R