tags:

views:

26

answers:

1

hi,the database has two table,one is mb_user table and other is mb_comment table。A user have some comments.When I insert a comment into mb_comment ,the comment_user_id of mb_comment is null.I can't save value of comment_user_id.Please help me!

Map file mb_user Map file

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="MiserBlogData" namespace="MiserBlogData.Entities" default-lazy="false">
  <class name="A_Person" table="mb_user" discriminator-value="0">
    <id name="Id" column ="user_id" >
      <generator class ="native"/>
    </id>
    <discriminator column="user_role" type="int" />
    <property name="State" column="user_state" />
    <property name ="Name" column="user_name" />
    <property name ="Pwd" column="user_pwd" />
    <property name ="CDate" column="user_cdate" />
    <property name ="UDate" column="user_udate" />
    <property name ="Role" column="user_role" />
    <property name ="Face" column="user_face" />
    <bag name="CommentList"  >
      <key column="comment_user_id" foreign-key="FK_PersonComment" />
      <one-to-many class="Comment" />
    </bag>
  </class>
</hibernate-mapping>

mb_comment Map file

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="MiserBlogData" namespace="MiserBlogData.Entities">
  <class name ="Comment" table="mb_comment">
    <id name="Id" column ="comment_id">
      <generator class ="native"/>
    </id>
    <property name ="ArticleId" column="comment_article_id" />
    <property name ="Content" column="comment_content" />
    <property name ="State" column="comment_state" />
    <property name ="CDate" column="comment_cdate" />

    <many-to-one name="Person"  column="comment_user_id" class="A_Person" not-null="true" foreign-key="FK_PersonComment"   />
  </class>
</hibernate-mapping>

C# code

    protected ISession _session = NHibernateHelper.GetCurrentSession();

    public virtual object Save<T>(T obj) where T : class
    {
        return _session.Save(obj);
    }

error :not-null property references a null or transient valueMiserBlogData.Entities.Comment.Person

I know,if I remove not-null="true" in mb_comment file ,system is good.But comment_user_id column is null.How to solve?

A: 

You should assign the property Person on your Comment to a valid A_Person instance. Then NHibernate will know which user id to write in the table.

driis