NHibernate SaveOrUpdateCopy не будет вставлять сущность с CompositeId - PullRequest
2 голосов
/ 16 июля 2010

У меня есть объект с CompositeId, который не будет вставлять новые строки в базу данных с помощью SaveOrUpdateCopy. Оператор INSERT, сгенерированный NHibernate, заполняется знаком "?" для значения каждого поля. Он отлично вставляется с помощью 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;
                    }
                }
            }

Ответы [ 2 ]

1 голос
/ 17 июля 2010

Добавление свойства версии в класс составного ключа, подробное объяснение см. В этой статье .

0 голосов
/ 07 июля 2016

Привет, я использую Composite с FluentnHibernate, но моя реализация равно и 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");


    }
}

Надеюсь, это поможет вам.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...