使用nhibernate做update操作时出现如下错误:
此 SqlParameterCollection 的 Count=71 的索引 71 无效。
XML文件
<?
xml version="1.0" encoding="utf-8"
?>
< hibernate-mapping xmlns ="urn:nhibernate-mapping-2.2" >
< class name ="PERSON, Sgidi.Model" table ="PERSON" >
< id name ="PNUMBER" type ="String" unsaved-value ="null" >
< column name ="PNUMBER" length ="50" sql-type ="varchar" not-null ="true" unique ="true" index ="PK_PERSON_1" />
< generator class ="native" />
</ id >
< property name ="roomcode" type ="String" >
< column name ="room_code" length ="10" sql-type ="varchar" not-null ="true" />
</ property >
< many-to-one name ="dept_info" class ="dept_info" column ="room_code" not-found ="ignore" />
</ class >
</ hibernate-mapping >
< hibernate-mapping xmlns ="urn:nhibernate-mapping-2.2" >
< class name ="PERSON, Sgidi.Model" table ="PERSON" >
< id name ="PNUMBER" type ="String" unsaved-value ="null" >
< column name ="PNUMBER" length ="50" sql-type ="varchar" not-null ="true" unique ="true" index ="PK_PERSON_1" />
< generator class ="native" />
</ id >
< property name ="roomcode" type ="String" >
< column name ="room_code" length ="10" sql-type ="varchar" not-null ="true" />
</ property >
< many-to-one name ="dept_info" class ="dept_info" column ="room_code" not-found ="ignore" />
</ class >
</ hibernate-mapping >
有两种解决方法
1.配置文件
<?
xml version="1.0" encoding="utf-8"
?>
< hibernate-mapping xmlns ="urn:nhibernate-mapping-2.2" >
< class name ="PERSON, Sgidi.Model" table ="PERSON" >
< id name ="PNUMBER" type ="String" unsaved-value ="null" >
< column name ="PNUMBER" length ="50" sql-type ="varchar" not-null ="true" unique ="true" index ="PK_PERSON_1" />
< generator class ="native" />
</ id >
< property name ="roomcode" type ="String" insert ="false" update ="false" >
< column name ="room_code" length ="10" sql-type ="varchar" not-null ="true" />
</ property >
< many-to-one name ="dept_info" class ="dept_info" column ="room_code" not-found ="ignore" />
</ class >
</ hibernate-mapping >
< hibernate-mapping xmlns ="urn:nhibernate-mapping-2.2" >
< class name ="PERSON, Sgidi.Model" table ="PERSON" >
< id name ="PNUMBER" type ="String" unsaved-value ="null" >
< column name ="PNUMBER" length ="50" sql-type ="varchar" not-null ="true" unique ="true" index ="PK_PERSON_1" />
< generator class ="native" />
</ id >
< property name ="roomcode" type ="String" insert ="false" update ="false" >
< column name ="room_code" length ="10" sql-type ="varchar" not-null ="true" />
</ property >
< many-to-one name ="dept_info" class ="dept_info" column ="room_code" not-found ="ignore" />
</ class >
</ hibernate-mapping >
但这样的话以后字段roomcode不能进行修改操作
2.配置文件
<?
xml version="1.0" encoding="utf-8"
?>
< hibernate-mapping xmlns ="urn:nhibernate-mapping-2.2" >
< class name ="PERSON, Sgidi.Model" table ="PERSON" >
< id name ="PNUMBER" type ="String" unsaved-value ="null" >
< column name ="PNUMBER" length ="50" sql-type ="varchar" not-null ="true" unique ="true" index ="PK_PERSON_1" />
< generator class ="native" />
</ id >
< property name ="roomcode" type ="String" insert ="false" update ="false" >
< column name ="room_code" length ="10" sql-type ="varchar" not-null ="true" />
</ property >
< many-to-one name ="dept_info" class ="dept_info" column ="room_code" not-found ="ignore"
</ class >
</ hibernate-mapping >
< hibernate-mapping xmlns ="urn:nhibernate-mapping-2.2" >
< class name ="PERSON, Sgidi.Model" table ="PERSON" >
< id name ="PNUMBER" type ="String" unsaved-value ="null" >
< column name ="PNUMBER" length ="50" sql-type ="varchar" not-null ="true" unique ="true" index ="PK_PERSON_1" />
< generator class ="native" />
</ id >
< property name ="roomcode" type ="String" insert ="false" update ="false" >
< column name ="room_code" length ="10" sql-type ="varchar" not-null ="true" />
</ property >
< many-to-one name ="dept_info" class ="dept_info" column ="room_code" not-found ="ignore"
insert
="false"
update
="false"
/>
</ class >
</ hibernate-mapping >
参考资料:
reference:
http://codeverity.com/timweaver/nhibernate-invalid-index-for-this-sqlparametercollection/
http://codeverity.com/timweaver/nhibernate-invalid-index-for-this-sqlparametercollection/