Hello,
I'm still fairly new to NHibernate, and we are attempting to build an application which allows many-to-many associations between several entities. My question is, what's the best way to accomplish this in the mapping? Has anyone else tried this or can direct me to a sample app with similar parameters?
Below, I'll attempt the describe the key areas of what we have. So far, I've found that I have to use not-found="ignore" because NHibernate enforces referential integrity. Also, using the cascade features seem unreliable for updates in this situation (possibly due to my mappings). Right now, I create and insert each object and then create and insert the association, and this works fine. But I would prefer to use the cascade features if I could.
Thanks in advance for your help.
Here's the table structure. It's hosted on MSSQL2005. All Id fields are GUID's:
[Association] ParentId ChildId
[Person] Id
[Location] Id
[Document] Id
Each POCO classes looks something like this:
class Person{ Guid Id {get;set;} IList<Person> Persons {get;private set} IList<Location> Locations {get;private set} IList<Document> Documents{get;private set} }
Below is a mapping file for the Person entity:
<?xml version="1.0" encoding="utf-8" ?> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Model" namespace="Model.DAO"> <class name="Person"> <id name="Id" type="System.Guid" column="Id"> <generator class="guid.native"/> </id> <bag name="Persons" table="Association"> <key column="ParentId"/> <many-to-many class="Person" column="ChildId" not-found="ignore"> </bag>
<bag name="Locations" table="Association" cascade="none"> <key column="ParentId"/> <many-to-many class="Location" column="ChildId" not-found="ignore"/> </bag>
<bag name="Documents" table="Association" cascade="none"> <key column="ParentId" /> <many-to-many class="Document" column="ChildId" not-found="ignore"/> </bag>
</class>
</hibernate-mapping>
|