简体   繁体   English

NHibernate SaveOrUpdateCopy不会插入带有CompositeId的实体

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

I have an entity with a CompositeId that won't insert new rows to the database using SaveOrUpdateCopy. 我有一个带有CompositeId的实体,该实体不会使用SaveOrUpdateCopy将新行插入数据库。 The INSERT statement generated by NHibernate is populated with "?" NHibernate生成的INSERT语句填充有“?” for the value of every field. 每个领域的价值。 It inserts fine with SaveOrUpdate, it updates fine with either SaveOrUpdateCopy or SaveOrUpdate, and any entity without a CompositeId inserts/updates fine with SaveOrUpdateCopy. 它使用SaveOrUpdate进行插入,使用SaveOrUpdateCopy或SaveOrUpdate进行更新,没有CompositeId的任何实体都通过SaveOrUpdateCopy进行插入/更新。 I don't want to create an if/then looking for entities with CompositeId to decide if it should use SaveOrUpdate or SaveOrUpdateCopy. 我不想创建一个if / then,然后使用CompositeId查找实体来决定是否应使用SaveOrUpdate或SaveOrUpdateCopy。 Is there some trick to getting SaveOrUpdateCopy to work with entities with CompositeId? 是否有一些技巧可以使SaveOrUpdateCopy与具有CompositeId的实体一起使用?

Here's the code (names changed to protect the innocent): 这是代码(为了保护无辜者而更改名称):

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");
        }

Database call: 数据库调用:

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;
            }
        }

Database save: 数据库保存:

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

将版本属性添加到组合键类,有关详细说明,请参阅本文

Hi I use compositeId with FluentnHibernate but my implementation of the Equals and GetHashCode is different. 嗨,我在FluentnHibernate中使用了CompositeId,但是我对EqualsGetHashCode的实现却不同。 This one class that have 7 fields as Key: 这一类有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");


    }
}

I hope this will help you. 我希望这能帮到您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM