-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Mapping a simple non-changing list of values
PostPosted: Thu Jul 08, 2010 5:50 am 
Newbie

Joined: Fri Jul 02, 2010 9:00 am
Posts: 9
Hi,

How would a map the following list of "role names" that never change?

ADMIN
USER
COACH


I just need a table called ROLES(id, rolename) that contains these values. Kinda like an ENUM.

Thanks


Last edited by rubenski on Thu Jul 08, 2010 8:30 am, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: Mapping a simple non-changing list of values
PostPosted: Thu Jul 08, 2010 8:25 am 
Newbie

Joined: Fri Jul 02, 2010 9:00 am
Posts: 9
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


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.