oracle存储过程:
create or replace PROCEDURE "PD_CUSTOMER" ( p_VERSION IN CUSTOMER.VERSION%TYPE,p_CUSTOMERID IN CUSTOMER.CUSTOMERID%type ) IS BEGIN DELETE FROM CUSTOMER WHERE CUSTOMERID = p_CUSTOMERID and VERSION= p_version; END ;
<?xml version="1.0" encoding="utf-8" ?> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernateSample.Domain" namespace="NHibernateSample.Domain"> <class name="Customer" table="Customer" > <id name="Id" column="CustomerId" unsaved-value="0"> <generator class="native" ></generator> </id> <version name="Version" column="Version" type="Int32" unsaved-value="0"></version> <component name="Name" class="NHibernateSample.Domain.Name,NHibernateSample.Domain" > <property name="FirstName" column="FirstName" type="string" length="50" ></property> <property name="LastName" column="LastName" type="string" length="50"></property> </component> <set name="Orders" table="`Order`" generic="true" > <key column="Customer" foreign-key="FK_CustomerOrders"></key> <one-to-many class="NHibernateSample.Domain.Order,NHibernateSample.Domain"/> </set> <sql-delete>exec pd_customer?,?</sql-delete> <!--<property name="FirstName"></property> <property name="LastName"></property>--> <!--<property name="Version"></property>--> </class> </hibernate-mapping>
数据访问层代码:
public Customer GetCustomerById(int customerid) { return _session.Get<Customer>(customerid); }
public void DeleteCustomerTransction(Customer cus) { using (ITransaction tx =_session.BeginTransaction()) { try { _session.Delete(cus); _session.Flush(); tx.Commit(); } catch (NHibernate.HibernateException) { tx.Rollback(); throw; } } }
测试方法:
[Test] public void DeleteCustomerTest() { var customer = _sample.GetCustomerById(1); _sample.DeleteCustomerTransction(customer); var testcustomer = _sample.GetCustomerById(1); Assert.IsNull(testcustomer); }
报错信息
------ Test started: Assembly: NHibernateSample.Test.dll ------ NHibernate: SELECT customer0_.CustomerId as CustomerId2_0_, customer0_.Version as Version2_0_, customer0_.FirstName as FirstName2_0_, customer0_.LastName as LastName2_0_ FROM Customer customer0_ WHERE customer0_.CustomerId=:p0;:p0 = 1 [Type: Int32 (0)] NHibernate: UPDATE "Order" SET Customer = null WHERE Customer = :p0;:p0 = 1 [Type: Int32 (0)] NHibernate: exec pd_customer:p0,:p1;:p0 = 1 [Type: Int32 (0)], :p1 = 1 [Type: Int32 (0)] Test 'NHibernateSample.Test.CRUDWithTransctionTest.DeleteCustomerTest' failed: NHibernate.Exceptions.GenericADOException : could not delete: [NHibernateSample.Domain.Customer#1][SQL: exec pd_customer?,?] ----> System.Data.OracleClient.OracleException : ORA-00900: ?? SQL ?? 在 NHibernate.Persister.Entity.AbstractEntityPersister.Delete(Object id, Object version, Int32 j, Object obj, SqlCommandInfo sql, ISessionImplementor session, Object[] loadedState) 在 NHibernate.Persister.Entity.AbstractEntityPersister.Delete(Object id, Object version, Object obj, ISessionImplementor session) 在 NHibernate.Action.EntityDeleteAction.Execute() 在 NHibernate.Engine.ActionQueue.Execute(IExecutable executable) 在 NHibernate.Engine.ActionQueue.ExecuteActions(IList list) 在 NHibernate.Engine.ActionQueue.ExecuteActions() 在 NHibernate.Event.Default.AbstractFlushingEventListener.PerformExecutions(IEventSource session) 在 NHibernate.Event.Default.DefaultFlushEventListener.OnFlush(FlushEvent event) 在 NHibernate.Impl.SessionImpl.Flush() CRUDWithTransction.cs(54,0): 在 NHibernateSample.DataAccess.CRUDWithTransction.DeleteCustomerTransction(Customer cus) CRUDWithTransctionTest.cs(71,0): 在 NHibernateSample.Test.CRUDWithTransctionTest.DeleteCustomerTest() --OracleException 在 System.Data.OracleClient.OracleConnection.CheckError(OciErrorHandle errorHandle, Int32 rc) 在 System.Data.OracleClient.OracleCommand.Execute(OciStatementHandle statementHandle, CommandBehavior behavior, Boolean needRowid, OciRowidDescriptor& rowidDescriptor, ArrayList& resultParameterOrdinals) 在 System.Data.OracleClient.OracleCommand.ExecuteNonQueryInternal(Boolean needRowid, OciRowidDescriptor& rowidDescriptor) 在 System.Data.OracleClient.OracleCommand.ExecuteNonQuery() 在 NHibernate.AdoNet.AbstractBatcher.ExecuteNonQuery(IDbCommand cmd) 在 NHibernate.Persister.Entity.AbstractEntityPersister.Delete(Object id, Object version, Int32 j, Object obj, SqlCommandInfo sql, ISessionImplementor session, Object[] loadedState) 0 passed, 1 failed, 0 skipped, took 1.93 seconds (NUnit 2.5.5).
以前也遇到过,不知道是不是同一个问题
怎么解决?
@杀猪三刀:
无效的sql语句,检查一下sql有没有拼错。
@angelshelter: sql语句都是在存储过程中,而且存储过程可以正常运行
@杀猪三刀: 那你是不是以存储过程的方式运行的???我记得以前java分slq,预编译,存储过程3种方式执行。。。