0
sql-insert要素を使用してネイティブなinsert文をオーバーライドするNHibernateマッピングがいくつかありますが、カスタムSQLを使用するためには取得できません。アイデンティティジェネレータに何を使用しても、NHibernateの標準INSERT文が使用されます。ここに私のマッピングファイルがあります:カスタムSQLインサートを使用しないNHibernate
<?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>
ここでは、SQL文NHibernateのは、実際に実行されます:
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