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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T04:59:04+00:00 2026-05-11T04:59:04+00:00

I’m currently using NHibernate as my data access layer, using Fluent NHibernate to create

  • 0

I’m currently using NHibernate as my data access layer, using Fluent NHibernate to create the mapping files for me. I have two classes, TripItem and TripItemAttributeValue, which have a many-to-many relation between them.

The mapping is as follows:

public class TripItemMap : ClassMap<TripItem2> {     public TripItemMap()     {         WithTable('TripItemsInt');         NotLazyLoaded();          Id(x => x.ID).GeneratedBy.Identity().WithUnsavedValue(0);         Map(x => x.CreateDate, 'CreatedOn').CanNotBeNull();         Map(x => x.ModifyDate, 'LastModified').CanNotBeNull();          /* snip */          HasManyToMany<TripItemAttributeValue>(x => x.Attributes).AsBag()             .WithTableName('TripItems_TripItemAttributeValues_Link')             .WithParentKeyColumn('TripItemId')             .WithChildKeyColumn('TripItemAttributeValueId')             .LazyLoad();     } }  public class TripItemAttributeValueMap : ClassMap<TripItemAttributeValue> {     public TripItemAttributeValueMap()     {         WithTable('TripItemAttributeValues');          Id(x => x.Id).GeneratedBy.Identity();         Map(x => x.Name).CanNotBeNull();          HasManyToMany<TripItem2>(x => x.TripItems).AsBag()             .WithTableName('TripItems_TripItemAttributeValues_Link')             .WithParentKeyColumn('TripItemAttributeValueId')             .WithChildKeyColumn('TripItemId')             .LazyLoad();     } } 

At some point in my application I fetch existing attributes from the database, add them to tripItem.Attributes, then save the tripItem object. In the end, the TripItems_TripItemAttributeValues_Link never gets any new records, resulting in the relations not being persisted.

If it helps, these are the mapping files generated by Fluent NHibernate for these classes:

<?xml version='1.0' encoding='utf-8'?> <hibernate-mapping xmlns='urn:nhibernate-mapping-2.2' default-lazy='true' assembly='ETP.Core' namespace='ETP.Core.Domain'>   <class name='TripItem2' table='TripItemsInt' xmlns='urn:nhibernate-mapping-2.2' lazy='false'>     <id name='ID' column='ID' type='Int32' unsaved-value='0'>       <generator class='identity' />     </id>     <property name='CreateDate' column='CreatedOn' type='DateTime' not-null='true'>       <column name='CreatedOn' />     </property>     <property name='ModifyDate' column='LastModified' type='DateTime' not-null='true'>       <column name='LastModified' />     </property>     <bag name='Attributes' lazy='true' table='TripItems_TripItemAttributeValues_Link'>       <key column='TripItemId' />       <many-to-many column='TripItemAttributeValueId' class='ETP.Core.Domain.TripItemAttributeValue, ETP.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' />     </bag>   </class> </hibernate-mapping> 

and

<?xml version='1.0' encoding='utf-8'?> <hibernate-mapping xmlns='urn:nhibernate-mapping-2.2' default-lazy='true' assembly='ETP.Core' namespace='ETP.Core.Domain'>   <class name='TripItemAttributeValue' table='TripItemAttributeValues' xmlns='urn:nhibernate-mapping-2.2'>     <id name='Id' column='Id' type='Int32'>       <generator class='identity' />     </id>     <property name='Name' column='Name' length='100' type='String' not-null='true'>       <column name='Name' />     </property>     <bag name='TripItems' lazy='true' table='TripItems_TripItemAttributeValues_Link'>       <key column='TripItemAttributeValueId' />       <many-to-many column='TripItemId' class='ETP.Core.Domain.TripItem2, ETP.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' />     </bag>   </class> </hibernate-mapping> 

What am I doing wrong here ?

  • 1 1 Answer
  • 3 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. 2026-05-11T04:59:04+00:00Added an answer on May 11, 2026 at 4:59 am

    @efdee

    I was having the same problem and spent almost two days on this. I had a many to many relationship and the link table wasn’t being updated either. I’m new to NHibernate, just trying to learn it so take everything I say with a grain of salt.

    Well it turned out that it’s not Fluent NHibernate, nor the mapping, but me not understanding how NHibernate works with many-to-many. In a many-to-many relationship if collections on both entities aren’t populated, NHibernate doesn’t persist data to the link table.

    Let’s say I have this entities in a many-to-many relationship :

     partial class Contact {    public string ContactName {get; set;}    public IList Locations {get; set;}  }  partial class Location {    public string LocationName {get; set;}    public string LocationAddress {get;set;}    public IList Contacts {get;set;} }  

    when I add to a location to Contact.Locations, I have to make sure that the contact is also present inside location.Contacts.

    so to add a location i have this method inside my Contact class.

     public void AddLocation(Location location)         {             if (!location.Contacts.Contains(this))             {                 location.Contacts.Add(this);             }             Locations.Add(location);         } 

    This seems to have solved my problem, but like I said I’m just picking up NHibernate and learning it, may be there’s a better way. If anyone has a better solution, please post.

    This is the post that pointed me to check both collections: http://www.coderanch.com/t/217138/Object-Relational-Mapping/link-table-of-ManyToMany-annotation

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

Sidebar

Ask A Question

Stats

  • Questions 156k
  • Answers 156k
  • 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 can use a grid to add layers to your… May 12, 2026 at 10:49 am
  • Editorial Team
    Editorial Team added an answer Yes, constructors can throw exceptions. Usually this means that the… May 12, 2026 at 10:49 am
  • Editorial Team
    Editorial Team added an answer Ok, I just added the config sections directly to the… May 12, 2026 at 10:49 am

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is

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.