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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T16:50:02+00:00 2026-05-21T16:50:02+00:00

In my domain model a User is associated with many Places – the relationship

  • 0

In my domain model a User is associated with many Places – the relationship is modelled via a UserPlace class and a mapping (with FluentNHibernate) as follows (the User has a collection of UserPlace called Neighbourhood:

public class UserMap : ClassMap<User>
{
    HasMany(x => x.Neighbourhood)
      .Component(c =>
        {
          c.Map(x => x.IsDefault).Not.Nullable();
          c.Map(x => x.Selected).Not.Nullable().Default("0");
          c.References(x => x.Place).Fetch.Join();
        }
      ).Not.LazyLoad().Cascade.SaveUpdate();
}

Whenever I modify any UserPlace entity belonging to a user and then persist the user to the db, all UserPlace rows for that user are deleted and then re-inserted.

I assume this is because NHibernate doesn’t know how to uniquely identify one of these rows from another. In other words, the component in my mapping doesn’t have a key as such.

A primary key can be formed by combining the User_id and Place_id columns in the table that stores the relationship between these two entities. How can I set up this key using Fluent? And will this solve the delete-and-re-insert behaviour that I’m seeing?

EDIT: I asked on NHUsers about this and Fabio Maulo suggested using an IdBag. This isn’t supported in Fluent NHibernate as far as I can tell – and components don’t allow identifiers. How else can I map this many-to-many relationship and prevent the delete-all-reinsert-all issue?

EDIT 2: Here are the tables that NH generate based on my mappings

CREATE TABLE [dbo].[User](
[Id] [uniqueidentifier] NOT NULL,
--- a bunch of unimportant fields
CONSTRAINT [PK__User] PRIMARY KEY CLUSTERED ([Id] ASC))

CREATE TABLE [dbo].[Neighbourhood](
[User_id] [uniqueidentifier] NOT NULL,
[IsDefault] [bit] NOT NULL,
[Place_id] [int] NOT NULL,
[Selected] [bit] NOT NULL)

CREATE TABLE [dbo].[Place](
[Id] [int] IDENTITY(1000,1) NOT NULL,
--- a bunch of unimportant fields
CONSTRAINT [PK_Place] PRIMARY KEY CLUSTERED ([Id] ASC))

There is a FK relationship between User.Id and Neighbourhood.User_Id and between Neighbourhood.Place_id and Place.Id

  • 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. Editorial Team
    Editorial Team
    2026-05-21T16:50:03+00:00Added an answer on May 21, 2026 at 4:50 pm

    You may be able to map this relationship like this to avoid the scenario you are running into:

    public class UserMap : ClassMap<User>
    {
        HasMany(x => x.Neighbourhood)
          .KeyColumn("User_id")
          .Not.LazyLoad().Cascade.SaveUpdate();
    }
    
    public class NeighborHoodMap : ClassMap<NeighborHood>
    {
        Table("Neighbourhood");
            CompositeId()
                .KeyReference(x => x.User, "User_id")
                .KeyReference(x => x.Place, "Place_id");
    
        Map(x => x.IsDefault).Not.Nullable();
        Map(x => x.Selected).Not.Nullable().Default("0");
    }
    

    Neighborhood class would look something like this then:

    public class NeighborHood
    {
        public virtual User User { get; set; }
        public virtual Place Place { get; set; }
        public virtual bool IsDefault { get; set; }
        public virtual bool Selected { get; set; }
    
        public override bool Equals(object obj)
        {
           //check here to make sure these objects are equal (user_id and place_id are the same)    
        }
    
        public override int GetHashCode()
        {
            return User.Id.GetHashCode() ^ Place.Id.GetHashCode();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I got similar domain model 1) User. Every user got many cities. @OneToMany(targetEntity=adv.domain.City.class...) 2)
I have the following domain model setup using EF code first: public class User
My domain model with associations is as follows: Project has many Task s Task
Say I've got a domain model created from C# classes like this: public class
I have a class in my domain model root that looks like this: namespace
I have User and Address classes as follows: class User { ... ... @OneToOne(
I am working on the domain model for a project. I have a class
When designing both the domain-model and class-diagrams I am having some trouble understanding what
I have model User class User < ActiveRecord::Base has_and_belongs_to_many :roles attr_accessible :login, :email, :password,
My simplified domain model looks something like this: public abstract class Entity<IdK> { public

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.