-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: Root entity is not saved but the rest of the graph is...?
PostPosted: Wed Jan 16, 2008 2:03 pm 
Newbie

Joined: Wed Jan 16, 2008 1:52 pm
Posts: 3
I have the following model:

Organization (root entity) (identified by Guid)
-ContactInformation (related entity to Organization)
--Address (related entity to ContactInformation)

When I try to save Organization using NHibernate, the ContactInformation entity and it's related Address are saved perfectly but the root entity Organization is not persisted to the underlying storage.

I'm pulling my hair out trying to figure out why. There is no exception when I call save and after calling save if I check the Organization's key field in the entity it is set with a new Guid just as I would expect if the save action worked but when I check in the database it is not there.


example code for save
Code:
ISession session = factory.OpenSession();
session.SaveOrUpdate(organization);




here is my mapping file

Code:
<?xml version="1.0"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="RELink.Library.Domain" assembly="RELink.Library">
  <class name="Organization" table="Organization" lazy="true">
    <id name="Id" unsaved-value="00000000-0000-0000-0000-000000000000" access="field.camelcase">
      <column name="OrganizationId" />
        <generator class="guid">
      </generator>
    </id>
    <many-to-one name="ContactInformation" column="ContactInformationId" class="ContactInformation" cascade="all-delete-orphan" access="field.camelcase" />
    <property name="dateCreated" access="field" />
  </class>

  <class name="ContactInformation" table="ContactInformation" lazy="true">
    <id name="Id" type="System.Int64" unsaved-value="0" access="field.camelcase">
      <column name="ContactInformationId" />
      <generator class="native"></generator>
    </id>

   
    <property name="CompanyName" />
    <property name="FirstName" />

    <many-to-one name="Address" column="AddressId" access="field.camelcase" class="Address" cascade="all-delete-orphan" />
  </class>

  <class name="Address" table="Address" lazy="true">
    <id name="Id" type="System.Int64" unsaved-value="0" access="field.camelcase">
      <column name="AddressId"/>
      <generator class="native"></generator>
    </id>

    <property name="Number" />
  </class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 16, 2008 2:17 pm 
Expert
Expert

Joined: Mon Nov 26, 2007 2:29 pm
Posts: 443
Kasi,
Not much of your Organization object seems suitable to be updated. The id, once set, doesn't change. The "date created" also seems pretty immutable.
It seems to me, Hibernate is working as expected.
Why would you want to change the ID of an already created object?

_________________
Gonzalo Díaz


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 16, 2008 2:21 pm 
Newbie

Joined: Wed Jan 16, 2008 1:52 pm
Posts: 3
The problem is Organization is never being Inserted???? Why not?

It's true, organization itself, once created will not change, but in this case the problem is that Organization is new so it should end up in creating a new Organization record in the database, but that never happens.

When I use the following code
Code:
Organization o = new Organization
o.Name = "RELink"; (this property delegates to the inner contact information)
session.SaveOrUpdate(o);


A contact information record is created, but no Organization record is created in the database.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 17, 2008 10:55 am 
Regular
Regular

Joined: Mon Mar 20, 2006 10:49 pm
Posts: 59
Try calling Save instead of SaveOrUpdate for a new Organization. If that works, it means that SaveOrUpdate can't distinguish between new and existing Organizations, possibly because the mapping doesn't specify unsaved-value for the id.

Mike

_________________
Mike Abraham


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 17, 2008 1:05 pm 
Newbie

Joined: Wed Jan 16, 2008 1:52 pm
Posts: 3
I figured it out but I can't say I understand why...

If I call session.Flush() the root entity is saved to the db just fine but why does the inner relationships of my root get persisted whether I call flush or not?

I would think that if I call SaveOrUpdate and nhibernate determines it needs to save the graph it would preform all needed INSERT's and UPDATE's as part of a single transaction then calling Flush would Commit the transaction but it seems the INSERT and UPDATE statements for all entities under the root are being persisted immediately and the root entity is only being persisted if I call Flush.

How can I change this to work as I would expect?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 17, 2008 1:21 pm 
Regular
Regular

Joined: Mon Mar 20, 2006 10:49 pm
Posts: 59
You didn't provide any of your session management code, so it's difficult to say exactly what's going on. But here is one theory that seems to fit the facts. First, NHibernate will flush from time to time, with the primay guarantee being that a query will never return stale data. However, if you close the session without call Flush(), any unpersisted changes will be lost. The explanation for why the non-root objects are persisted, but the root object is not, is that objects with "native" id's are always persisted as soon as they are saved, because NHibernate needs to get the id immediately since it is used as the object identifier.

So, if you were closing the session without calling Flush(), you would get exactly the behavior you reported.

Also, regarding transactions. If you want transactional behavior, you must start and commit the transactions yourself. NHIbernate won't do it for you.

I trust this is useful.

Mike

_________________
Mike Abraham


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.