<sql-insert>用于子类</sql-insert>

时间:2009-11-10 19:32:31

标签: c# nhibernate stored-procedures

我的对象映射如下:

<class name="A" table="TableA">
    <id name="ID" column="AId" type="Int32" unsaved-value="0">
        <generator class="native" />
    </id>
    <discriminator column="Type" type="Int32" />
    <property name="Description" />
</class>

<subclass name="B" discriminator-value="0" extends="A">
    <property name="B_specific_properties />
</subclass>

<subclass name="C" discriminator-value="1" extends="A">
    <property name="C_specific_properties />
</subclass>

我打算使用存储过程来执行插入,目前B和C都使用相同的存储过程插入到表中。我的问题是上面映射的对象,sql-insert是否属于A类?如果是,是否期望在对象A上执行save()?如何在保存时识别B和C的附加属性?

OR

使用一个存储过程为两个子类保存一个不可能完成的任务,虽然它们属于同一个表吗?

非常感谢您的任何意见!

1 个答案:

答案 0 :(得分:0)

我会告诉你我是怎么做到的

<class name="CompanyCharacteristic" table="firma_cecha" abstract="true" discriminator-value="not null">
    <id name="Id" column="id_firma_cecha">
        <generator class="native" />
    </id>
    <discriminator column="typ_cechy"/>
    <property name="FriendlyTypeName" column="typ_cechy" insert="false" update="false"/>

    <property name="All" column="cecha_all" access="nosetter.camelcase"/>

    <!-- Auditing data -->
    <property name="UserInserted" access="nosetter.camelcase" column="user_insert" insert="true" update="false"/>
    <property name="DateInserted" column="data_insert" generated="insert" insert="false" update="false"/>

    <subclass discriminator-value="Akceptacja kart płatniczych" name="CreditCardAcceptanceCharacteristic">
        <property name="DoesIt" column="cecha_dec_1" not-null="true" type="DataAccess.NHibernate.Infrastructure.UserTypes.EnumValueInfoUserType`1[[Domain.Characteristics.YesNoNumericEnum, Domain]], DataAccess"/>
        <sql-insert>
            DECLARE @returned decimal(20,0)
            exec sp_FirmaCecha_Add
            @TypCechy = 'Akceptacja kart płatniczych',
            @CechaAll = ?,
            @UserInsertZrodlo = ?,
            @CechaDec1 = ?,
            @IdFirmaCecha = @returned OUTPUT
            SELECT @returned
        </sql-insert>
        <sql-update>
            exec sp_FirmaCecha_Update
            @TypCechy = 'Akceptacja kart płatniczych',
            @UserUpdateZrodlo = ?,
            @CechaDec1 = ?,
            @IdFirmaCecha = ?
        </sql-update>
        <sql-delete>
            exec sp_FirmaCecha_Delete ?
        </sql-delete>
    </subclass>

    <sql-insert>
        raiserror ('CompanyInfoCharacteristic nie moze byc dodwana za pomocą NHibernate',11,1)
    </sql-insert>
    <sql-update>
        raiserror ('CompanyInfoCharacteristic nie moze byc aktualizowana za pomocą NHibernate.',11,1)
    </sql-update>
    <sql-delete>
        raiserror ('CompanyInfoCharacteristic nie moze byc kasowana za pomocą NHibernate.',11,1)
    </sql-delete>

正如您所看到的,每个子类都需要拥有自己的sql语句。基类也可以有自己的语句。在这个例子中,我不允许通过NHibernate

修改Base类实例