NHibernate cascading save

后端 未结 2 941
夕颜
夕颜 2021-01-07 05:22

This is trying to insert null into Comment.BlogArticleID.

The following GenericADOException appeared: \"could not insert: [NHibernate__OneToMany.BO.Comment][SQL: INS

相关标签:
2条回答
  • 2021-01-07 05:53

    you haven't mapped the Article to the Comment:

    <hibernate-mapping
      xmlns="urn:nhibernate-mapping-2.2"
      assembly="NHibernate__OneToMany.BO"
      namespace="NHibernate__OneToMany.BO"
      default-access="property">
    
      <class name="Comment" table="Comment">
        <id name="ID">
          <generator class="native" />
        </id>
    
        <property name="Name" />
        <many-to-one name="BlogArticle" column="BlogArticleID" />   <----------
      </class>
    </hibernate-mapping>
    
    public class Comment
        {
            private int _id;
            public virtual int ID
            {
                get { return _id; }
                set { _id = value; }
            }
    
            public Comment() { }
    
            public Comment(int id, string name, BlogArticle article) <------------
            {
                this._id = id;
                this._name = name;
                this._blogArticle = article; <------------
            }
    
            private string _name;
            public virtual string Name
            {
                get { return _name; }
                set { _name = value; }
            }   
    
            private BlogArticle _blogArticle;       <------------
            public virtual BlogArticle Name       <------------
            {
                get { return _blogArticle; }       <------------
                set { _blogArticle= value; }       <------------
            }   
    
        }
    
    0 讨论(0)
  • 2021-01-07 06:09

    The key column of the associated class (Comment.BlogArticleID) must be nullable in the database. NHibernate will insert rows leaving this column NULL and then perform an update to set the key.

    Adding not-null="true" to the key element would not work, as this attribute is only used by the schema export tool.

    Note that the failing insert includes a select for the generated identity for the new child row.

    0 讨论(0)
提交回复
热议问题