NHibernate的 - 尝试更新不需要更新的实体 [英] NHibernate - Tries to update entity that does not need to be updated

查看:245
本文介绍了NHibernate的 - 尝试更新不需要更新的实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个名为Event的实体,其中包含许多报告(一对多),其中包含许多来源(多对多)。

我正面临一个问题我只是无法弄清楚,当我试图更新我的事件,它会尝试更新报告,因为级联(这是很好)
,它会尝试更新连接表上的报表的来源由于级联(这很好),但由于某种原因,它也试图更新源实体,它不应该更新,因为它没有任何变化。

  public class Event 
{
public virtual IList< Report>报告{get;组; }
public virutal int Id {get;组; }
public Event()
{
Reports = new List< Report>();



public class EventMapping:ClassMap< Event>
{
public EventMapping()
{
表(EVENTS);
Id(x => x.Id).Column(ID)。GeneratedBy.Sequence(EVENT_ID_SEQ);

HasMany(x => x.Reports).KeyCoulmn(EVENT_ID)。Cascade.SaveUpdate();



public class Report
{
public virtual int Id {get;组; }
public virtual int Status {get;组; }
公共虚拟IList< Source>来源{get;组; }
$ b $ public Report()
{
Sources = new List< Source>();
}
}

public class ReportMapping:ClassMap< Report>
{
public ReportMapping()
{
表(REPORTS);
Id(x => x.Id).Column(ID)。GeneratedBy.Sequence(DIVUACH_ID_SEQ);
Map(x => x.Status).Column(Status);
HasManyToMany(x => x.Sources).Table(SOURCES_IN_REPORT)。ParentKeyColumn(REPORT_ID)。ChildKeyColumn(KOD_SOURCE)。Cascade.All();



public class Source
{
public virtual int Id {get;组; }
}

public class SourceMapping:ClassMap< Source>
{
public SourceMapping()
{
表(SOURCE);
Id(x => x.Id).Column(ID);






$ b

这是我做什么以及什么时候失败。 / p>

  var eventFromDb = _session.Get< Event>(eventId); 

eventFromDb.Reports.ToList()。ForEach(x => x.Status = GetStatus());

_session.Update(eventFromDb);

任何想法为什么?

解决方案

不需要的更新通常是因为属性的意外更改。 NHibernate有自动更改跟踪和更新所有记录时,属性不会返回完全相同的值分配给它加载时。



看到这个答案:





顺便说一句,你不需要调用更新()。当他们在会话中时,NHibernate会更新已更改的实体。你从数据库中加载的所有东西(例如,在你的情况下用 session.get())在会话中。只要提交交易即可。


so I have an entity named Event which contains many Reports (one to many) which contains many sources (many to many).

I'm facing a problem that I just cant figure out, when I'm trying to update my event, it will try to update the report because of the cascade (which is fine) and it will try to update the report's sources on the join table because of the cascade (which is fine), but for some reason it also tries to update the Source entity, which it shouldn't update because there is no change in it.

public class Event
{
    public virtual IList<Report> Reports { get; set; }
    public virutal int Id { get; set; }
    public Event()
    {
        Reports = new List<Report>();
    }
}

public class EventMapping : ClassMap<Event>
{
    public EventMapping()
    {
        Table("EVENTS");
        Id(x => x.Id).Column("ID").GeneratedBy.Sequence("EVENT_ID_SEQ");

        HasMany(x => x.Reports).KeyCoulmn("EVENT_ID").Cascade.SaveUpdate();
    }
}

public class Report
{
    public virtual int Id { get; set; }
    public virtual int Status { get; set; }
    public virtual IList<Source> Sources { get; set; }

    public Report()
    {
        Sources = new List<Source>();
    }
}

public class ReportMapping : ClassMap<Report>
{
    public ReportMapping()
    {
        Table("REPORTS");
        Id(x => x.Id).Column("ID").GeneratedBy.Sequence("DIVUACH_ID_SEQ");
        Map(x => x.Status).Column("Status");
        HasManyToMany(x => x.Sources).Table("SOURCES_IN_REPORT").ParentKeyColumn("REPORT_ID").ChildKeyColumn("KOD_SOURCE").Cascade.All();
    }
}

public class Source
{
    public virtual int Id { get; set; }
}

public class SourceMapping : ClassMap<Source>
{
    public SourceMapping()
    {
        Table("SOURCE");
        Id(x => x.Id).Column("ID");
    }
}

here is what I do and when it fails.

var eventFromDb = _session.Get<Event>(eventId);

eventFromDb.Reports.ToList().ForEach(x => x.Status = GetStatus());

_session.Update(eventFromDb);

Any idea why?

解决方案

Unwanted updates usually are cause be accidental changes of properties. NHibernate has automatic change tracking and updates all records when the properties do not return the exact same value that is assigned to it when loading.

See this answers:

by the way, you do not need to call update(). NHibernate updates changed entities anyway when they are in the session. Everything you load from the database (e.g. by session.get() in your case) is in the session. Just commit the transaction.

这篇关于NHibernate的 - 尝试更新不需要更新的实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