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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T08:16:14+00:00 2026-05-12T08:16:14+00:00

In my class Case I have an IDictionary with Entity (a class) as key

  • 0

In my class Case I have an IDictionary with Entity (a class) as key and Roles (an enum) as value. When trying to save a new instance (non-persisted) of Case, where the IDictionary is filled with new instances of Entity I get the following error:

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

These are the classes (Roles is an enum):

public class Case
{
    public Case { EntityCollection = new Dictionary<Entity, Roles>(); }
    public virtual int Id { get; set; }
    public virtual IDictionary<Entity, Roles> EntityCollection { get; set; }
}

and

public class Entity
{
    public virtual int Id { get; set; }
}

And mapping is as follows:

<class name="Case" table="[Case]">
    <id name="Id" column="Id" type="Int32" unsaved-value="any">
        <generator class="hilo"/>
    </id>
    <map name="EntityCollection" table="CaseEntityRoles" 
     cascade="save-update" lazy="false" inverse="false">
        <key column="CaseId" />
        <index-many-to-many class="Entity" 
         column="EntityId" />
        <element column="Roles" type="Roles" not-null="true" />
    </map>
</class>

and

<class name="Entity" table="[Entity]">
    <id name="Id" column="Id" type="Int32" unsaved-value="0">
        <generator class="hilo"/>
    </id>
</class>

Sample of the test code:

[Test]
public void Can_add_new_case()
{
    var newCase = new Case();
    newCase.EntityCollection.Add(new Entity(), Roles.Role1);
    newCase.EntityCollection.Add(new Entity(), Roles.Role2);

    /* At which point I try to persist newCase and get an exception */
}

In the testcode the newCase-instance is persisted, but the new entities are not. I’ve tried a lot of different things, like adding a <version> tag to Entity and messing around with unsaved-value, but nothing seems to help. And as you can see from the mapping I do have cascade=”save-update”.
Any ideas?

  • 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-12T08:16:14+00:00Added an answer on May 12, 2026 at 8:16 am

    I think you need to persist the Entity objects that are referenced by the Case first, before you try to persist the Case. I had a similar issue that I solved this way. For example, using the Rhino NHRepository:

    [Test]
    public void Can_add_new_case()
    {
        var newCase = new Case();
        var entity1 = new Entity();
        var entity2 = new Entity();
        newCase.EntityCollection.Add(entity1, Roles.Role1);
        newCase.EntityCollection.Add(entity2, Roles.Role2);
    
        Rhino.Commons.NHRepository<Entity> entityRepository = new NHRepository<Entity>();
        Rhino.Commons.NHRepository<Case> caseRepository = new NHRepository<Case>();
    
        using (UnitOfWork.Start())
        {
            entityRepository.SaveOrUpdate(entity1);
            entityRepository.SaveOrUpdate(entity2);
            caseRepository.SaveOrUpdate(newCase);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say I have the following code: abstract class Animal case class Dog(name:String) extends
In java, is there ever a case for allowing a non-abstract class to be
For example is it better to have: public class Case : EntityIdentifiable { public
I have this inheritance sealed abstract class MyValue case class MyString(s:String) extends MyValue case
The following test case fails in rhino mocks: [TestFixture] public class EnumeratorTest { [Test]
class A : IFoo { } ... A[] arrayOfA = new A[10]; if(arrayOfA is
I have the following code: public class DataReader<T> where T : class { public
Let's have a following example: public class X { } public class Y {
I have a library which returns a hierarchical list composed of IDictionary, IList and
I bump into this from time to time during class design... when I have

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.