I am new to NHibernate and have found it extremely interesting. I have been trying to persist objects to a mapping table (the table that breaks a many to many relations in the relational database design) but not been able to do so so far. I am able to retrieve data from the mapping table using my current hibernate-mapping, though.
Here is what my database looks like:
Groups (groupId, groupName) Reports (reportID, reportName) GroupReports(groupId, reportId)
Very typical and simple relations in terms of data modeling.
I know this question has been asked many times and I have read lots of them. Believe me, I really did.
Here is my mapping for the Group entity class:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="DataTransfer" namespace="DataTransfer">
<class name="DataTransfer.Group, DataTransfer" table="GROUPS">
<id name="GroupId">
<column name="groupId" sql-type="int" not-null="true"/>
<generator class="native"/>
</id>
<property name="GroupName" column="groupName" type ="string" length="150" not-null="true"/>
<bag name="Reports" cascade="none" inverse="true">
<key column="groupId"/>
<many-to-many column="reportId" class="DataTransfer.Report, DataTransfer"/>
</bag>
</class>
</hibernate-mapping>
And my mapping for the Report entity class likes below:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="DataTransfer" namespace="DataTransfer">
<class name="DataTransfer.Report, DataTransfer" table="REPORTS" >
<id name="ReportId" column="reportId" type="int" unsaved-value="0">
<generator class="native"/>
</id>
<property name = "ReportName" column="reportName" type="string" length="250" not-null="true"/>
<bag name="MappedGroups" table="GroupReports" cascade="all" inverse="false" lazy="true">
<key column="reportId"/>
<many-to-many class="DataTransfer.Group, DataTransfer" column="groupId" />
</bag>
</class>
</hibernate-mapping>
According to what I have read, these mappings should allow me to both retrieve and persist the mapping data from the associate table GroupReports. My unit test code reveals only select SQL statement, no inserts to the mapping table.
Given my mappings, what should do to persist the mapping data?