-->
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: Subtyping many to many relationships
PostPosted: Thu Nov 23, 2006 7:54 am 
Newbie

Joined: Wed Nov 22, 2006 7:28 am
Posts: 12
I have these tables :

SystemUsers

Code:
CREATE TABLE [dbo].[SystemUsers] (
   [PartyRoleID] [int] NOT NULL ,
   [UserName] [nvarchar] (100) COLLATE Latin1_General_CI_AS NOT NULL ,
) ON [PRIMARY]
GO

ALTER TABLE [dbo].[SystemUsers] ADD
   CONSTRAINT [PK_SystemUsers] PRIMARY KEY  CLUSTERED
   (
      [PartyRoleID]
   )  ON [PRIMARY]
GO



SystemUserRoles

Code:
CREATE TABLE [dbo].[SystemUserRoles] (
   [SystemUserRoleID] [int] IDENTITY (1, 1) NOT NULL ,
   [SystemRoleID] [int] NOT NULL ,
   [PartyRoleID] [int] NOT NULL
) ON [PRIMARY]
GO

ALTER TABLE [dbo].[SystemUserRoles] ADD
   CONSTRAINT [PK_SystemUserRoles] PRIMARY KEY  CLUSTERED
   (
      [SystemUserRoleID]
   )  ON [PRIMARY]



SystemRoles

Code:
CREATE TABLE [dbo].[SystemRoles] (
   [SystemRoleID] [int] IDENTITY (1, 1) NOT NULL ,
   [SystemRoleName] [nvarchar] (255) COLLATE Latin1_General_CI_AS NOT NULL
   ) ON [PRIMARY]
GO

ALTER TABLE [dbo].[SystemRoles] ADD
   CONSTRAINT [PK_SystemRoles] PRIMARY KEY  CLUSTERED
   (
      [SystemRoleID]
   )  ON [PRIMARY]
GO




I need to be able to subtype the relationship between SystemUsers and SystemRoles to add different type of attributes depending on this type.

My mappings are :

For SystemUsers I have this as a collection of SystemUserRole which encapsulates a relationship between a SystemUser and a SystemRole

Code:
<idbag name="_systemUserRoles" table="SystemUserRoles" lazy="true" cascade="save-update" access="field">                  <collection-id column="SystemUserRoleID" type="Int32">
                <generator class="identity"/>
            </collection-id>
            <key column="PartyRoleID" foreign-key="PartyRoles"/>
            <composite-element class="Nop.Prophet.Model.Roles.SystemUserRole, Nop.Prophet">
               <many-to-one name="_systemRole" column="SystemRoleID"
                  class="Nop.Prophet.Model.Roles.SystemRoleBase, Nop.Prophet"
                  access="field"
                  cascade="save-update"
               />         
            </composite-element>
         </idbag>   



Now..I have tried to map SystemUserRole so that I can create subtypes for it but I am not sure what I need to have mapped for this to work.


And this is the mapping for SystemUserRole :

Code:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
   <class name="Nop.Prophet.Model.Roles.SystemUserRole, Nop.Prophet" table="SystemUserRoles">
      <id name="_systemUserRoleID" column="SystemUserRoleID" type="Int32" access="field">
         <generator class="identity" />
      </id>
      <many-to-one name="_partyRole" column="PartyRoleID"
               class="Nop.Prophet.Model.Roles.PartyRoleBase, Nop.Prophet"
               access="field"
               cascade="save-update"
            />
      <many-to-one name="_systemRole" column="SystemRoleID"
               class="Nop.Prophet.Model.Roles.SystemRoleBase, Nop.Prophet"
               access="field"
               cascade="save-update"
            />   
      
      <joined-subclass name="Nop.Prophet.Model.Roles.SponsoredSystemUserRole, Nop.Prophet" extends="Nop.Prophet.Model.Roles.SystemUserRole, Nop.Prophet" table="SponsoredSystemUserRoles">
         <key column="SystemUserRoleID" />
           <property
            name="_sponsorName"
            column="SponsorName"
            type="String"
            length="50"
            access="field" />
         <property
            name="_sponsorEmail"
            column="SponsorEmail"
            type="String"
            length="50"
            access="field" />
      </joined-subclass>
      
      </class>   
</hibernate-mapping>



I seem to be able to retrieve SystemUserRoles correctly but when I add a new relationship by doing so in SystemUser class :

