李春良

导航

NHibernate one-to-many 插入

最近刚刚接触NHibernate,遇到了one-to-many关系的实体,在更新的时候总是报 “此 OracleParameterCollection 的 Count=10 的索引 10 无效。”的错误,在网上找了好久终于找到答案,特此记录一下。

两个实体类:

LANDSUPPLY_PARCELLAND

using System;
using Iesi.Collections;
using System.Collections.Generic;
using Iesi.Collections.Generic;
using HHSoft.LandSupply.DataEntity.SystemManager;

namespace HHSoft.LandSupply.DataEntity.SourceManager
{
    [Serializable]
    
public class LANDSUPPLY_PARCELLAND : Query
    {
        
#region 私有变量
        
private bool m_isAdded;
        
private bool m_isChanged;
        
private bool m_isDeleted;
        
private string m_parcelland_id;
        
private string m_parcelland_code;
        
private string m_parcelland_address;
        
private float m_parcelland_area;
        
private string m_parcelland_orgcode;
        
private string m_parcelland_islicense;
        
private string m_parcelland_status;
        
private float m_parcelland_gynyarea;
        
private float m_parcelland_jtnyarea;
        
private float m_parcelland_jtwlyarea;
        
private float m_parcelland_jtjsarea;
        
private float m_parcelland_gyjsarea;
        
private string m_parcelland_coordinate;
        
#endregion

        
#region 构造函数
        
/// <summary>
        
/// 默认构造函数
        
/// </summary>
        public LANDSUPPLY_PARCELLAND()
        {
            m_parcelland_id 
= String.Empty;
            m_parcelland_code 
= String.Empty;
            m_parcelland_address 
= String.Empty;
            m_parcelland_area 
= 0;
            m_parcelland_orgcode 
= String.Empty;
            m_parcelland_islicense 
= String.Empty;
            m_parcelland_status 
= String.Empty;
            m_parcelland_gynyarea 
= 0;
            m_parcelland_jtnyarea 
= 0;
            m_parcelland_jtwlyarea 
= 0;
            m_parcelland_jtjsarea 
= 0;
            m_parcelland_gyjsarea 
= 0;
            m_parcelland_coordinate 
= String.Empty;
        }
        
#endregion

        
#region 公共属性

        
/// <summary>
        
/// 行政区编码+自增长ID
        
/// </summary>        
        public virtual string ParcellandId
        {
            
get { return m_parcelland_id; }


            
set { m_parcelland_id = value; }


        }

        
/// <summary>
        
/// 宗地编号
        
/// </summary>        
        public virtual string ParcellandCode
        {
            
get { return m_parcelland_code; }


            
set { m_parcelland_code = value; }


        }

        
/// <summary>
        
/// 宗地位置
        
/// </summary>        
        public virtual string ParcellandAddress
        {
            
get { return m_parcelland_address; }


            
set { m_parcelland_address = value; }


        }

        
/// <summary>
        
/// 宗地面积
        
/// </summary>        
        public virtual float ParcellandArea
        {
            
get;

            
set;


        }

        
/// <summary>
        
/// 组织机构编码
        
/// </summary>        
        public virtual string ParcellandOrgcode
        {
            
get { return m_parcelland_orgcode; }


            
set { m_parcelland_orgcode = value; }


        }

        
/// <summary>
        
/// 是否发证
        
/// </summary>        
        public virtual string ParcellandIslicense
        {
            
get { return m_parcelland_islicense; }


            
set { m_parcelland_islicense = value; }


        }

        
/// <summary>
        
/// 宗地状态:0:未供;1:已供
        
/// </summary>        
        public virtual string ParcellandStatus
        {
            
get { return m_parcelland_status; }


            
set { m_parcelland_status = value; }


        }

        
/// <summary>
        
/// 国有农用地面积
        
/// </summary>        
        public virtual float ParcellandGynyarea
        {
            
get { return m_parcelland_gynyarea; }


            
set { m_parcelland_gynyarea = value; }


        }

        
/// <summary>
        
/// 集体农用地面积
        
/// </summary>        
        public virtual float ParcellandJtnyarea
        {
            
get { return m_parcelland_jtnyarea; }


            
set { m_parcelland_jtnyarea = value; }


        }

        
/// <summary>
        
/// 集体未利用地面积
        
/// </summary>        
        public virtual float ParcellandJtwlyarea
        {
            
get { return m_parcelland_jtwlyarea; }


            
set { m_parcelland_jtwlyarea = value; }


        }

        
/// <summary>
        
/// 集体建设用地面积
        
/// </summary>        
        public virtual float ParcellandJtjsarea
        {
            
get { return m_parcelland_jtjsarea; }


            
set { m_parcelland_jtjsarea = value; }


        }

        
/// <summary>
        
/// 国有建设用地面积
        
/// </summary>        
        public virtual float ParcellandGyjsarea
        {
            
get { return m_parcelland_gyjsarea; }


            
set { m_parcelland_gyjsarea = value; }


        }

        
/// <summary>
        
/// 坐标信息
        
/// </summary>        
        public virtual string ParcellandCoordinate
        {
            
get { return m_parcelland_coordinate; }


            
set { m_parcelland_coordinate = value; }


        }
        
private IList<LANDSUPPLY_PARCELSOURCE> _parcelSourceList = new List<LANDSUPPLY_PARCELSOURCE>();
        
public IList<LANDSUPPLY_PARCELSOURCE> ParcelSourceList { get { return _parcelSourceList; } set { _parcelSourceList = value; } }

        
public AreaModel Area { getset; }

        
/// <summary>
        
/// 返回该实体是否新添加
        
/// </summary>
        public virtual bool IsAdded
        {
            
get { return m_isAdded; }
            
set { m_isAdded = value; }
        }

        
/// <summary>
        
/// 返回该实体是否被修改
        
/// </summary>
        public virtual bool IsChanged
        {
            
get { return m_isChanged; }
            
set { m_isChanged = value; }
        }

        
/// <summary>
        
/// 返回该实体是否标记删除
        
/// </summary>
        public virtual bool IsDeleted
        {
            
get { return m_isDeleted; }
            
set { m_isDeleted = value; }
        }

        
#endregion

        
#region 公共函数
        
/// <summary>
        
/// 重写Equals函数
        
/// </summary>
        public override bool Equals(object obj)
        {
            
if (this == obj) return true;
            
if ((obj == null|| (obj.GetType() != this.GetType())) return false;
            LANDSUPPLY_PARCELLAND castObj 
= (LANDSUPPLY_PARCELLAND)obj;
            
return (castObj != null&& (this.m_parcelland_id == castObj.ParcellandId);
        }

        
/// <summary>
        
/// 重写GetHashCode函数
        
/// </summary>
        public override int GetHashCode()
        {
            
int hash = 57; hash = 27 * hash * m_parcelland_id.GetHashCode();
            
return hash;
        }

        
/// <summary>
        
/// 重写ToString方法
        
/// <summary>
        
/// <returns>主键值</returns>
        public override string ToString()
        {
            
if (this.m_parcelland_id != null)
            {
                
return this.m_parcelland_id.ToString();//此处根据实际修改
            }
            
else
            {
                
return base.ToString();
            }
        }
        
#endregion
    }
}

