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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T01:05:50+00:00 2026-06-05T01:05:50+00:00

I am getting a TransientObjectException when attempting to save an object that is part

  • 0

I am getting a TransientObjectException when attempting to save an object that is part of a many-to-many association. I somewhat understand why this is happening, but want to understand how to properly accomplish what I am trying to do.

What I am trying to do, in a nutshell:

My application has a list of users and a list of roles. Users can be assigned to multiple roles, and roles can be assigned to multiple users. There is a web page where an admin can perform these assignments, and the assignments can be done in both directions (e.g., the admin can pick a user, then add roles to it; OR, pick a role and add users to it).

For example, let’s say the admin clicks “Edit” on user “Alice.” The admin is presented with a list of roles available and a list of roles already assigned to Alice. The admin then assigns a new role to Alice and clicks “Save.”

On the server, the transient User and assigned Role objects are received from the client. If I just assign the transient Role list to the User object (e.g., user.Roles = roles), I can update just fine. However, if I happen to read the User from the database before making this assignment, I get a TransientObjectException on the associated Role object.

Class definitions:

public class Role
{
    public Guid ID { get; set; }
    public virtual IList<User> Users { get; set; }
}

public class User
{
    public Guid ID { get; set; }
    public virtual IList<Role> Roles { get; set; }
}

Mapping:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="Role">
    <id name="ID">
      <generator class="guid"/>
    </id>

    <bag name="Users" table="Role_User" lazy="false" cascade="none">
      <key column="RoleID" />
      <many-to-many column="UserID" class="User" />
    </bag>
  </class>
</hibernate-mapping>

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="User">
    <id name="ID">
      <generator class="guid"/>
    </id>

    <bag name="Roles" table="Role_User" lazy="false" cascade="none">
      <key column="UserID" />
      <many-to-many column="RoleID" class="Role" />
    </bag>
  </class>
</hibernate-mapping>

Code to save (works)

public void UpdateUser(User user, IList<Role> associatedRoles)
{
    using (var session = _sessionFactory.OpenSession())
    {
        user.Roles = associatedRoles;
        session.Merge<User>(user);
    }
}

Code to save (fails)

public void UpdateUser(User user, IList<Role> associatedRoles)
{
    using (var session = _sessionFactory.OpenSession())
    {
        User originalUser = session.Get<User>(user.ID);
        // Code that does some audit reporting/logging
        LogDifferences(originalUser, user);
        user.Roles = associatedRoles;
        session.Merge<User>(user);
    }
}

object is an unsaved transient instance – save the transient instance before merging

  • 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-05T01:05:51+00:00Added an answer on June 5, 2026 at 1:05 am

    Miroslav Popovic’s points are highly relevant; however, in my case I was running into problems because of entities in the database which had a Version set to an empty guid. These records were created outside of NHibernate. When saving an association between them, NHibernate was failing because it detected one of the association entries as a transient object due to its 0 version, even though it referred to an existing item.

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

Sidebar

Related Questions

Getting this error when I follow the simple code example from jsoncpp, that basically
Getting this. Site works fine locally, but up on production server I get this
Getting the following error, which I understand and have seen before. But one thing
getting problem is that and how can solve this .I trying the internet is
Getting java.io.EOFException while sending a large java object using AciveMQ. Below is the large
Getting No Data Access Object for AudienceManagerProfile when I make any OE API calls.
Getting an error is usually unpleasant, but sometimes it is also unpleasant when you
Getting some odd behavior from google custom search that I can't seem to suss
Getting started with TDD and I want to ground up a Repository-driven Model. However,
getting a couple errors that it's a virtual function with non-virtual destructor. how can

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.