Code:
SystemUserRole systemUserRole = new SystemUserRole();
systemUserRole.SystemRole = role;
systemUserRole.PartyRole = this;
this._systemUserRoles.Add(systemUserRole);


I get the following error : Object must implement IConvertible.


I have been through different ways when trying to implement different types for many to many relationships, could anyone help me how to do this ?

BTW I am a newbie in NHibernate and I am using 1.0.1.0

Cheers


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 23, 2006 10:16 am 
Newbie

Joined: Wed Nov 22, 2006 7:28 am
Posts: 12
Right I managed to go a bit further this time.

Mapping in SystemUsers for the collection of SystemRoles for the user
Code:
<bag name="_systemUserRoles" table="SystemUserRoles" lazy="true" cascade="save-update" access="field">                        
   <key column="PartyRoleID" foreign-key="PartyRoles"/>
   <composite-element class="Nop.Prophet.Model.Roles.SystemUserRole, Nop.Prophet">
      <many-to-one name="_systemRole" column="SystemRoleID"
         class="Nop.Prophet.Model.Roles.SystemRoleBase, Nop.Prophet"
         access="field"
         cascade="save-update"
      />         
</composite-element>


Mapping for SystemUserRoles :

Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
   <class name="Nop.Prophet.Model.Roles.SystemUserRole, Nop.Prophet" table="SystemUserRoles">
      <id name="_systemUserRoleID" column="SystemUserRoleID" type="Int32" access="field">
         <generator class="identity" />
      </id>
      <many-to-one
         name="_systemRole"
         column="SystemRoleID"
         class="Nop.Prophet.Model.Roles.SystemRoleBase, Nop.Prophet"
         access="field"
         />   
               
      <joined-subclass name="Nop.Prophet.Model.Roles.SponsoredSystemUserRole, Nop.Prophet" extends="Nop.Prophet.Model.Roles.SystemUserRole, Nop.Prophet" table="SponsoredSystemUserRoles">
         <key column="SystemUserRoleID" />
           
           <property
            name="_sponsorName"
            column="SponsorName"
            type="String"
            length="50"
            access="field" />
         
         <property
            name="_sponsorEmail"
            column="SponsorEmail"
            type="String"
            length="50"
            access="field" />
            
      </joined-subclass>
     
      </class>
</hibernate-mapping>



I can now retrieve and add roles (type SystemUserRole using an interface called ISystemUserRole).

I even tried to insert a relationship of type SponsoredSystemUserRole, NHibernate didn't fail but it didn't go nowhere near that table, it only updated SystemUserRole table..


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 23, 2006 11:10 am 
Newbie

Joined: Wed Nov 22, 2006 7:28 am
Posts: 12
It seems I've been overcomplicating things quite a bit for nothing!

Mapping for collection of System Roles given to a user :


Code:
<bag name="_systemUserRoles" table="SystemUserRoles" lazy="true" cascade="save-update"
            access="field">
            <key column="PartyRoleID" foreign-key="PartyRoles" />   
            <one-to-many class="Nop.Prophet.Model.Roles.SystemUserRole, Nop.Prophet" />      
</bag>




Mapping for a SystemUserRole relationship between a user and a role :

Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
   <class name="Nop.Prophet.Model.Roles.SystemUserRole, Nop.Prophet" table="SystemUserRoles">
      <id name="_systemUserRoleID" column="SystemUserRoleID" type="Int32" access="field">
         <generator class="identity" />
      </id>
      <many-to-one
         name="_user"
         column="PartyRoleID"
         class="Nop.Prophet.Model.Roles.SystemUser, Nop.Prophet"
         access="field"
         />
      <many-to-one name="_systemRole" column="SystemRoleID" class="Nop.Prophet.Model.Roles.SystemRoleBase, Nop.Prophet"
         access="field" />
      <joined-subclass name="Nop.Prophet.Model.Roles.SponsoredSystemUserRole, Nop.Prophet" extends="Nop.Prophet.Model.Roles.SystemUserRole, Nop.Prophet"
         table="SponsoredSystemUserRoles">
         <key column="SystemUserRoleID" />
         <property name="_sponsorName" column="SponsorName" type="String" length="50" access="field" />
         <property name="_sponsorEmail" column="SponsorEmail" type="String" length="50" access="field" />
      </joined-subclass>
   </class>
</hibernate-mapping>



Hopefully this will help a poor soul in the future!


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.