Hello,
I'm a newbie and I have an issue with my many-to-many mapping. It's saving to the "parent" table, but not to the "middle" table. I'm using 1.2 Beta with sql server 2000.
here is my db diagram
Here is my Mapping file for "JumptreeForum_Discussions" table and class
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="Jumptree.Forum.BusinessEntities"
namespace="Jumptree.Forum.BusinessEntities"
>
<class name="JumptreeForum_Discussions" lazy="false">
<id name="DiscussionID">
<generator class="native" />
</id>
<property name="DiscussionTitle" />
<property name="DiscussionLastPostedOn" />
<property name="DiscussionDefaultComment" />
<property name="CreatedBy" />
<property name="CreatedByIP" />
<property name="CreatedOn" />
<property name="UpdatedBy" />
<property name="UpdatedOn" />
<bag name="Categories" table="JumptreeForum_DiscussionsCategories">
<key column="DiscussionID" />
<many-to-many class="JumptreeForum_Categories" column="CategoryID" />
</bag>
</class>
</hibernate-mapping>
Here is my mapping file for "JumptreeForum_Categories"
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="Jumptree.Forum.BusinessEntities"
namespace="Jumptree.Forum.BusinessEntities"
>
<class name="JumptreeForum_Categories" lazy="false">
<id name="CategoryID">
<generator class="native" />
</id>
<property name="ProjectCategoryName" />
<property name="CreatedBy" />
<property name="CreatedOn" />
<property name="UpdatedBy" />
<property name="UpdatedOn" />
<bag name="Discussions" table="JumptreeForum_DiscussionsCategories" inverse="true">
<key column="CategoryID" />
<many-to-many class="JumptreeForum_Discussions" column="DiscussionID" />
</bag>
</class>
</hibernate-mapping>
As you can see, I did a bi-direction mapping. JumptreeForum_Categories table have a list of pre-defined categories
In the corresponding JumptreeForum_Discussions class, I have
Code:
private IList<JumptreeForum_Categories> categories = new List<JumptreeForum_Categories>();
public IList<JumptreeForum_Categories> Categories
{
get { return categories; }
set { categories = value; }
}
In my ASP.NET web page I did someting like
Code:
JumptreeForum_Discussions discussion = new JumptreeForum_Discussions();
JumptreeForum_Categories category = new JumptreeForum_Categories();
//the cateogory id got from checkbox on my form
category.CategoryID = Convert.ToInt32(categoryCheckBox_hf.Value);
discussion.Categories.Add(category);
discussion.DiscussionTitle = subject_txt;
discussion.CreatedBy = name_txt;
discussion.DiscussionDefaultComment = content;
discussion.CreatedByIP = Request.UserHostAddress;
discussion.CreatedOn = System.DateTime.Now;
discussion.UpdatedBy = discussion.CreatedBy;
discussion.UpdatedOn = discussion.CreatedOn;
ExampleApplication.GetCurrentSession().Save(discussion);
The result is the discussion got saved into the "JumptreeForum_Discussions", but no association to the JumptreeForum_DIscussionCategories table is saved.
I'm obviously doing something wrong, please advise. Thanks