 LANDSUPPLY_PARCELSOURCE 

View Code
using System;

namespace HHSoft.LandSupply.DataEntity.SourceManager
{

    [Serializable]
    
public class LANDSUPPLY_PARCELSOURCE : Query
    {
        
#region 私有变量
        
private bool m_isAdded;
        
private bool m_isChanged;
        
private bool m_isDeleted;
        
private string m_parcelsource_id;
        
private string m_parcelsource_parcelid;
        
private float m_parcelsource_totalarea;
        
private float m_parcelsource_gynyarea;
        
private float m_parcelsource_jtnyarea;
        
private float m_parcelsource_jtwlyarea;
        
private float m_parcelsource_jtjsarea;
        
private float m_parcelsource_gyjsarea;
        
private string m_parcelsource_sourceid;
        
private string m_source_type;
        
#endregion

        
#region 构造函数
        
/// <summary>
        
/// 默认构造函数
        
/// </summary>
        public LANDSUPPLY_PARCELSOURCE()
        {
            m_parcelsource_id 
= String.Empty;
            m_parcelsource_parcelid 
= String.Empty;
            m_parcelsource_totalarea 
= 0;
            m_parcelsource_gynyarea 
= 0;
            m_parcelsource_jtnyarea 
= 0;
            m_parcelsource_jtwlyarea 
= 0;
            m_parcelsource_jtjsarea 
= 0;
            m_parcelsource_gyjsarea 
= 0;
            m_parcelsource_sourceid 
= String.Empty;
            m_source_type 
= String.Empty;
        }
        
#endregion

        
#region 公共属性

        
/// <summary>
        
/// 自增长ID
        
/// </summary>        
        public virtual string ParcelsourceId
        {
            
get { return m_parcelsource_id; }


            
set { m_parcelsource_id = value; }


        }

        
/// <summary>
        
/// 宗地ID
        
/// </summary>        
        public virtual string ParcelsourceParcelid
        {
            
get { return m_parcelsource_parcelid; }


            
set { m_parcelsource_parcelid = value; }


        }

        
/// <summary>
        
/// 总面积
        
/// </summary>        
        public virtual float ParcelsourceTotalarea
        {
            
get { return m_parcelsource_totalarea; }


            
set { m_parcelsource_totalarea = value; }


        }

        
/// <summary>
        
/// 国有农用地面积
        
/// </summary>        
        public virtual float ParcelsourceGynyarea
        {
            
get { return m_parcelsource_gynyarea; }


            
set { m_parcelsource_gynyarea = value; }


        }

        
/// <summary>
        
/// 集体农用地面积
        
/// </summary>        
        public virtual float ParcelsourceJtnyarea
        {
            
get { return m_parcelsource_jtnyarea; }


            
set { m_parcelsource_jtnyarea = value; }


        }

        
/// <summary>
        
/// 集体未利用地面积
        
/// </summary>        
        public virtual float ParcelsourceJtwlyarea
        {
            
get { return m_parcelsource_jtwlyarea; }


            
set { m_parcelsource_jtwlyarea = value; }


        }

        
/// <summary>
        
/// 集体建设用地面积
        
/// </summary>        
        public virtual float ParcelsourceJtjsarea
        {
            
get { return m_parcelsource_jtjsarea; }


            
set { m_parcelsource_jtjsarea = value; }


        }

        
/// <summary>
        
/// 国有建设用地 面积
        
/// </summary>        
        public virtual float ParcelsourceGyjsarea
        {
            
get { return m_parcelsource_gyjsarea; }


            
set { m_parcelsource_gyjsarea = value; }


        }

        
/// <summary>
        
/// 供地来源ID
        
/// </summary>        
        public virtual string ParcelsourceSourceid
        {
            
get { return m_parcelsource_sourceid; }


            
set { m_parcelsource_sourceid = value; }


        }

        
/// <summary>
        
/// 1:农转征项目;2:国有存量;3:国有未利用地;4:增减挂钩;5:填海造地
        
/// </summary>        
        public virtual SourceType? SourceType
        {
            
get;


            
set;


        }

        
public virtual LANDSUPPLY_PARCELLAND ParcelLand { getset; }

        
/// <summary>
        
/// 返回该实体是否新添加
        
/// </summary>
        public virtual bool IsAdded
        {
            
get { return m_isAdded; }
            
set { m_isAdded = value; }
        }

        
/// <summary>
        
/// 返回该实体是否被修改
        
/// </summary>
        public virtual bool IsChanged
        {
            
get { return m_isChanged; }
            
set { m_isChanged = value; }
        }

        
/// <summary>
        
/// 返回该实体是否标记删除
        
/// </summary>
        public virtual bool IsDeleted
        {
            
get { return m_isDeleted; }
            
set { m_isDeleted = value; }
        }

        
#endregion

        
#region 公共函数
        
/// <summary>
        
/// 重写Equals函数
        
/// </summary>
        public override bool Equals(object obj)
        {
            
if (this == obj) return true;
            
if ((obj == null|| (obj.GetType() != this.GetType())) return false;
            LANDSUPPLY_PARCELSOURCE castObj 
= (LANDSUPPLY_PARCELSOURCE)obj;
            
return (castObj != null&& (this.m_parcelsource_id == castObj.ParcelsourceId);
        }

        
/// <summary>
        
/// 重写GetHashCode函数
        
/// </summary>
        public override int GetHashCode()
        {
            
int hash = 57; hash = 27 * hash * m_parcelsource_id.GetHashCode();
            
return hash;
        }

        
/// <summary>
        
/// 重写ToString方法
        
/// <summary>
        
/// <returns>主键值</returns>
        public override string ToString()
        {
            
if (this.m_parcelsource_id != null)
            {
                
return this.m_parcelsource_id.ToString();//此处根据实际修改
            }
            
else
            {
                
return base.ToString();
            }
        }
        
#endregion
    }
}

对应的配置文件

LANDSUPPLY_PARCELLAND.hbm.xml

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-lazy="false">
  
<class name="HHSoft.LandSupply.DataEntity.SourceManager.LANDSUPPLY_PARCELLAND,HHSoft.LandSupply.DataEntity" table="LANDSUPPLY_PARCELLAND" dynamic-update="true">

    
<id name="ParcellandId" column="PARCELLAND_ID" type="String">
      
<generator class="assigned"/>
    
</id>
    
<property column="PARCELLAND_CODE" type="String" name="ParcellandCode" length="100" />
    
<property column="PARCELLAND_ADDRESS" type="String" name="ParcellandAddress" length="300" />
    
<property column="PARCELLAND_AREA" type="float" name="ParcellandArea" />
    
<property column="PARCELLAND_ORGCODE" type="String" name="ParcellandOrgcode" length="20" />
    
<property column="PARCELLAND_ISLICENSE" type="String" name="ParcellandIslicense" length="1" />
    
<property column="PARCELLAND_STATUS" type="String" name="ParcellandStatus" length="1" />
    
<property column="PARCELLAND_GYNYAREA" type="float" name="ParcellandGynyarea" />
    
<property column="PARCELLAND_JTNYAREA" type="float" name="ParcellandJtnyarea" />
    
<property column="PARCELLAND_JTWLYAREA" type="float" name="ParcellandJtwlyarea" />
    
<property column="PARCELLAND_JTJSAREA" type="float" name="ParcellandJtjsarea" />
    
<property column="PARCELLAND_GYJSAREA" type="float" name="ParcellandGyjsarea" />
    
<property column="PARCELLAND_COORDINATE"  name="ParcellandCoordinate" />
    
<bag cascade="save-update" lazy="false"  name="ParcelSourceList" table="LANDSUPPLY_PARCELSOURCE"   generic="true" >
      
<key column="PARCELSOURCE_PARCELID" foreign-key="FK_PARCELLAND" />
      
<one-to-many class="HHSoft.LandSupply.DataEntity.SourceManager.LANDSUPPLY_PARCELSOURCE,HHSoft.LandSupply.DataEntity" />
    
</bag>
    
<many-to-one name="Area" column="PARCELLAND_ORGCODE" not-null="false" insert="false" update="false"  class="HHSoft.LandSupply.DataEntity.SystemManager.AreaModel,HHSoft.LandSupply.DataEntity" cascade="none"  foreign-key="FK_CustomerOrders" lazy="false"/>
  
</class>
</hibernate-mapping>

LANDSUPPLY_PARCELSOURCE.hbm.xml

 

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-lazy="false">
    
<class name="HHSoft.LandSupply.DataEntity.SourceManager.LANDSUPPLY_PARCELSOURCE,HHSoft.LandSupply.DataEntity" table="LANDSUPPLY_PARCELSOURCE" dynamic-update="true">

        
<id name="ParcelsourceId" column="PARCELSOURCE_ID" type="String">
            
<generator class="assigned"/>
        
</id>
        
<property column="PARCELSOURCE_PARCELID" type="String" name="ParcelsourceParcelid" length="100" />
        
<property column="PARCELSOURCE_TOTALAREA" type="float" name="ParcelsourceTotalarea" />
        
<property column="PARCELSOURCE_GYNYAREA" type="float" name="ParcelsourceGynyarea" />
        
<property column="PARCELSOURCE_JTNYAREA" type="float" name="ParcelsourceJtnyarea" />
        
<property column="PARCELSOURCE_JTWLYAREA" type="float" name="ParcelsourceJtwlyarea" />
        
<property column="PARCELSOURCE_JTJSAREA" type="float" name="ParcelsourceJtjsarea" />
        
<property column="PARCELSOURCE_GYJSAREA" type="float" name="ParcelsourceGyjsarea" />
        
<property column="PARCELSOURCE_SOURCEID" type="String" name="ParcelsourceSourceid" length="100" />
        
<property column="SOURCE_TYPE"  name="SourceType"  />
    
<many-to-one column="PARCELSOURCE_PARCELID" name="ParcelLand" not-null="false" cascade="none" lazy="false" class="HHSoft.LandSupply.DataEntity.SourceManager.LANDSUPPLY_PARCELLAND,HHSoft.LandSupply.DataEntity" foreign-key="FK_PARCELLAND"/>
  
</class>
</hibernate-mapping>

这样配置之后就一直报错,无法更新parcelLand类。

解决方法:

修改LANDSUPPLY_PARCELSOURCE.hbm.xml中many-to-one节点,添加insert="false" update="false"

 

 <many-to-one column="PARCELSOURCE_PARCELID" name="ParcelLand" not-null="false" cascade="none" lazy="false" insert="false" update="false" class="HHSoft.LandSupply.DataEntity.SourceManager.LANDSUPPLY_PARCELLAND,HHSoft.LandSupply.DataEntity" foreign-key="FK_PARCELLAND"/>

这样就能正常更新了。

posted on 2011-06-21 11:10  李春良  阅读(901)  评论(0编辑  收藏  举报