Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 522391
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:20:57+00:00 2026-05-13T08:20:57+00:00

Ok, i have two entities named WorkPostSheet and WorkPost where the WorkPostSheet contains a

  • 0

Ok, i have two entities named WorkPostSheet and WorkPost where the WorkPostSheet contains a collection of WorkPost-items:

public class WorkPostSheet
{
    ...

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

In my integration test, i notice that nhibernate creates an update statement which I cannot understand. The test:

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

The output:

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

This makes sence since I am creating a user (TestData.CreateAndCommitUser)

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

This makes sence since I am creating a workitem

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

This makes sence as I am creating a workpost sheet

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

This is commited in the WorkPostRepository (workpost sheet is root entity)

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

Now this one I not understand. The foreign is updated with the same value assigned when the entity was first commited. Can you help me understand this?

NHibernate: SELECT workpostsh0_.Id as Id3_1_, workpostsh0_.PeriodFrom as PeriodFrom3_1_, workpostsh0_.PeriodTo as PeriodTo3_1_, workpostsh0_.userId as userId3_1_, user1_.Id as Id0_0_, user1_.UserName as UserName0_0_, user1_.FirstName as 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

Load from repository

NHibernate: SELECT workposts0_.sheetId as sheetId2_, workposts0_.Id as Id2_, workposts0_.Id as Id2_1_, workposts0_.WorkDone as WorkDone2_1_, workposts0_.workItemId as workItemId2_1_, workposts0_.sheetId as sheetId2_1_, workitem1_.Id as Id1_0_, workitem1_.LastChanged as 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

Lazy load

Here are the mapping files:

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T08:20:57+00:00Added an answer on May 13, 2026 at 8:20 am

    Did you set the “inverse” attribute to “true” for the collection side of the association?

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.