In your user mapping:
<set name="Roles" table="UserRoles">
<key column="UserId"/>
<element column="Role"
type="NHibernate.Type.EnumStringType`1[[YourNamespace.RoleEnum,
YourAssembly]]"/>
</set>
your user class:
public class User
{
private ICollection<Role> roles = new HashSet<Role>();
...
public ICollection<Role> Roles { get { return roles; } }
}
You don't need to map roles.
2011/3/9 H.Alex <[email protected]>
> I don't really know if its possible or not. If a user has just one
> role, why not add a column in the user table and give it the enum
> value?
> If the user can have many roles, I would create a roles table and have
> a many to many with users. We did exactly that at work and it worked
> out.
>
> On Mar 9, 7:20 pm, mysterd429 <[email protected]> wrote:
> > Thanks for your reply, Alex. The role table has just one column that
> > I'm using, so I don't really see the point in bringing a table into
> > the domain. Ideally, I'd like something like
> >
> > public enum Role
> > {
> > Module1User = 1,
> > Module2User = 2.
> > Administrator = 3,
> >
> > }
> >
> > public class User
> > {
> > public virtual IList<Role> Roles { set; get; }
> >
> > }
> >
> > If that's not possible, I'll do what you suggested and try the many-to-
> > many, which I think would look like this:
> >
> > public enum RoleCode
> > {
> > Module1User = 1,
> > Module2User = 2.
> > Administrator = 3,
> >
> > }
> >
> > public class Role
> > {
> > public virtual RoleCode { set; get; }
> >
> > }
> >
> > public class User
> > {
> > public virtual IList<Role> Roles { set; get; }
> >
> > }
> >
> > Thanks!
> >
> > On Mar 9, 12:34 pm, "H.Alex" <[email protected]> wrote:
> >
> >
> >
> > > Maybe you could map that as a many-to-many between the user table and
> > > the role table. I think the UserRole table wouldn't end up in your
> > > domain.
> > > I don't think it matters in this case that a user can only have one
> > > role. Anyhow, that's how I would hack it :)
> >
> > > On Mar 8, 8:23 pm, mysterd429 <[email protected]> wrote:
> >
> > > > Hi all,
> >
> > > > I'm not sure how to go about solving this using NHibernate. I have
> an
> > > > enumeration, Role, that corresponds to a lookup table in the
> database,
> > > > ROLE. There's a User class that maps to the USER table. There's
> also
> > > > a table, USER_ROLE, that has a composite primary key of USER_ID and
> > > > ROLE_ID. I'd like for the User class to have an ISet<Role>, which
> > > > would indicate which roles they have.
> >
> > > > Obviously, I could create a new class, UserRole, but I think the more
> > > > OO way would be to use the ISet. Unfortunately, I'm not really sure
> > > > how to get started, since the other side of the "many" is basically
> an
> > > > integer and not a class.
> >
> > > > Thanks!- Hide quoted text -
> >
> > - Show quoted text -
>
> --
> You received this message because you are subscribed to the Google Groups
> "nhusers" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/nhusers?hl=en.
>
>
--
You received this message because you are subscribed to the Google Groups
"nhusers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/nhusers?hl=en.