1

我有一个条目,其中包含如下映射文件中所示的注释列表。

我的场景:

  1. 我为给定条目添加新注释(如 SaveComment 方法中所示)并提交更改
  2. 之后我查询入口域对象的评论,但我的新评论尚未添加

NHibnerate 在这里使用过时的缓存数据吗?我该如何解决这个问题?

using (ITransaction transaction = Session.BeginTransaction())
{
    try
    {
        Entry entry = Session.Load<Entry>(message.EntryId);

        Comment comment = SaveComment(entry, new BroadcastMetadata { some data });

        transaction.Commit();

        // I access the entry.LatestBroadcast info here 
        // but my entry doesnt have the new comment assigned yet !
        var latestData = entry.LatestBroadcast; // is null
    }
}

private Comment SaveComment(Entry entry, BroadcastMetadata broadcastMetadata)
{
    Session.Save(broadcastMetadata);

    var comment = new Comment
    {
        Entry = entry
        OldBroadcastData = entry.LatestBroadcast,
        NewBroadcastData = broadcastMetadata
    };
    Session.Save(comment);

    return comment;
}

我的 C# 课程:

public class Entry
{
    public virtual BroadcastMetadata LatestBroadcast
    {
        get
        {
            BroadcastMetadata latestBroadcast = null;

            Comment broadcastComment = Comments.LastOrDefault();
            if (broadcastComment != null)
            {
                latestBroadcast = broadcastComment.NewBroadcastData;
            }
            else
            {
                latestBroadcast = BroadcastData;
            }

            return latestBroadcast;
        }
    }

    public virtual IList<Comment> Comments { get; protected set; }
}

我的映射:

<class name="Entry" table="`LogbookEntry`">

  <bag name="Comments" table="LogbookComment" lazy="false" inverse="true">
     <key column="EntryId" />
     <one-to-many 
       class="LogbookService.Core.Model.Comment, LogbookService.Core" />
  </bag>

<class name="Comment" table="LogbookComment">

  <many-to-one name="Entry" column="EntryId" fetch="join" lazy="false" cascade="none"
      class="LogbookService.Core.Model.Entry, LogbookService.Core" />

</class>
4

2 回答 2

1

关键是,using {}语句内部的 C# 代码没有将 to 分配CommentEntry. 在这种情况下,NHibernate 仅用于将所有更改发布到数据库中。

其他话:

  • Comment被坚持
  • 它将包含对Entry (EntryId 列将包含正确值)的引用
  • 稍后Entry将被加载,它将包含引用Comment

因此,NHibernate 可以正常工作。

但是,在代码片段中,我们使用的是 C# 代码,它不受持久性更改的影响。所以我们还必须显式地添加CommentEntry集合中。只是一个纯 C# 实体处理:

var comment = new Comment
{
    Entry = entry
    ...
};
entry.Comments.Add(comment)

注意:您正在关闭 lazy settings,我想您有充分的理由这样做。但我至少会用 batch: 扩展包映射<bag name="Comments" ... batch-size="25">。在此处阅读更多信息19.1.5。使用批量获取

于 2013-06-20T03:45:33.483 回答
0

需要注意的一点是,您正在请求事务范围内的最后一条评论,这意味着 NHibernate 尚未处理该事务,并且可能仍在获取缓存数据,尽管它已经提交。尝试这个:

using (ITransaction transaction = Session.BeginTransaction())
     {
        try
        {
           Entry entry = Session.Load<Entry>(message.EntryId);
           Comment comment = SaveComment(entry, new BroadcastMetadata { some data });
           transaction.Commit();
        }
     }

     // i access the entry.LatestBroadcast info here but my entry doesnt have the new comment assigned yet !
     var latestData = entry.LatestBroadcast; // is null

如果这不起作用。通过执行Session.Flush()在提交事务后尝试刷新会话。希望这可以帮助!

于 2013-06-19T22:39:58.987 回答