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
SystemUserRolesCode:
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]
SystemRolesCode:
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