前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >为什么nhibernate 不能保存on-to-many的结构

为什么nhibernate 不能保存on-to-many的结构

作者头像
阿新
发布2018-04-12 19:05:23
6890
发布2018-04-12 19:05:23
举报
文章被收录于专栏:c#开发者c#开发者

下面是主类文件

代码语言:javascript
复制
Code

namespace EasyTalk.Module

{

    /// <summary>

    /// SiteAddress object for NHibernate mapped table 'SiteAddress'.

    /// </summary>

    [Serializable]

    public class SiteAddress

    {

        #region Member Variables

        protected string _siteid;

        protected string _alias;

        protected string _address;

        protected string _transporttype;

        protected string _description;

        protected string _iscurrent;

        protected IList<AddressAttributes> _addressattributes;

        #endregion

        #region Constructors

            

        public SiteAddress() {}

                    

        public SiteAddress(string siteid, string alias, string address, string transporttype, string description, string iscurrent) 

        {

            this._siteid= siteid;

            this._alias= alias;

            this._address= address;

            this._transporttype= transporttype;

            this._description= description;

            this._iscurrent= iscurrent;

        }



        public SiteAddress(string siteid)

        {

            this._siteid= siteid;

        }

        

        #endregion

        #region Public Properties

        public  virtual string SiteId

        {

            get { return _siteid; }

            set {_siteid= value; }

        }

        public  virtual string Alias

        {

            get { return _alias; }

            set {_alias= value; }

        }

        public  virtual string Address

        {

            get { return _address; }

            set {_address= value; }

        }

        public  virtual string TransportType

        {

            get { return _transporttype; }

            set {_transporttype= value; }

        }

        public  virtual string Description

        {

            get { return _description; }

            set {_description= value; }

        }

        public  virtual string IsCurrent

        {

            get { return _iscurrent; }

            set {_iscurrent= value; }

        }

        public  virtual IList<AddressAttributes> AddressAttributes

        {

            get { return _addressattributes; }

            set {_addressattributes= value; }

        }



        public virtual void AddItem(AddressAttributes item)

        {

            if (_addressattributes == null)

                _addressattributes = new List<AddressAttributes>();

            _addressattributes.Add(item);

        }

        #endregion

        

        #region Equals And HashCode Overrides

        /// <summary>

        /// local implementation of Equals based on unique value members

        /// </summary>

        public override bool Equals( object obj )

        {

            if( this == obj ) return true;

            if( ( obj == null ) || ( obj.GetType() != this.GetType() ) ) return false;

            SiteAddress castObj = (SiteAddress)obj;

            return ( castObj != null ) &&

            this._siteid == castObj.SiteId;

        }

        /// <summary>

        /// local implementation of GetHashCode based on unique value members

        /// </summary>

        public override int GetHashCode()

        {

            int hash = ;

            hash =  * hash * _siteid.GetHashCode();

            return hash;

        }

        #endregion

        

    }

}

下面是明细

代码语言:javascript
复制
Code

namespace EasyTalk.Module

{

    /// <summary>

    /// AddressAttributes object for NHibernate mapped table 'AddressAttributes'.

    /// </summary>

    [Serializable]

    public class AddressAttributes

    {

        #region Member Variables

        protected decimal _attrid;

        protected string _attributekey;

        protected string _attributevalue;

        protected string _endpointid;

        protected string _transporttype;

        #endregion

        #region Constructors

            

        public AddressAttributes() {}

                    

        public AddressAttributes(string attributekey, string attributevalue, string endpointid, string transporttype) 

        {

            this._attributekey= attributekey;

            this._attributevalue= attributevalue;

            this._endpointid= endpointid;

            this._transporttype= transporttype;

        }



        public AddressAttributes(string endpointid)

        {

            this._endpointid= endpointid;

        }

        

        #endregion

        #region Public Properties

        public  virtual decimal AttrId

        {

            get { return _attrid; }

            set {_attrid= value; }

        }

        public  virtual string AttributeKey

        {

            get { return _attributekey; }

            set {_attributekey= value; }

        }

        public  virtual string AttributeValue

        {

            get { return _attributevalue; }

            set {_attributevalue= value; }

        }

        public  virtual string EndpointId

        {

            get { return _endpointid; }

            set {_endpointid= value; }

        }

        public  virtual string TransportType

        {

            get { return _transporttype; }

            set {_transporttype= value; }

        }

        #endregion

        

        #region Equals And HashCode Overrides

        /// <summary>

        /// local implementation of Equals based on unique value members

        /// </summary>

        public override bool Equals( object obj )

        {

            if( this == obj ) return true;

            if( ( obj == null ) || ( obj.GetType() != this.GetType() ) ) return false;

            AddressAttributes castObj = (AddressAttributes)obj;

            return ( castObj != null ) &&

            this._attrid == castObj.AttrId;

        }

        /// <summary>

        /// local implementation of GetHashCode based on unique value members

        /// </summary>

        public override int GetHashCode()

        {

            int hash = ;

            hash =  * hash * _attrid.GetHashCode();

            return hash;

        }

        #endregion

        

    }

}

配置文件

代码语言:javascript
复制
Code

<?xml version="1.0" encoding="utf-8"?>

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">

  <!--Build: with lujan99@usa.net Nhibernate template-->

  <class name="EasyTalk.Module.SiteAddress,EasyTalk.Module" table="SiteAddress" lazy="true">

      <id name="SiteId" column="Site_ID" type="string" unsaved-value="0">

          <generator class="assigned" />

      </id>

    <property name="Alias" column="Alias" type="string" />

    <property name="Address" column="Address" type="string" />

    <property name="TransportType" column="TransportType" type="string" />

    <property name="Description" column="Description" type="string" />

    <property name="IsCurrent" column="isCurrent" type="string" />

    <bag name="AddressAttributes"   lazy="true"   cascade="all">

      <key column="Endpoint_ID" />

      <one-to-many class="EasyTalk.Module.AddressAttributes,EasyTalk.Module" />

    </bag>

  </class>

</hibernate-mapping>
代码语言:javascript
复制
Code

<?xml version="1.0" encoding="utf-8"?>

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">

  <!--Build: with lujan99@usa.net Nhibernate template-->

  <class name="EasyTalk.Module.AddressAttributes,EasyTalk.Module" table="AddressAttributes" lazy="true">

    <id name="AttrId" column="Attr_ID" type="Decimal" unsaved-value="0">

      <generator class="native" />

    </id>

    <property name="AttributeKey" column="AttributeKey" type="string" />

    <property name="AttributeValue" column="AttributeValue" type="string" />

    <property name="EndpointId" column="Endpoint_ID" type="string" a="true" />

    <property name="TransportType" column="TransportType" type="string" />

  </class>

</hibernate-mapping>

代码

代码语言:javascript
复制
Code

AddressAttributes s = new AddressAttributes();

        //s.Endpoints = e;

        //s.AttrId = 1002;

        s.EndpointId = "";

        s.AttributeKey = "test";

        s.AttributeValue = "test";

        s.TransportType = "msmq";

        //=====================

        //a.Endpoints.Add(e);

        a.AddItem(e);

        //e.AddressAttributes.Add(s);

        e.AddItem(s);



        SiteAddress sa = new SiteAddress();

        sa.Address = "add";

        //sa.AddItem(s);

        sa.Alias = "alias";

        sa.Description = "desc";

        sa.IsCurrent = "";

        sa.SiteId = "";

        sa.TransportType = "msmq";

        



        session.Save(sa);

保存也没有提示错误,但是就是保存不了数据,我单个保存主表也不行,单个保存明细表可以。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2007-12-20 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档