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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T09:20:47+00:00 2026-05-12T09:20:47+00:00

My model contains a class Section which has an ordered list of Statics that

  • 0

My model contains a class Section which has an ordered list of Statics that are part of this section. Leaving all the other properties out, the implementation of the model looks like this:

public class Section
{
    public virtual int Id { get; private set; }
    public virtual IList<Static> Statics { get; private set; }
}

public class Static
{
    public virtual int Id { get; private set; }
}

In the database, the relationship is implemented as a one-to-many, where the table Static has a foreign key pointing to Section and an integer column Position to store its index position in the list it is part of.

The mapping is done in Fluent NHibernate like this:

public SectionMap()
{
    Id(x => x.Id);
    HasMany(x => x.Statics).Cascade.All().LazyLoad()
            .AsList(x => x.WithColumn("Position"));
}

public StaticMap()
{
    Id(x => x.Id);
    References(x => x.Section);
}

Now I am able to load existing Statics, and I am also able to update the details of those. However, I cannot seem to find a way to add new Statics to a Section, and have this change persisted to the database. I have tried several combinations of:

  • mySection.Statics.Add(myStatic)
  • session.Update(mySection)
  • session.Save(myStatic)

but the closest I have gotten (using the first two statements), is to an SQL exception reading: “Cannot insert the value NULL into column ‘Position'”. Clearly an INSERT is attempted here, but NHibernate does not seem to automatically append the index position to the SQL statement.

What am I doing wrong? Am I missing something in my mappings? Do I need to expose the Position column as a property and assign a value to it myself?

EDIT: Apparently everything works as expected, if I remove the NOT NULL constraint on the Static.Position column in the database. I guess NHibernate makes the insert and immediatly after updates the row with a Position value.

While this is an anwers to the question, I am not sure if it is the best one. I would prefer the Position column to be not nullable, so I still hope there is some way to make NHibernate provide a value for that column directly in the INSERT statement.

Thus, the question is still open. Any other solutions?

  • 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-12T09:20:47+00:00Added an answer on May 12, 2026 at 9:20 am

    When using a bidirectional one-to-many relationship in NHibernate one of the ends must be “inverse”. Best practice is to set the end with the collection as inverse, since that avoids unnecessary SQL statements and allows the id column to be “not null”.

    In section 6.4 of the documentation you can find the following note:

    Very Important Note: If the column of a association is declared NOT NULL, NHibernate may cause constraint violations when it creates or updates the association. To prevent this problem, you must use a bidirectional association with the many valued end (the set or bag) marked as inverse=”true”. See the discussion of bidirectional associations later in this chapter.

    So, you need to add .Inverse() to your HasMany mapping in SectionMap.

    public SectionMap()
    {
        Id(x => x.Id);
        HasMany(x => x.Statics)
            .Cascade.All()
            .LazyLoad()
            .Inverse()
            .AsList(x => x.WithColumn("Position"));
    }
    

    You would also probably want an Add and Remove method on Section, which sets/resets the reference of the static as well as adding/removing the static to/from its own collection:

    public virtual void AddStatic(Static static)
    {
        Statics.Add(static);
        static.Section = this;
    }
    
    
    public virtual void RemoveStatic(Static static)
    {
        Statics.Remove(static);
        static.Section = null;
    }
    

    These methods makes sure the references are kept accurate on both sides of the relationship.

    According to section 6.8 of the docs NHibernate does not support bidirectional relationships when using indexed collections:

    Please note that NHibernate does not support bidirectional one-to-many associations with an indexed collection (list, map or array) as the “many” end, you have to use a set or bag mapping.

    So, if you are still having trouble, consider using a unidirectional relationship instead of a bidirectional, however that might mean that your foreign key column needs to be nullable (according to the note in the beginning of the post). Otherwise you might have to map your collection as a bag or set instead of a list.

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

Sidebar

Ask A Question

Stats

  • Questions 277k
  • Answers 277k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You will have to use NSURLConnection. This is fairly straightforward,… May 13, 2026 at 2:54 pm
  • Editorial Team
    Editorial Team added an answer I posted the same issue in the IIS forum and… May 13, 2026 at 2:54 pm
  • Editorial Team
    Editorial Team added an answer The solution is to add a call to ValidationSummaryOnSubmit() after… May 13, 2026 at 2:54 pm

Related Questions

I have a Character model and a Link model. The Link model represents a
I'm working on a web site which contains sections that need to be secured
I have three modules in my Maven project (this is slightly simplified): model contains
I'm trying to think of a naming convention that accurately conveys what's going on

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.