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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:18:17+00:00 2026-06-12T11:18:17+00:00

My database has two tables: Users UserId PK Likes LikeId PK LikedUserId FK LikingUserId

  • 0

My database has two tables:

Users
    UserId        PK

Likes
    LikeId        PK
    LikedUserId   FK
    LikingUserId  FK

These tables represent user ‘likes’. Likes are directional – user A likes user B, but user B does not like user A.

I am mapping with NHibernate like this:

<class name="User" table="Users">
    <id name="UserId">
        <generator class="assigned"></generator>
    </id>

    <idbag name="LikedBy" table="Likes" inverse="true">
        <collection-id column="LikeId" type="guid">
            <generator class="guid.comb"/>
        </collection-id>
        <key column="LikedUserId"/>
        <many-to-many column="LikingUserId" class="User"/>
    </idbag>

    <idbag name="Liking" table="Likes">
        <collection-id column="LikeId" type="guid">
            <generator class="guid.comb"/>
        </collection-id>
        <key column="LikingUserId"/>
        <many-to-many column="LikedUserId" class="User"/>
    </idbag>
</class>

Corresponding to this class:

public class User 
{
    public Guid UserId { get; set; }
    public ICollection<User> LikedBy { get; set; }
    public ICollection<User> Liking { get; set; }
}

This works fine. However, the Liking and LikedBy collections for two different users can get out of sync. For instance:

using (var session = new NHibHelp.OpenSession())
{
    var user1 = session.GetById<User>(id1);
    var user2 = session.GetById<User>(id2);

    int initialCount = user2.LikedBy.Count();

    user1.Liking.Add(user2);
    session.Update(user1);

    int finalCount = user2.LikedBy.Count();

    Assert.AreEqual(initalCount,finalCount); // passes
}

So if I add to the Liking collection of one user, the LikedBy collection of another user is not updated. To have it updated, I need to do Session.Refresh().

This isn’t ideal. I’m quite new to NHibernate, so I don’t know if there is another solution – is there anything better I can do? For instance, is it possible to map this scenario in such a way that LikedBy is automatically updated when appropriate?

  • 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-12T11:18:18+00:00Added an answer on June 12, 2026 at 11:18 am

    You can add a method to the User class that syncs both collections:

    public class User 
    {
        public Guid UserId { get; set; }
        public ICollection<User> LikedBy { get; set; }
        public ICollection<User> Liking { get; set; }
    
        public void Like(User user) 
        {
           this.Liking.Add(user);
           user.LikedBy.Add(this);
        }
    }
    

    You may also make this collections readonly to an external code:

    private ICollection<User> likedBy;
    private ICollection<User> liking;
    
    public IEnumerable<User> LikedBy { get { return this.likedBy; } }
    public IEnumerable<User> Liking { get { return this.liking; } }
    
    <idbag access="field.camelcase" ... />
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables in my database who looks like this: User: userid(int) username(varchar)
I'm working on a SQL Server 2008 database. This database has two tables. Book
One of my tables in my SQL database has a growth rate of two
I have a database framework where I have two tables. The first table has
My database has two types of entries: The very dynamic (users, comments, etc) and
For the sake of simplicity, let's say my database has two tables, videos and
so I have this problem. My Database has two tables that look something like
I have a database with two tables. One of the tables contains users, the
I have a Database with the following two tables, USERS, POSTS I am looking
I have a database with two tables: Users , Items Users looks like this:

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.