NHibernate NHibernate使用时误区

NHibernate使用时误区

一.异常:

出现org.hibernate.StaleStateException: Unexpected row count: 0 expected: 1异常的原因:

使用的是hibernate的saveOrUpdate方法保存实例。saveOrUpdate方法要求ID为null时才执行SAVE,在其它情况下执行UPDATE。

在保存实例的时候是新增,但你的ID不为null,所以使用的是UPDATE,但是数据库里没有主键相关的值,所以出现异常。 

二.配置:

<?xml version="1.0"?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
  <session-factory name="test">
    <property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
    <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
    <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
    <property name="connection.connection_string">Data Source=192.168.0.22;Persist Security Info=True;User ID=sa;Password=123456;Initial Catalog=test;</property>
    <property name="show_sql">false</property>
    <mapping assembly="Web.test"/>
  </session-factory>
</hibernate-configuration>

原文地址:https://www.cnblogs.com/zlp520/p/5478027.html