nHibernate在删除子列表之前更新null

时间:2016-03-10 03:49:40

标签: nhibernate

我有以下型号......

public class SLPiece : BaseEntityEditable
{
  public virtual ICollection<SLPieceOrder> SLPieceOrders { get; set; }
}

  <class name="SLPiece" table="P_SL_Piece" lazy="false" dynamic-update="true">

    <id name="Id" column="Id_SL_Piece" type="Int64">
      <generator class="Geraes.Library.Core.Domain.CustomTableHiLoGenerator, Library.Core.Domain" />
    </id>

    <set name="SLPieceOrders" generic="true" lazy="true" cascade="all-delete-orphan" fetch="select" inverse="true">
      <key column="Id_SL_Piece" />
      <one-to-many class="SLPieceOrder"/>
    </set>

  </class>

public class SLPieceOrder : BaseEntity
{
  public virtual SLPiece SLPiece { get; set; }

  public virtual SLPieceProduct SLPieceProduct { get; set; }
}

  <class name="SLPieceOrder" table="P_SL_Piece_Order" lazy="false" dynamic-update="true">

    <id name="Id" column="Id_SL_Piece_Order" type="Int64">
      <generator class="Geraes.Library.Core.Domain.CustomTableHiLoGenerator, Library.Core.Domain" />
    </id>

    <many-to-one name="SLPiece" column="Id_SL_Piece" class="SLPiece" cascade="none" fetch="join" not-null="true"/>

    <many-to-one name="SLPieceProduct" class="SLPieceProduct" column="Id_SL_Piece_Product" lazy="proxy" cascade="all-delete-orphan" fetch="select" unique="true"/>

  </class>

public class SLPieceProduct : BaseEntityEditable
{
  public virtual String CustomerName { get; set; }
}

  <class name="SLPieceProduct" table="P_SL_Piece_Product" lazy="false" dynamic-update="true">

    <id name="Id" column="Id_SL_Piece_Product" type="Int64">
      <generator class="Geraes.Library.Core.Domain.CustomTableHiLoGenerator, Library.Core.Domain" />
    </id>

    <property name="CustomerName" column="Customer_Name" type="String" not-null="false" />

  </class>

当我尝试使用命令删除列表时...

  slPiece.SLPieceOrders.Clear();

生成了Oracle错误...

 UPDATE
    P_SL_Piece_Order 
  SET
    Id_SL_Piece_Product = :p0 
  WHERE
    Id_SL_Piece_Order = :p1;
    :p0 = NULL [Type: Int64 (0)], :p1 = 12325 [Type: Int64 (0)]

ERROR [ADOExceptionReporter] ORA-24381: error(s) in array DML

ORA-01407: cannot update ("PCT_PRD"."P_SL_PIECE_ORDER"."ID_SL_PIECE_PRODUCT") to NULL

将Oracle数据库中的列ID_SL_PIECE_PRODUCT更改为nullable可以解决问题,但我认为这对数据模型没有好处。

EDIT1(日志)

command 0:
    UPDATE
        P_SL_Piece_Order 
    SET
        Id_SL_Piece_Product = :p0 
    WHERE
        Id_SL_Piece_Order = :p1;
    :p0 = NULL [Type: Int64 (0)], :p1 = 12424 [Type: Int64 (0)]
command 1:
    UPDATE
        P_SL_Piece_Order 
    SET
        Id_SL_Piece_Product = :p0 
    WHERE
        Id_SL_Piece_Order = :p1;
    :p0 = NULL [Type: Int64 (0)], :p1 = 12426 [Type: Int64 (0)]

11 10:37:55.362 DEBUG [SQL] Batch commands:
command 0:
    DELETE 
    FROM
        P_SL_Piece_Order 
    WHERE
        Id_SL_Piece_Order = :p0;
    :p0 = 12423 [Type: Int64 (0)]

11 10:37:55.442 DEBUG [SQL] Batch commands:
command 0:
    DELETE 
    FROM
        P_SL_Piece_Product 
    WHERE
        Id_SL_Piece_Product = :p0;
    :p0 = 12322 [Type: Int64 (0)]

11 10:37:55.515 DEBUG [SQL] Batch commands:
command 0:
    DELETE 
    FROM
        P_SL_Piece_Order 
    WHERE
        Id_SL_Piece_Order = :p0;
    :p0 = 12424 [Type: Int64 (0)]
command 1:
    DELETE 
    FROM
        P_SL_Piece_Order 
    WHERE
        Id_SL_Piece_Order = :p0;
    :p0 = 12425 [Type: Int64 (0)]

11 10:37:55.596 DEBUG [SQL] Batch commands:
command 0:
    DELETE 
    FROM
        P_SL_Piece_Product 
    WHERE
        Id_SL_Piece_Product = :p0;
    :p0 = 12323 [Type: Int64 (0)]

11 10:37:55.673 DEBUG [SQL] Batch commands:
command 0:
    DELETE 
    FROM
        P_SL_Piece_Order 
    WHERE
        Id_SL_Piece_Order = :p0;
    :p0 = 12426 [Type: Int64 (0)]

我该如何解决这种情况?

非常感谢你!

EDIT2(我的解决方案)

将all-delete-orphan更改为save-update ...

<id name="Id" column="Id_SL_Piece" type="Int64">
  <generator class="Geraes.Library.Core.Domain.CustomTableHiLoGenerator, Library.Core.Domain" />
</id>

<set name="SLPieceOrders" generic="true" lazy="true" cascade="save-update" fetch="select" inverse="true">
  <key column="Id_SL_Piece" />
  <one-to-many class="SLPieceOrder"/>
</set>

<id name="Id" column="Id_SL_Piece_Order" type="Int64">
  <generator class="Geraes.Library.Core.Domain.CustomTableHiLoGenerator, Library.Core.Domain" />
</id>

<many-to-one name="SLPiece" column="Id_SL_Piece" class="SLPiece" cascade="none" fetch="join" not-null="true"/>

<many-to-one name="SLPieceProduct" class="SLPieceProduct" column="Id_SL_Piece_Product" lazy="proxy" cascade="save-update" fetch="select" unique="true" update="false"/>

另外,添加了属性update =“false”

我需要更改排除代码(不使用机制“one-shot-delete”)...

var slPieceProducts = new List<SLPieceProduct>();

                if (!slPiece.SLPieceOrders.IsNull() && (slPiece.SLPieceOrders.Count() > 0))
                {
                    foreach (var slPieceOrder in slPiece.SLPieceOrders)
                    {
                        if (!slPieceOrder.SLPieceProduct.IsNull() && !slPieceProducts.Contains(slPieceOrder.SLPieceProduct))
                            slPieceProducts.Add(slPieceOrder.SLPieceProduct);

                        slPieceOrder.SLPieceProduct = null;
                    }

                    foreach (var slPieceOrder in slPiece.SLPieceOrders)
                        productionDataService.DeleteSLPieceOrder(slPieceOrder);

                    foreach (var slPieceProduct in slPieceProducts)
                        productionDataService.DeleteSLPieceProduct(slPieceProduct);

                    slPiece.SLPieceOrders.Clear();
                }

0 个答案:

没有答案