I've looked everywhere for a clear implementation of this and I'm
either not looking hard enough or I'm missing the point. I have two
tables in a SQL Server database with a pivot table (ID and two foreign
keys) in between them. In a perfect world I would like to be able to
modify/create/delete collections/entities on either side but I'm not
sure how you would map that correctly. Here's what my mappings look
like so far:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
namespace="Domain.DTO"
assembly="Domain.DTO"
schema="dbo">
<class name="Group" table="[tblGroup]" lazy="true">
<id name="ID" column="ID" type="Int32">
<generator class="native" />
</id>
<version name="NHVersion" column="[NHVersion]" unsaved-value="0" /
>
<property name="Name" column="[Name]" type="String" not-
null="true" length="40" />
<bag name="ProgramKeys" table="tblGroupProgramKey" lazy="true"
cascade="all" inverse="false">
<key column="tblGroupID" />
<many-to-many class="Domain.DTO.ProgramKey, Domain.DTO"
column="tblProgramKeyID" />
</bag>
<bag name="Users" table="tblUserGroups" lazy="true" cascade="none"
inverse="true">
<key column="tblGroupID" />
<many-to-many class="Domain.DTO.User, Domain.DTO"
column="tblUserID" />
</bag>
</class>
<class name="User" table="[tblUser]" lazy="true">
<id name="ID" column="ID" type="Int32">
<generator class="native" />
</id>
<version name="NHVersion" column="[NHVersion]" unsaved-value="0" /
>
<property name="UserID" column="[UserID]" type="String" not-
null="true" length="20" />
<bag name="Groups" table="tblUserGroups" lazy="true" cascade="all"
inverse="false">
<key column="tblUserID" />
<many-to-many class="Domain.DTO.Group, Domain.DTO"
column="tblGroupID" />
</bag>
<bag name="ProgramKeysAllowed" table="tblUserProgramKeyAllowed"
lazy="true" cascade="all" inverse="false">
<key column="tblUserID" />
<many-to-many class="Domain.DTO.ProgramKey, Domain.DTO"
column="tblProgramKeyID" />
</bag>
<bag name="ProgramKeyDenied" table="tblUserProgramKeyDenied"
lazy="true" cascade="all" inverse="false">
<key column="tblUserID" />
<many-to-many class="Domain.DTO.ProgramKey, Domain.DTO"
column="tblProgramKeyID" />
</bag>
</class>
<class name="ProgramKey" table="[tblProgramKey]" lazy="true">
<id name="ID" column="ID" type="Int32">
<generator class="native" />
</id>
<version name="NHVersion" column="[NHVersion]" unsaved-value="0" /
>
<property name="ViewNumber" column="[ViewNumber]" type="Int32" not-
null="true" />
<bag name="Groups" table="tblGroupProgramKey" lazy="true"
cascade="none" inverse="true">
<key column="tblProgramKeyID" />
<many-to-many class="Domain.DTO.Group, Domain.DTO"
column="tblGroupID" />
</bag>
<bag name="UsersAllowed" table="tblUserProgramKeyAllowed"
lazy="true" cascade="none" inverse="true">
<key column="tblProgramKeyID" />
<many-to-many class="Domain.DTO.User, Domain.DTO"
column="tblUserID" />
</bag>
<bag name="UsersDenied" table="tblUserProgramKeyDenied"
lazy="true" cascade="none" inverse="true">
<key column="tblProgramKeyID" />
<many-to-many class="Domain.DTO.User, Domain.DTO"
column="tblUserID" />
</bag>
</class>
</hibernate-mapping>
I'm using IList<T>'s for my collections and here's the code I'm
attempting:
session = SessionFactory.OpenSession();
ProgramKey getKey = session.Get<ProgramKey>(id);
session.Delete(getKey);
When I first attempted to do this, it was cascading all over the place
deleting things it shouldn't. I looked around and changed the
mappings to what they are now. It's no longer deleting things it
shouldn't but it's not cascading anything either. I would like to be
able to delete a ProgramKey and have it remove any references to it in
the related pivot tables. Any thoughts on how I could get my many-to-
many collections set up this way? Or are things set up correctly and
this is just a limitation with NHibernate?
Thanks in advance for any help the community can offer.
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---