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

  • Home
  • SEARCH
  • 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 8245997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T22:21:52+00:00 2026-06-07T22:21:52+00:00

I’ve a One to Many mapping. An entity with a collection persisted on two

  • 0

I’ve a One to Many mapping.
An entity with a collection persisted on two table 1:N

This is the class and the mapping:

public class Test
    {
        public virtual string Id { get; set; }
        public virtual string Description { get; set; }
        public virtual IList<TestItem> Items { get; set; }

        public Test()
        {
            Items = new List<TestItem>();
        }
    }

    public class TestItem
    {
        public virtual string Id { get; set; }
        public virtual Test Test { get; set; }
        public virtual string ItemCode { get; set; }
        public virtual string ItemData { get; set; }
    }

    public class TestMap : ClassMapping<Test>
    {
        public TestMap()
        {
            Id(x => x.Id, m => m.Column("IDTest"));

            Bag(x => x.Items, c =>
            {
                c.Key(k =>
                {
                    k.NotNullable(true);
                    k.Column("IDTest");
                });
                c.Cascade(Cascade.DeleteOrphans);
                c.Lazy(CollectionLazy.NoLazy);
                c.Inverse(true);
            }, r => r.OneToMany(m =>
            {
                m.NotFound(NotFoundMode.Exception);
                m.Class(typeof(TestItem));
            }));
        }
    }

    public class TestItemMap : ClassMapping<TestItem>
    {
        public TestItemMap()
        {
            Id(x => x.Id, m => m.Column("IDTestItem"));

            ManyToOne(x => x.Test, m =>
            {
                m.Column("IDTest");
                m.NotNullable(false);
                m.Lazy(LazyRelation.NoLazy);
            });

            Property(x => x.ItemCode);
            Property(x => x.ItemData);
        }
    }

And this is the code.
If I remove the marked line I get the error.

var session = SessionFactory.OpenSession();

            using (var tr = session.BeginTransaction())
            {
                var test = new Test();
                test.Id = "T01";
                test.Description = "Desc TEST 01";

                session.SaveOrUpdate(test); // If Removed Get INSERT ERROR on TestItem

                var item = new TestItem { Id = "NEW01", ItemCode = "A", Test = test, ItemData = "New T01A" };
                test.Items.Add(item);
                session.SaveOrUpdate(item);

                session.SaveOrUpdate(test);

                tr.Commit();
            }

My question is:

Is this the best practice to persist a “one to many” relation ?

It is possible to use the code below and save all only saving the header Row (test) and automatically cascading all inserts on the child table??

var session = SessionFactory.OpenSession();

            using (var tr = session.BeginTransaction())
            {
                var test = new Test();
                test.Id = "T01";
                test.Description = "Desc TEST 01";

                //session.SaveOrUpdate(test); // If Removed Get INSERT ERROR on TestItem

                var item = new TestItem { Id = "NEW01", ItemCode = "A", Test = test, ItemData = "New T01A" };
                test.Items.Add(item);
                //session.SaveOrUpdate(item);

                session.SaveOrUpdate(test);

                tr.Commit();
            }

I suppose I need to change something on my mapping code but i do not undestand what!!!

Many days of work, googling, stackoverflowing but nothing change the result.

Thank you!!!

  • 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-07T22:21:54+00:00Added an answer on June 7, 2026 at 10:21 pm

    Q: Is this the best practice to persist a “one to many” relation ?

    A: If you’re talking about a “composition” relationship, the cascading approach can be a good choice.

    Q: It is possible to use the code below and save all only saving the header Row (test) and automatically cascading all inserts on the child table??

    A: Yes it is possible. But your code should look like this:

    public class TestMap : ClassMapping<Test>
    {
        public TestMap()
        {
            Id(x => x.Id, m => m.Column("IDTest"));
    
            Bag(x => x.Items, c =>
            {
                c.Key(k =>
                {
                    k.NotNullable(true);
                    k.Column("IDTest");
                });
                c.Cascade(Cascade.All | Cascade.DeleteOrphans); //<- My suggestion
                c.Lazy(CollectionLazy.NoLazy);
                c.Inverse(true);
            }, r => r.OneToMany(m =>
            {
                m.NotFound(NotFoundMode.Exception);
                m.Class(typeof(TestItem));
            }));
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim
Does anyone know how can I replace this 2 symbol below from the string
I have two tables with like below codes: Table: Accounts id | username |
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
I am reading a book about Javascript and jQuery and using one of the

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.