4

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

masoud ramezani
  • 22,228
  • 29
  • 98
  • 151
Vahid Ghadiri
  • 3,966
  • 7
  • 36
  • 45
  • Just to be clear, do you mean that there is one object that is self referencing, or do you mean that there are two employee objects, with reference(s) between them? IE, are there two employee records, with Foreign keys between them, or one employee record with a foreign key pointing to the primary key. – bzarah Jul 06 '11 at 16:16
  • also, it would be helpful to have the mapping file or code for the employee entity. – bzarah Jul 06 '11 at 16:22
  • thanks for ur suggestion. i'll try to be more clear. i edited my question. there are two employee records, with Foreign keys between them. – Vahid Ghadiri Jul 07 '11 at 16:35

1 Answers1

4

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.

bzarah
  • 358
  • 1
  • 9
  • I don't want to edti Entity1 that is an existing record in Db. but i have to fetch it so i can assign it to Entity2's RefId property that has Employee type. – Vahid Ghadiri Jul 09 '11 at 18:35
  • meanwhile just Entity2 has a FK that references to Entity1 as it's manager but Entity1 has a null value in it's refId property. my goal is to have these to records in table: a manager who has null value in it's refId column and an employee who has managerId value in it's refId column. – Vahid Ghadiri Jul 09 '11 at 18:39
  • Even easier! If you only need to create a reference from Entity2 to Entity 1, you really don't even need to fetch the record. If you know the id value of Entity1, you can use Entity2.Ref = Session.Load(idValue). This will hook up the reference for you. I just noticed something now that I should have caught earlier. You need to remove the RefId property. The is all that is required to make the reference. Having the RefId there is redundant, and is probably causing some issues for you. – bzarah Jul 11 '11 at 11:21
  • your right. having two RefId and properties made me in trouble. but i prefered cut insert="false" update="false" from refid and past it in Properties and this solve my problem. thanks for your advice. good luck – Vahid Ghadiri Jul 12 '11 at 18:14