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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:54:04+00:00 2026-05-23T13:54:04+00:00

i have a self join employees entity class with id,name and ref columns that

  • 0

i have a self join employees entity class with id,name and ref columns that has relation with it self. i want to create new instance of that and persist it to db.

at first i created an instance of Employee class and named it manager. then i fetched a record from Employee table with these values: Id = 1, Name = “A”, RefId = null
and set those values to manager object. after that i created an instance of Employee class again
and set it’s properties value like this:
emp.Name = “B”, emp.Ref = manager.
finally i persisted it by using base.Add(resource) method. at that time Nhibernate raised the following error:
“object references an unsaved transient instance save the transient instance before flushing”.

this is mapping file contents:

<class name="Employee" table="Employee" schema="dbo" optimistic-lock="none" lazy="true">
    <id name="Id" access="property" column="Id">
        <generator class="identity" />
    </id>
    <property name="Name" type="String" column="Name" length="50" />
    <property name="RefId" type="Int64" column="RefId"  insert="false" update="false"/>
    <many-to-one name="Ref" class="Employee" column="RefId" not-null="false" fetch="select" />
 class>

please help me to solve this error.
thx

  • 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-23T13:54:04+00:00Added an answer on May 23, 2026 at 1:54 pm

    Let’s say Entity 1 is the existing record in the database, and Entity 2 is the new record that you are trying to create that has a reference to Entity 1.

    Hibernate is telling you that the new entity (entity 2) you are saving has a reference to entity 1 (the one from the database), and that entity 1 has unsaved changes that must be saved before it can save entity 2. The easiest thing to do is save entity 1 first, then save entity 2. But, I think the real issue is how you are getting an instance of Entity1.

    You say that you create and instance of employee, then name it manager, then fetch a record from the employee table. If you are trying to update the existing record from the table, why don’t you fetch the entity first, then edit it? Why are you instantiating an object at all? The other thing I wasn’t sure about was whether the relationship between the objects was bi-directional. That is, Entity 1 has an FK to Entity 2 AND ALSO Entity 2 has an FK to Entity 1. If this is the case, you need to make sure you assign your “Ref” property twice. Entityy1.Ref = Entity2 AND ALSO Entity2.Ref = Entity1.

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

Sidebar

Related Questions

I have a query with a self join that looks like this, select t1.
I have a class: class MyClass: def __init__(self, foo): if foo != 1: raise
I have implemented a linked list as a self-referencing database table: CREATE TABLE LinkedList(
I have the following code: class IncidentTag: def __init__(self,tag): self.tag = tag def equals(self,obj):
I have a VB6 application that needs to update it's self. For this purpose,
I have the following example code: class A(object): def __init__(self, id): self.myid = id
I have a nib that I load the usual way [NSBundle loadNibNamed:@AuthorizationWindow owner:self]; and
I have a self join through an intersect table in an app I'm working
I have a self-hosted WCF that runs under network_service beautifully on \Server1, but needs
I have Laconica (self hosted twitter ) configured on my local intranet and would

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.