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.  [ 5 posts ] 
Author Message
 Post subject: Many-to-many relation with additional column
PostPosted: Tue Oct 31, 2006 7:41 pm 
Beginner
Beginner

Joined: Mon Mar 20, 2006 7:59 am
Posts: 30
Hello,

I want to set up an user authorization system with NHibernate 1.2.0 beta1. I have two relevant classes (here in pseudo syntax):

Code:
class Permission
{
    [PrimaryKey]
    String InternalName{ get; set; }
    String ExternalName{ get; set; }
}

class UserGroup
{
    [PrimaryKey]
    int Id { get; set; }
    String Name { get; set; }
    IList<Permission> GrantedPermissions { get; set; }
    IList<Permission> RevokedPermissions { get; set; }
}


I was thinking of three tables: UserGroups, Permissions, UserGroups_Permissions.

UserGroup_Permissions should contain three columns: (PermissionInternalName, UserGroupId, PermissionType) where PermissionType = 'G' if and only if the permission was granted and PermissionType = 'R' if and only if the permission was revoked.

I tried this with:
Code:
      <bag name="GrantedPermissions" table="UserGroups_Permissions" where="PermissionType = 'G'">
         <key column="UserGroupId" />
         <many-to-many class="Permission" column="PermissionInternalName" />         
      </bag>


Is this scenario possible or do I need two relation tables?

Regards and thanks in advance,
Dominik


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 01, 2006 7:57 am 
Beginner
Beginner

Joined: Fri Oct 20, 2006 8:02 am
Posts: 36
I think You should do something like this:

Code:
<bag
   name='Permissions'
   cascade='all'
   lazy='true'
   inverse='false'
   table='UserGroups_Permissions'
   >
   <key column='UserGroupId' foreign-key='UserGroups' />
   <composite-element class='UserGroups_Permissions' >
      <many-to-one
         name='Permission'
         column='PermissionInternalName'
         class='Permission'
         not-null='true'
      />
      <property name='PermissionType' column='PermissionType' type='Char' />
   </composite-element>
</bag>


and then build your two lists going throw this whole one.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Nov 12, 2006 11:11 am 
Beginner
Beginner

Joined: Mon Mar 20, 2006 7:59 am
Posts: 30
Thank you for this tip, that did the trick :-)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 14, 2006 9:16 pm 
Newbie

Joined: Wed Dec 13, 2006 11:57 pm
Posts: 6
What you are doing very similar to what i did. I will put it in the context of your provided example to illustration the problem I am having. Basically, if you navigate from UserGroup to get UserGroups_Permissions, the composite-id in UserGroups_Permissions class will be empty.

I'm using NH 1.0.3. I suspect it is a bug. Can someone confirm it?

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 18, 2006 3:24 pm 
Regular
Regular

Joined: Fri Feb 03, 2006 5:28 pm
Posts: 73
Location: Québec, QC, Canada
anuarneto wrote:
I think You should do something like this:

Code:
<bag
   name='Permissions'
   cascade='all'
   lazy='true'
   inverse='false'
   table='UserGroups_Permissions'
   >
   <key column='UserGroupId' foreign-key='UserGroups' />
   <composite-element class='UserGroups_Permissions' >
      <many-to-one
         name='Permission'
         column='PermissionInternalName'
         class='Permission'
         not-null='true'
      />
      <property name='PermissionType' column='PermissionType' type='Char' />
   </composite-element>
</bag>


and then build your two lists going throw this whole one.


could you explain your mapping.. I have a very very similar situation.. and I'd like to understand what I'm about to do ! :)

Thanks


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