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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T04:37:14+00:00 2026-06-05T04:37:14+00:00

I have 2 tables with manyToMany relationship. public class User { public virtual Guid

  • 0

I have 2 tables with manyToMany relationship.

public class User
{
   public virtual Guid Id { get; set; }
   public virtual UInt64 UserId { get; set; }
   public virtual IList<Group> Groups{ get; set; }
}

public class Group
{
   public virtual Guid Id { get; set; }
   public virtual Guid GroupGuid  { get; set; }
   public virtual IList<User> Users{ get; set; }
}

I use Fluent NHibernate to AutoMap them as follows:

.Override<User>(obj => obj.HasManyToMany(x => x.Groups).Table("UserToGroup").Cascade.AllDeleteOrphan())

.Override<Group>(obj => obj.HasManyToMany(x => x.Users).Table("UserToGroup").Cascade.AllDeleteOrphan().Inverse())

When I add a User which contains groups list, All table updated OK:
Insert of new user in User table. OK
Insert of new group in Group table. OK
Insert of new relation inUserToGroup table. OK

I use the following code to add a User (including groups):

public void AddUser(User userData, IList<Guid> groupIds)
{
   User user = new User();
   user.UserId = userData.UserId;

   IList<User> Users = new List<Users();
   Users.Add(user);

   IList<Group> groups = new List<Group>();

   foreach (Guid groupId in groupIds)
   {
      Group grp = new Group();
      grp.GroupGuid = groupId;
      grp.Users = Users;
      groups.Add(grp);
   }

   user.Groups = groups;
   Session.Save(user);
}

The problem:
When I add a new User that contains a GroupGuid that is already exist in Group table, a new record is inserted to Group table with the same GroupGuid and new id.

Questions:

  1. How do I make NHibernate adding only new GroupGuid ids to Group table ? and still add a relation in UserToGroup table which related the new User to the exist Group.

  2. In AddUser method – should I set both side with entity i.e. set user.Groups with group list and set group.Users with user list ?

Thanks,
Haimon.

  • 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-05T04:37:16+00:00Added an answer on June 5, 2026 at 4:37 am

    The reason you are experiencing this is because you are likely using unassociated Group entities when you associate them with your User object. So you need to load them from the database.

    Alternatively, you can switch to using the Guids as the id for the entity, instead of whatever other primary key you’ve set up.

    To answer your second question, you should just be able to set one side of the relationship. However, I’m confused as to why you would have 2 separate tables for the relationship. You typically only need 1 other table:

    User
    Group
    UserGroup
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Group table with ManyToMany relationship with the User table via a
I have this 2 classes: @Entity public class Student extends User { ... @ManyToMany(mappedBy
I have 2 tables with manyToMany relationship. user can have many groups and groups
I have 2 Hibernate objects Group and User which are related with a ManyToMany
OK, I have mapped with annotations two tables with a bidirectional @ManyToMany relationship. Now
I have a pretty simple JPA @ManyToMany relationship set up in my application. A
i have two tables: Authority , Permission they have many to many relationship class
I have tables like this: tblUsers int UserID string UserName tblUsersInRoles int UserID int
I have two tables: Users and Roles. A user may have more roles, so
I have the following (simplified) Hibernate entities: @Entity @Table(name = package) public class Package

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.