-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Newbie, How to save Many-To-Many using 1.2 and Generics?
PostPosted: Thu Mar 29, 2007 1:09 am 
Beginner
Beginner

Joined: Tue Feb 13, 2007 9:29 pm
Posts: 21
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


Image


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


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 29, 2007 3:24 am 
Beginner
Beginner

Joined: Tue Feb 13, 2007 9:29 pm
Posts: 21
Thanks all, got the prblem solved.

in my httpmodule's Application_EndRequest, it needs to do a session.Flush() before transaction rollback and session.close(). that solved the problem.

As it turns out, I thought I had to do a bi-direct mapping in both mapping files for NHibernate to work, but that's not the case. All i needed was to define the many-to-many one one end, inside of JumptreeForum_Discussion, and everythign will be taken care of.

Still lots of to learn :)

Thanks agin.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.