簡體   English   中英

NHibernate SaveOrUpdateCopy不會插入帶有CompositeId的實體

[英]NHibernate SaveOrUpdateCopy won't insert entity with CompositeId

我有一個帶有CompositeId的實體,該實體不會使用SaveOrUpdateCopy將新行插入數據庫。 NHibernate生成的INSERT語句填充有“?” 每個領域的價值。 它使用SaveOrUpdate進行插入,使用SaveOrUpdateCopy或SaveOrUpdate進行更新,沒有CompositeId的任何實體都通過SaveOrUpdateCopy進行插入/更新。 我不想創建一個if / then,然后使用CompositeId查找實體來決定是否應使用SaveOrUpdate或SaveOrUpdateCopy。 是否有一些技巧可以使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;
                    }
                }
            }

將版本屬性添加到組合鍵類,有關詳細說明,請參閱本文

嗨,我在FluentnHibernate中使用了CompositeId,但是我對EqualsGetHashCode的實現卻不同。 這一類有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");


    }
}

我希望這能幫到您。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM