自定义sql-insert NHibernate

时间:2018-08-23 09:21:24

标签: nhibernate nhibernate-mapping

我有这样的类和映射:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="Test.Program" assembly="Test">
  <joined-subclass name="Program" table="Programs" lazy="false" extends="Test.Program, Test">
    <key column="PROGRAM_ID"/>

    <list name="Events" table="ProgramEvents" lazy="false" cascade="none">
      <key column="PROGRAM_ID" />
      <many-to-many column="EVENT_ID" class="Test.Event, Test" where="ID IS NOT NULL" />
      <sql-insert>INSERT INTO ProgramEvents (PROGRAM_ID, EVENT_ORDER, EVENT_ID, IS_REPEATABLE) VALUES (?, ?, ?, 0)</sql-insert>
      <sql-update>UPDATE ProgramEvents SET EVENT_ID = ?, IS_REPEATABLE = 0 WHERE PROGRAM_ID = ?</sql-update>
      <sql-delete>DELETE ProgramEvents WHERE PROGRAM_ID = ?</sql-delete>
    </list>
  </joined-subclass>

public class Program 
{
   [DataMember]
   public virtual IList<Event> Events { get; set; }
}

目前,我在ProgramEvents表中具有IS_REPEATABLE列,该列始终设置为0。因此我将此类属性添加到我的类中:

public virtual List<Event> RepeatableEvents { get; set; }

现在保存时,必须检查每个事件是否在RepeatableEvents列表的“事件”列表中,并设置值(1或0)。 做这样的事情很棒:

<sql-insert>INSERT INTO ProgramEvents (PROGRAM_ID, EVENT_ORDER, EVENT_ID, IS_REPEATABLE) VALUES (?, ?, ?, RepeatableEvents.Containts(EVENT_ID) ? 1 : 0 )</sql-insert>

是否可以这样做?

PS 我不能只是将IsRepeatable属性添加到Event类。

0 个答案:

没有答案