2010-07-16 2 views
2

SaveOrUpdateCopy를 사용하여 데이터베이스에 새 행을 삽입하지 않는 CompositeId가있는 엔티티가 있습니다. NHibernate에 의해 생성 된 INSERT 문은 "?" 모든 필드 값 그것은 SaveOrUpdate와 함께 잘 삽입하고 SaveOrUpdateCopy 또는 SaveOrUpdate로 잘 업데이트되며 SaveOrUpdateCopy를 사용하여 CompositeId 삽입/업데이트를하지 않은 엔티티도 정상적으로 업데이트됩니다. 나는 ComposOld를 사용하여 엔티티를 찾고 ifOrUpdate 또는 SaveOrUpdateCopy를 사용해야하는지 결정하고 싶지 않습니다. SaveOrUpdateCopy가 CompositeId가있는 엔티티와 함께 ​​작동하도록하는 데있어 몇 가지 트릭이 있습니까?NHibernate SaveOrUpdateCopy는 CompositeId와 함께 엔티티를 삽입하지 않습니다.

 
public class MyEntity 
    { 
     public virtual Int32 FirstProperty { get; set; } 
     public virtual string SecondProperty { get; set; } 
     public virtual string DataText { get; set; } 

     public override int GetHashCode() 
     { 
      int hashCode = 0; 
      hashCode = hashCode^FirstProperty.GetHashCode()^
         SecondProperty.GetHashCode(); 
      return hashCode; 
     } 

     public override bool Equals(object obj) 
     { 
      MyEntity toCompare = obj as MyEntity; 
      if(toCompare == null) 
      { 
       return false; 
      } 
      return (GetHashCode() != toCompare.GetHashCode()); 
     } 
    } 
 
public MyEntityMap() 
     { 
      CompositeId() 
       .KeyProperty(x => x.FirstProperty, "first_property") 
       .KeyProperty(x => x.SecondProperty, "second_property"); 

      Map(x => x.DataText, "data_text") 
       .Nullable(); 

      Table("dbo.my_entity"); 
     } 

데이터베이스 전화 : 여기

가 (이름이 무죄를 보호하기 위해 변경) 코드의

 
public MyEntity GetMyEntity(long firstProperty, string secondProperty) 
     { 
      using (var session = sessionFactory.OpenSession()) 
      { 
       var result = from entity in 
           session.Linq() 
          where entity.FirstProperty == firstProperty 
            && entity.SecondProperty== secondProperty 
          select entity; 
       return result.Count() > 0 ? result.First() : null; 
      } 
     } 

데이터베이스 저장 :

 
using (var session = sessionFactory.OpenSession()) 
      { 
       using (var transaction = session.BeginTransaction()) 
       { 
        try 
        { 
         session.SaveOrUpdateCopy(entity); 
         transaction.Commit(); 
        } 
        catch(Exception ex) 
        { 
         transaction.Rollback(); 
         throw; 
        } 
       } 
      } 

답변

1

이 버전 추가 속성을 C로 omposite 키 클래스에 대한 자세한 설명은 this article을 참조하십시오.

+0

내가 제안한 링크에서 제안 된 솔루션을 조사했지만 nhibernate xml 스키마에서 버전 요소가 허용되지 않습니다. 내가 잘못하고 있습니다? 감사 – Hoghweed

0

안녕 나는 FluentnHibernate와 compositeId를 사용하지만 내 구현GetHashCode이 다른 같음. 키로 7 개의 필드가있는이 클래스는

public class PresupuestoGastoPromocion 

{ 
    public PresupuestoGastoPromocion() { } 

    public virtual int Año { get; set; } 
    public virtual int Mes { get; set; } 
    public virtual int PartidaId { get; set; } 
    public virtual string Equipo { get; set; } 
    public virtual string CodFamilia { get; set; } 
    public virtual string GDP { get; set; } 
    public virtual int TipoPresupuestoId { get; set; } 
    public virtual float Monto { get; set; } 
    public override bool Equals(object obj) 
    { 
     if (obj == null) 
      return false; 
     var t = obj as PresupuestoGastoPromocion; 
     if (t == null) 
      return false; 
     if (CodFamilia == t.CodFamilia && Año == t.Año && Mes == t.Mes && TipoPresupuestoId == t.TipoPresupuestoId && Equipo == t.Equipo && PartidaId == t.PartidaId && GDP == t.GDP) 
      return true; 
     return false; 
    } 
    public override int GetHashCode() 
    { 
     return (CodFamilia + "|" + Año + "|" + Mes + "|" + TipoPresupuestoId + "|" + Equipo + "|" + PartidaId + "|" + GDP).GetHashCode(); 
    } 

} 



public class PresupuestoGastoPromocionMap : ClassMap<PresupuestoGastoPromocion> 
{ 
    public PresupuestoGastoPromocionMap() 
    { 
     Table("PresupuestoGastoPromocion"); 
     CompositeId() 
      .KeyProperty(x => x.Año) 
      .KeyProperty(x => x.Mes) 
      .KeyProperty(x => x.TipoPresupuestoId) 
      .KeyProperty(x => x.CodFamilia, "Cod_Familia") 
      .KeyProperty(x => x.Equipo) 
      .KeyProperty(x => x.GDP) 
      .KeyProperty(x => x.PartidaId); 

     Map(x => x.Monto).Column("Monto"); 


    } 
} 

이 정보가 도움이되기를 바랍니다.

관련 문제