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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T22:54:39+00:00 2026-05-18T22:54:39+00:00

I’m trying to map a many-to-many relationship between two entities, but I need to

  • 0

I’m trying to map a many-to-many relationship between two entities, but I need to decorate that entity with a number of properties – see the diagram below:

Many-to-Many Relationship with additional attributes on relationship table

Reads is my relationship table in this case – I added an identity column on it in order to avoid using a composite key, but the valuable information here is really the UserId, the FeedItemId, and the TimeRead attribute. Here is how I have tried to map this relationship, based on similar examples I’ve seen on StackOverFlow:

User

public class UserMap : ClassMap<User>
{
    public UserMap()
    {
        Id(x => x.UserId).GeneratedBy.Identity();
        Map(x => x.UserName).Length(DataConstants.UserNameLength).Unique().Not.Nullable();
        Map(x => x.EmailAddress).Length(DataConstants.EmailAddressLength).Unique().Not.Nullable();
        Map(x => x.DateJoined).Not.Nullable();
        Map(x => x.Password).Length(DataConstants.PasswordHashLength).Not.Nullable();
        HasManyToMany(x => x.UserRoles).Cascade.AllDeleteOrphan().AsBag().Table("UsersInRole");
        HasManyToMany(x => x.SubscribedFeeds).Cascade.DeleteOrphan().AsBag().Table("Subscriptions");
        HasManyToMany(x => x.OwnedFeeds).Cascade.All().AsBag().Table("FeedOwners");
        HasMany(x => x.Reads).Cascade.DeleteOrphan().Fetch.Join().Inverse().KeyColumn("UserId");
    }
}

FeedItem

public class FeedItemMap : ClassMap<FeedItem>
{
    public FeedItemMap()
    {
        Id(x => x.FeedItemId).GeneratedBy.Identity();
        Map(x => x.OriginalUri).Not.Nullable().Unique().Length(DataConstants.FeedUriLength);
        Map(x => x.DatePublished).Not.Nullable();
        Map(x => x.Title).Not.Nullable().Length(DataConstants.FeedItemTitleLength);
        References(x => x.Feed);
        HasManyToMany(x => x.Tags).Cascade.All().Table("PostTags");
        HasManyToMany(x => x.Categories).Cascade.All().Table("PostsInCategory");
        HasMany(x => x.Reads).Cascade.AllDeleteOrphan().Inverse().Fetch.Join().KeyColumn("FeedItemId");
    }
}

Reads:

public class FeedReadMap : ClassMap<FeedRead>
{
    public FeedReadMap()
    {
        Table("Reads");
        //CompositeId()
        //    .KeyProperty(x => x.TimeRead, "TimeRead")
        //    .KeyReference(x => x.ItemRead, "FeedItemId")
        //    .KeyReference(x => x.Reader, "UserId");
        Id(x => x.ReadId).GeneratedBy.Identity();
        Map(x => x.TimeRead).Not.Nullable();
        References(x => x.Reader).Not.Nullable().Cascade.SaveUpdate().Column("UserId");
        References(x => x.ItemRead).Not.Nullable().Cascade.SaveUpdate().Column("FeedItemId");
    }
}

This code doesn’t raise errors as-is, but nothing is ever persisted to the Reads table when I try to do the following:

var read = new FeedRead {ItemRead = feed.Items[0], Reader = user, TimeRead = DateTime.Now};
        user.Reads.Add(read);
        feed.Items[0].Reads.Add(read);

        _repository.SaveUser(user);

This is probably because of the fact that both User and FeedItem have a .Inverse on the relationship mapping – I did it this way because it’s what I saw in most other examples that tried to model this same relationship.

When I remove the .Inverse off of the User mapping, I get this error instead:

NHibernate.TransientObjectException:
object references an unsaved transient
instance – save the transient instance
before flushing.

My ultimate goal is to be able to do Session.SaveOrUpdate(user) and have it insert any new feed reads directly into the Reads table, but I haven’t been able to find a way to do it yet. What am I doing wrong?

I’ve read through virtually every other question on StackOverFlow about this topic already and haven’t found a clear answer to this question.

  • 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-18T22:54:40+00:00Added an answer on May 18, 2026 at 10:54 pm

    It all depends on how you want to save new Reads.

    If you want to save new Reads via User, then you need Cacascade.AllDeleteOrphan() for User. As it stands, it will not cascade the new reads as you only have DeleteOrphan.

    public class UserMap : ClassMap<User>
    {
        public UserMap()
        {
            Id(x => x.UserId).GeneratedBy.Identity();
            Map(x => x.UserName).Length(DataConstants.UserNameLength).Unique().Not.Nullable();
            Map(x => x.EmailAddress).Length(DataConstants.EmailAddressLength).Unique().Not.Nullable();
            Map(x => x.DateJoined).Not.Nullable();
            Map(x => x.Password).Length(DataConstants.PasswordHashLength).Not.Nullable();
            HasManyToMany(x => x.UserRoles).Cascade.AllDeleteOrphan().AsBag().Table("UsersInRole");
            HasManyToMany(x => x.SubscribedFeeds).Cascade.DeleteOrphan().AsBag().Table("Subscriptions");
            HasManyToMany(x => x.OwnedFeeds).Cascade.All().AsBag().Table("FeedOwners");
            HasMany(x => x.Reads).Cascade.AllDeleteOrphan().Fetch.Join().Inverse().KeyColumn("UserId");
        }
    }
    

    Just a thought, I always like to minimize bi-directional relationships in an attempt to keep the domain simpler. Hence I would probably not have the collections on User or FeedItem if it could be avoided.

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

Sidebar

Related Questions

I want to count how many characters a certain string has in PHP, but
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
I need to clean up various Word 'smart' characters in user input, including but
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.