具有相同标识符值的不同对象已与会话相关联:0,实体:AppCrashLogData - NHibernate
A different object with the same identifier value was already associated with the session: 0, of entity: AppCrashLogData - NHibernate
我在我的网站中使用 NHibernate Api。当我尝试使用 NHibernate 将值插入到 oracle 数据库时,出现了这个异常。当行插入到 table 时,我有使用触发器和序列递增的 SEQNO 列。所以我不想从前端插入它。我从前端传递了除了 SEQNO 和 APITIME 之外的所有内容。
方法
using (ISession session = OpenSession())
{
ITransaction transaction = session.BeginTransaction();
try
{
foreach (AppCrashLogData ss in list)
{
ss.APITIME = DateTime.Now;
session.Evict(ss);
session.Save(ss);
}
transaction.Commit();
transaction.Dispose();
session.Close();
result = true;
}
catch (Exception ex)
{
transaction.Rollback();
result = false;
}
}
XML
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="CustomerAppWS" namespace="CustomerAppWS.Models">
<class name="AppCrashLogData" table="SLIC_NET_LIFE.CUSS_APP_CRASH_LOG" lazy="true" >
<id name="SEQNO" column="SEQNO" generator="assigned" unsaved-value="0" />
<property name="USERNAME">
<column name="USERNAME" sql-type="VARCHAR2" not-null="false" />
</property>
<property name="EXHEAD">
<column name="EXHEAD" sql-type="VARCHAR2" not-null="false" />
</property>
<property name="EXDETAIL">
<column name="EXDETAIL" sql-type="VARCHAR2" not-null="false" />
</property>
<property name="APPTIME">
<column name="APPTIME" sql-type="DATETIME" not-null="false" />
</property>
<property name="APITIME">
<column name="APITIME" sql-type="DATETIME" not-null="false" />
</property>
</class>
</hibernate-mapping>
AppCrashLogData
public class AppCrashLogData
{
public virtual int SEQNO { get; set; }
public virtual string USERNAME { get; set; }
public virtual string EXHEAD { get; set; }
public virtual string EXDETAIL { get; set; }
public virtual DateTime APPTIME { get; set; }
public virtual DateTime APITIME { get; set; }
}
nH 知道如何插入 ID 由序列驱动的记录。 XML 看起来像:
<id name="SEQNO" column="SEQNO">
<generator class="sequence-identity">
<param name="sequence">NAME_OF_SEQ</param>
</generator>
</id>
它应该使 nH 生成所需的 INSERT .. sequence.nextval RETURNING ... INTO ...
查询
如果您想保留基于触发器的方法,您可以将生成器声明为 class“触发器身份”,以使 nH 生成类似的插入,该插入检索由“插入前”计算的值" 触发器
我在我的网站中使用 NHibernate Api。当我尝试使用 NHibernate 将值插入到 oracle 数据库时,出现了这个异常。当行插入到 table 时,我有使用触发器和序列递增的 SEQNO 列。所以我不想从前端插入它。我从前端传递了除了 SEQNO 和 APITIME 之外的所有内容。
方法
using (ISession session = OpenSession())
{
ITransaction transaction = session.BeginTransaction();
try
{
foreach (AppCrashLogData ss in list)
{
ss.APITIME = DateTime.Now;
session.Evict(ss);
session.Save(ss);
}
transaction.Commit();
transaction.Dispose();
session.Close();
result = true;
}
catch (Exception ex)
{
transaction.Rollback();
result = false;
}
}
XML
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="CustomerAppWS" namespace="CustomerAppWS.Models">
<class name="AppCrashLogData" table="SLIC_NET_LIFE.CUSS_APP_CRASH_LOG" lazy="true" >
<id name="SEQNO" column="SEQNO" generator="assigned" unsaved-value="0" />
<property name="USERNAME">
<column name="USERNAME" sql-type="VARCHAR2" not-null="false" />
</property>
<property name="EXHEAD">
<column name="EXHEAD" sql-type="VARCHAR2" not-null="false" />
</property>
<property name="EXDETAIL">
<column name="EXDETAIL" sql-type="VARCHAR2" not-null="false" />
</property>
<property name="APPTIME">
<column name="APPTIME" sql-type="DATETIME" not-null="false" />
</property>
<property name="APITIME">
<column name="APITIME" sql-type="DATETIME" not-null="false" />
</property>
</class>
</hibernate-mapping>
AppCrashLogData
public class AppCrashLogData
{
public virtual int SEQNO { get; set; }
public virtual string USERNAME { get; set; }
public virtual string EXHEAD { get; set; }
public virtual string EXDETAIL { get; set; }
public virtual DateTime APPTIME { get; set; }
public virtual DateTime APITIME { get; set; }
}
nH 知道如何插入 ID 由序列驱动的记录。 XML 看起来像:
<id name="SEQNO" column="SEQNO">
<generator class="sequence-identity">
<param name="sequence">NAME_OF_SEQ</param>
</generator>
</id>
它应该使 nH 生成所需的 INSERT .. sequence.nextval RETURNING ... INTO ...
查询
如果您想保留基于触发器的方法,您可以将生成器声明为 class“触发器身份”,以使 nH 生成类似的插入,该插入检索由“插入前”计算的值" 触发器