1
正常情況下,參數化的SQL對CRUD非常適用,但我有一個實例需要使用存儲過程來執行插入操作。使用存儲過程和NHibernate插入記錄
我HBM文件具有以下(連同一堆其他屬性,箱包等)
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="MyProject.Entities" assembly="MyProject">
<class name="MyProject.Entities.Policy, MyProject" table="dbo.Policy" lazy="true">
<id name="Id" column="[PolicyId]">
<generator class="native" />
</id>
<sql-insert>exec spInsertPolicy ?,?,?,?,?,?,?</sql-insert>
</class>
</hibernate-mapping>
問號在SQL - 將號碼匹配性能和許多-TO-數量一個關係(Id沒有問號)。
我收到以下異常。如果我將生成器切換爲「增量」,它可以工作,但我不想這樣做,因爲其他進程可以保存到數據庫。
NHibernate.HibernateException: The database returned no natively generated identity value
at NHibernate.Id.IdentifierGeneratorFactory.GetGeneratedIdentity(IDataReader rs, IType type, ISessionImplementor session)
at NHibernate.Id.IdentityGenerator.InsertSelectDelegate.ExecuteAndExtract(IDbCommand insert, ISessionImplementor session)
at NHibernate.Id.Insert.AbstractReturningDelegate.PerformInsert(SqlCommandInfo insertSQL, ISessionImplementor session, IBinder binder)
at NHibernate.Persister.Entity.AbstractEntityPersister.Insert(Object[] fields, Boolean[] notNull, SqlCommandInfo sql, Object obj, ISessionImplementor session)
at NHibernate.Persister.Entity.AbstractEntityPersister.Insert(Object[] fields, Object obj, ISessionImplementor session)
at NHibernate.Action.EntityIdentityInsertAction.Execute()
at NHibernate.Engine.ActionQueue.Execute(IExecutable executable)
at NHibernate.Event.Default.AbstractSaveEventListener.PerformSaveOrReplicate(Object entity, EntityKey key, IEntityPersister persister, Boolean useIdentityColumn, Object anything, IEventSource source, Boolean requiresImmediateIdAccess)
有什麼建議嗎?謝謝。
我認爲在執行存儲過程後會被NHibernate自動調用。 你搖滾! – Eric 2010-06-23 15:09:24