nhibernate:为什么nhibernate更新我的子实体的外键?

时间:2010-01-06 21:07:32

标签: nhibernate profiling

好的,我有两个名为WorkPostSheet和WorkPost的实体,其中WorkPostSheet包含一组WorkPost项:

public class WorkPostSheet
{
    ...

    public virtual IEnumerable<WorkPost> WorkPosts
    {
        get;
        private set;
    }
}

在我的集成测试中,我注意到nhibernate创建了一个我无法理解的更新语句。测试:

    [Test]
    public void Commit_CreateNewWorkPostSheetWithWorkPosts_WorkPostsPersisted()
    {
        using (IUnitOfWork unitOfWork = IsolatingFactory.CreateReadOnlyUnitOfWork())
        {
            // arrange
            WorkPostSheetRepository repository = new WorkPostSheetRepository(IsolatingFactory);
            WorkItemRepository workItemRepository = new WorkItemRepository(IsolatingFactory);

            DateTime from = new DateTime(2000, 1, 1);
            DateTime to = new DateTime(2000, 2, 1);
            WorkPostSheet sheetWithWorkPosts = WorkPostSheet.Create(TestData.CreateAndCommitUser("min", "Marius", "Ingjer", IsolatingFactory), new TimePeriod(from, to));

            WorkItem workItemToPostWorkOn = WorkItem.Create(0, "A");
            workItemRepository.Commit(workItemToPostWorkOn);

            WorkPost workPost = sheetWithWorkPosts.Add(workItemToPostWorkOn, from);

            // act
            repository.Commit(sheetWithWorkPosts);
            unitOfWork.Session.Flush();
            unitOfWork.Session.Clear();

            WorkPostSheet sheetWithWorkPostsFromDb = repository.Get(sheetWithWorkPosts.Id, false);

            // assert
            CollectionAssert.Contains(sheetWithWorkPostsFromDb.WorkPosts, workPost);
        }
    }

输出:

  

NHibernate:INSERT INTO“User”(UserName,FirstName,LastName)VALUES(@ p0,@ p1,@ p2); select last_insert_rowid(); @ p0 ='min',@ p1 ='Marius',@ p2 ='Ingjer'

因为我正在创建一个用户(TestData.CreateAndCommitUser)

  

NHibernate:INSERT INTO“WorkItem”(LastChanged,Description,Id)VALUES(@ p0,@ p1,@ p2); @ p0 = 06.01.2010 21:50:25,@ p1 ='A',@ p2 = 0

这是因为我正在创建一个工作项

  

NHibernate:INSERT INTO“WorkPostSheet”(PeriodFrom,PeriodTo,userId)VALUES(@ p0,@ p1,@ p2); select last_insert_rowid(); @ p0 = 01.01.2000 00:00:00,@ p1 = 01.02.2000 00:00:00,@ p2 = 1

这样我就可以创建一个工作点表了

  

NHibernate:INSERT INTO“WorkPost”(WorkDone,workItemId,sheetId)VALUES(@ p0,@ p1,@ p2); select last_insert_rowid(); @ p0 = 0,@ p1 = 0,@ p2 = 1

这是在WorkPostRepository中提交的(workpost表是根实体)

  

NHibernate:UPDATE“WorkPost”SET sheetId = @ p0 WHERE Id = @ p1; @ p0 = 1,@ p1 = 1

现在这个我不明白。使用在第一次提交实体时分配的相同值更新外部。你能帮我理解一下吗?

  

NHibernate:SELECT workpostsh0_.Id为Id3_1_,workpostsh0_.PeriodFrom为PeriodFrom3_1_,workpostsh0_.PeriodTo为PeriodTo3_1_,workpostsh0_.userId为userId3_1_,user1_.Id为Id0_0_,user1_.UserName为UserName0_0_,user1_.FirstName为FirstName0_0_,user1_。 LastName as LastName0_0_ FROM“WorkPostSheet”workpostsh0_ inner join“User”user1_ on workpostsh0_.userId = user1_.Id WHERE workpostsh0_.Id=@p0; @ p0 = 1

从存储库加载

  

NHibernate:选择workposts0_.sheetId为sheetId2_,workposts0_.Id为Id2_,workposts0_.Id为Id2_1_,workposts0_.WorkDone为WorkDone2_1_,workposts0_.workItemId为workItemId2_1_,workposts0_.sheetId为sheetId2_1_,workitem1_.Id为Id1_0_,workitem1_。 LastChanged作为LastChan2_1_0_,workitem1_.Description as Descript3_1_0_ FROM“WorkPost”workposts0_ inner join“WorkItem”workitem1_ on workposts0_.workItemId = workitem1_.Id WHERE workposts0_.sheetId =@p0; @ p0 = 1

延迟加载

以下是映射文件:

sealed class WorkPostClassMap : ClassMap<WorkPost>
{
    public WorkPostClassMap()
    {
        Not.LazyLoad();

        Id(post => post.Id).GeneratedBy.Identity().UnsavedValue(0);
        Map(post => post.WorkDone);
        References(post => post.Item).Column("workItemId").Not.Nullable();
        References(Reveal.Property<WorkPost, WorkPostSheet>("Owner"), "sheetId").Not.Nullable();
    }
}

sealed class WorkPostSheetClassMap : ClassMap<WorkPostSheet>
{
    public WorkPostSheetClassMap()
    {
        Id(sheet => sheet.Id).GeneratedBy.Identity().UnsavedValue(0);
        Component(sheet => sheet.Period, period =>
                                             {
                                                 period.Map(p => p.From, "PeriodFrom");
                                                 period.Map(p => p.To, "PeriodTo");
                                             });
        References(sheet => sheet.Owner, "userId").Not.Nullable();
        HasMany(sheet => sheet.WorkPosts).KeyColumn("sheetId").AsBag();
    }
}

1 个答案:

答案 0 :(得分:1)

您是否为关联的收集方将“inverse”属性设置为“true”?

请参阅:http://ayende.com/Blog/archive/2009/01/18/nh-prof-new-feature-superfluous-ltmany-to-onegt-update.aspx