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.  [ 3 posts ] 
Author Message
 Post subject: many-to-many or one-to-many of enums
PostPosted: Sat Mar 08, 2008 5:35 am 
Newbie

Joined: Sat Mar 08, 2008 5:15 am
Posts: 2
My database contains 3 tables, users, user_roles, and roles. So essentially, there's a many to many relationship between users and roles. The roles table only contains a unique name column with an int id pk.

In my code, I've defined Roles as int enum with the appropriate values that exist in the database. Is it possible to make a domain object User contain a collection of these enum Roles? I can't seem to get it to work with either many-to-many or one-to-many. Any suggestions? Will I have to convert my enum Roles into a class?

Here's a snippet of my failure from my User.hbm.xml:

Code:
    <set name="Roles" table="user_roles" fetch="subselect">
      <key column="user_id" />
      <one-to-many class="MyDomain.Role, MyAssembly" />
    </set>


My conclusion is that my approach will not be possible.


Top
 Profile  
 
 Post subject: many-to-many or one-to-many of enums
PostPosted: Sat Mar 08, 2008 10:29 am 
Senior
Senior

Joined: Thu Jun 21, 2007 8:03 am
Posts: 127
Location: UK
Hi,

I think your conclusion is correct; that you will need a class, and nut just an enum, to map the Role to the database. I suspect the best course would be to have a Role class, and a many-to-many set.

You also mentioned that the roles table has a name column. This fuels the argument for having a class that maps to it - unless the name column isn't actually used.

You might be able to have static properties on your Role class to make it look like an Enum in use - that might make it closer to your original intention.

Regards,
Richard


Top
 Profile  
 
 Post subject: Re: many-to-many or one-to-many of enums
PostPosted: Sat Mar 08, 2008 4:06 pm 
Newbie

Joined: Sat Mar 08, 2008 5:15 am
Posts: 2
FlukeFan wrote:
Hi,

I think your conclusion is correct; that you will need a class, and nut just an enum, to map the Role to the database. I suspect the best course would be to have a Role class, and a many-to-many set.

You also mentioned that the roles table has a name column. This fuels the argument for having a class that maps to it - unless the name column isn't actually used.

You might be able to have static properties on your Role class to make it look like an Enum in use - that might make it closer to your original intention.

Regards,
Richard


Thanks for the confirmation. Ya, the name column isn't really used except to do reporting from the sql side. As of right now, it's the name column contains the same spelling as the entries in my enum Roles counterpart. But I'll go ahead and convert it to a class then.

You think the NHibernate team would ever want to build in a feature to support enums in this fashion?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.