NHibernate不使用自定义SQL插入

时间:2010-11-29 23:58:27

标签: sql nhibernate insert

我有几个NHibernate映射使用sql-insert元素来覆盖本机insert语句,但是我不能让这个使用自定义SQL。无论我用于身份生成器,它都使用NHibernate的标准INSERT语句。这是我的映射文件:

    <?xml version="1.0" encoding="utf-8" ?> 
      <hibernate-mapping xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:xsd="http://www.w3.org/2001/XMLSchema"  
      namespace="MyProject.Domain.Order"
      assembly="MyProject.Domain"
      schema="dbo"
      xmlns="urn:nhibernate-mapping-2.2">
     <class name="OrderBase" table="Order"  
                 polymorphism="explicit" 
                 optimistic-lock="version" 
                 discriminator-value="0">         
         <!--IDENTITY-->
         <id name="Id" access="nosetter.camelcase-underscore" column="intOrderID" type="System.Int32">
    <generator class="native" />
   </id>        
      <!--TYPE DISCRIMINATOR-->
      <discriminator  formula="case when intOrderStatusID = 0 then 0 else 1 end" insert="false" type="System.Int32" />      
      <!--VERSION-->
   <version name="DateUpdated" access="nosetter.camelcase-underscore" column="dtDateUpdated" type="Timestamp" unsaved-value="undefined" />     
      <!--PROPERTIES-->
   <property name="TotalItems" access="nosetter.camelcase-underscore" column="intTotalItems" type="System.Int32"/>
   <property name="TotalQuote" access="nosetter.camelcase-underscore" column="mnyTotalQuote" type="System.Decimal"/>
      <!--ASSOCIATIONS-->
   <many-to-one name="Account" access="nosetter.camelcase-underscore" class="MyProject.Domain.Customer.Account, MyProject.Domain" column="intAccountId" />    
   <sql-insert>exec usp_Order_Insert @p0, @p1, @p2, @p3</sql-insert>
         </class> 
    </hibernate-mapping>

这是NHibernate实际运行的SQL语句:

N'INSERT INTO dbo.Order (dtDateUpdated, intTotalItems, mnyTotalQuote, intAccountId) VALUES (@p0, @p1, @p2, @p3); select SCOPE_IDENTITY()',N'@p0 datetime,@p1 int,@p2 decimal(5,4),@p3 int',@p0='2010-11-29 15:39:02:480',@p1=1,@p2=4.5500,@p3=7777777

这是存储过程:

CREATE PROCEDURE [dbo].[usp_Order_Insert]
 @dtDateUpdated datetime
 , @intTotalItems int
 , @mnyTotalQuote money
 , @intAccountId int
AS

BEGIN

DECLARE @existingID int
 , @existingItems int
 , @existingMoney money

--If the record already exists
IF EXISTS
(
 SELECT * 
 FROM Order
 WHERE intAccountID = @intAccountId
  AND intOrderStatusID = 0
) 
 BEGIN
  --Get the existing record details
  SELECT @existingID = intOrderID
   , @existingItems = intTotalItems
   , @existingMoney = mnyTotalQuote 
  FROM Order
  WHERE intAccountID = @intAccountId
   AND intOrderStatusID = 0
  --Update the existing record
  UPDATE Order
  SET intTotalItems = (@existingItems + @intTotalItems)
   , mnyTotalQuote = (@existingMoney + @mnyTotalQuote)
   , dtDateUpdated = GetDate()
  WHERE intOrderID = @existingID
  --Return the id of the existing record
  SELECT intOrderID
  FROM Order 
  WHERE intOrderID = @existingID
 END
ELSE
 BEGIN 
  --Insert a new record
  INSERT INTO Order 
  (
   intAccountId
   , dtDateUpdated
   , intTotalItems
   , mnyTotalQuote
  ) 
  VALUES
  (
   @intAccountId
   , @dtDateUpdated
   , @intTotalItems
   , @mnyTotalQuote
  )
  --Return the new record id
  SELECT SCOPE_IDENTITY()
 END
END

1 个答案:

答案 0 :(得分:0)

好吧,经过多次惊愕,我发现了问题......

我没有意识到,但我的应用程序实际上是在创建OrderBase子类的新实例。所以我将自定义插入语句移动到nhibernate子类定义中,它就像一个魅力。