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 8078159
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T15:42:11+00:00 2026-06-05T15:42:11+00:00

I am trying to persist an object into the database. This operation should touch

  • 0

I am trying to persist an object into the database. This operation should touch two tables.

[HttpPost]
public ActionResult Create(Report report)
{
    try
    {
        report.Positions = new Iesi.Collections.Generic.HashedSet<Position>();
        var desks = this.session.Query<Desk>().ToList();

        foreach (var desk in desks)
        {
            foreach (var comm in desk.Commodities)
            {
                report.Positions.Add(
                    new Position
                        {
                            Report = report,
                            ReportId = report.Id,
                            Desk = desk,
                            DeskId = desk.Id,
                            Commodity = comm,
                            CommodityId = comm.Id,
                            Value = .0
                        });
            }
        }

        this.session.Save(report);
        return this.RedirectToAction("Index");
    }
    catch
    {
        // some handling
        return this.RedirectToAction("Index");
    }
}

This is my mapping:

public class EntityMapping<TKey, TEntity> : ClassMapping<TEntity>
    where TEntity : Entity<TKey>
{
    public EntityMapping()
    {
        this.Id(x => x.Id, mapper => mapper.Generator(Generators.GuidComb));
    }
}

public class PositionMapping : ClassMapping<Position>
{
    public PositionMapping()
    {
        this.Table("REPORTPOSITIONS");
        this.ComposedId(
            x =>
            {
                x.Property(p => p.ReportId);
                x.Property(p => p.DeskId);
                x.Property(p => p.CommodityId);
            });
        this.Version(x => x.Version, mapper => mapper.Generated(VersionGeneration.Always));
        this.Property(x => x.Value, mapper => mapper.Column("Position"));
    }
}

public class ReportMapping : EntityMapping<Guid, Report>
{
    public ReportMapping()
    {
        this.Table("REPORTS");
        this.Property(x => x.ReportDate, mapper => mapper.Type(NHibernateUtil.Date));
        this.Set(
            x => x.Positions,
            mapper =>
            {
                mapper.Key(km => km.Column("ReportId"));
                mapper.Lazy(CollectionLazy.Lazy);
                mapper.Inverse(true);
                mapper.Cascade(Cascade.All | Cascade.DeleteOrphans);
            },
            rel => rel.OneToMany());
    }
}

and this is the sql used by nhibernate:

INSERT INTO REPORTS
        (ReportDate,
         Id)
VALUES  ('2012-06-11T00:00:00.00' /* @p0_0 */,
         '7f4d8f3d-1175-4713-bd1c-a06d00bfc614' /* @p1_0 */)

INSERT INTO REPORTPOSITIONS
        (Position,
         CommodityId,
         DeskId,
         ReportId)
VALUES  (0 /* @p0_0 */,
         '3a7d80c4-85e9-ba4b-80d2-064f7f0b58b5' /* @p1_0 */,
         'ed7c4e75-7417-a241-a40a-0ff4bfad7172' /* @p2_0 */,
         '00000000-0000-0000-0000-000000000000' /* @p3_0 */)

The ReportId in the sql statement is C#’s default(GUID), because i have not set it in the controller action. NHibernate generates the id for the reports table. When I set the Id in the controller action

report.Id = Guid.NewGuid();

the sql uses different ids as parameters:

INSERT INTO REPORTS
        (ReportDate,
         Id)
VALUES  ('2012-06-11T00:00:00.00' /* @p0_0 */,
         '6164264e-29cd-4d9c-befd-a06d00c2defd' /* @p1_0 */)

INSERT INTO REPORTPOSITIONS
        (Position,
         CommodityId,
         DeskId,
         ReportId)
VALUES  (0 /* @p0_0 */,
         '3a7d80c4-85e9-ba4b-80d2-064f7f0b58b5' /* @p1_0 */,
         'ed7c4e75-7417-a241-a40a-0ff4bfad7172' /* @p2_0 */,
         '594b2206-7c25-4430-af18-5f2643f6c7bf' /* @p3_0 */)

How do I use the id generated by NHibernate for the second table?

UPDATE:

I tried to map the ManyToOne part explicitly

this.ManyToOne(x => x.Report, mapper => mapper.Column("ReportId"));

but with this it does not even try to insert into reportpositions;

When I instead do something like this

this.ManyToOne(x => x.Report, mapper => mapper.Column("foo"));

it creates this sql statement

INSERT INTO REPORTS
        (ReportDate,
         Id)
VALUES  ('2012-06-11T00:00:00.00' /* @p0_0 */,
         '4ff74d49-8749-400c-b079-a06d00e0bee5' /* @p1_0 */)

INSERT INTO REPORTPOSITIONS
        (foo,
         Position,
         CommodityId,
         DeskId,
         ReportId)
VALUES  ('4ff74d49-8749-400c-b079-a06d00e0bee5' /* @p0_0 */,
         0 /* @p1_0 */,
         '3a7d80c4-85e9-ba4b-80d2-064f7f0b58b5' /* @p2_0 */,
         'ed7c4e75-7417-a241-a40a-0ff4bfad7172' /* @p3_0 */,
         '00000000-0000-0000-0000-000000000000' /* @p4_0 */)

now foo has the correct key. Could someone please explain this behavior and offer a solution?

  • 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-06-05T15:42:12+00:00Added an answer on June 5, 2026 at 3:42 pm

    After some fiddling around and climbing NHibernates steep learning curve I came up with this solution.

    public class PositionMapping : ClassMapping<Position>
    {
        public PositionMapping()
        {
            this.Table("REPORTPOSITIONS");
            this.ComposedId(
                x =>
                {
                    x.ManyToOne(p => p.Report, mapper => mapper.Column("ReportId"));
                    x.ManyToOne(p => p.Desk, mapper => mapper.Column("DeskId"));
                    x.ManyToOne(p => p.Commodity, mapper => mapper.Column("CommodityId"));
                });
            this.Version(x => x.Version, mapper => mapper.Generated(VersionGeneration.Always));
            this.Property(x => x.Value, mapper => mapper.Column("Position"));
        }
    }
    

    I declared the ComposedId parts as ManyToOne and removed the Guid properties from the Position entity. NHibernate now populates the Report, Desk and Commodity properties in the Position entity according to table REPORTPOSITIONS and also persits all Positions to this table.

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

Sidebar

Related Questions

I'm trying to persist an object into the database using Cayenne Entity Manager. And
I am trying to persist a Template object in the app's database. It's not
I'm trying to persist an object into a MongoDB, using the following bit of
I am trying to create an Entity manager to persist a object. Here is
I'm trying to persist my object but when I look in the dev console,
i am trying to persist two entities(with one to many relationship) using jpa but
In the code snippet below, I am trying to persist two entities - Account
I'm trying to persist an Array of Arrays to my SQLite database in Rails.
Trying to insert values with Unicode Chars into a MySQL-database using Delphi 2010 and
I am trying to persist a string object in Mysql(5.0.67) using hibernate. My entity

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.