-->
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.  [ 18 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Cascade, child object's id is not updated
PostPosted: Mon Mar 02, 2009 11:00 pm 
Beginner
Beginner

Joined: Wed Dec 17, 2008 12:10 pm
Posts: 47
I am working with a parent object which has a Map collection map with child objects like this:

<map cascade="all-delete-orphan" catalog="SomeCatalog" inverse="true"
name="revisions" sort="unsorted" table="SomeTable">
<key column="PageFK"/>
<index column="Revision" type="int"/>
<one-to-many class="SomeType" not-found="ignore"/>
</map>

My problem is that as you can see I'm cascading the save to the child objects. They are being saved properly, but they id is not being updated on the child objects. So, until I open a new session and re-get the collection, anything that was added has an id of zero. Any thoughts?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 03, 2009 4:54 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Might be a problem with the id mapping of the child object. Can you post it as well?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 03, 2009 9:19 am 
Beginner
Beginner

Joined: Wed Dec 17, 2008 12:10 pm
Posts: 47
Here's the ID mapping of the child object:

<id name="id" type="java.lang.Integer">
<column name="SomeObjectID"/>
<generator class="identity"/>
</id>


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 03, 2009 9:47 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
The mapping seems to be ok. Are you using Session.merge() to update the parent object? merge() creates copies and doesn't modify existing objects.

Quote:
... anything that was added has an id of zero


Hmmm.... are you sure the id is '0' and not 'null'? What is the value of the child id before you add it to the the collection?

In the mapping you have type="java.lang.Integer", which suggests that unsaved items should have a 'null' id. If you use '0' I am not really sure what is going to happen. I would expect Hibernate to complain about multiple objects using the same id... but if Hibernate tries to insert an object with id=0 the database may actually generate a new >0 id. Since Hibernate doesn't allow id's to change the child id would still be 0.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 03, 2009 10:13 am 
Beginner
Beginner

Joined: Wed Dec 17, 2008 12:10 pm
Posts: 47
Ah, I see. I am using Session.saveOrUpdate(). Also, prior to being saved the value of the id is zero. In my data objects I'm using the primitive type int. Is there a way to use the primitive type int in the mapping?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 03, 2009 11:32 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
Try to add unsaved-value="0" to your id-mapping.

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 03, 2009 1:10 pm 
Beginner
Beginner

Joined: Wed Dec 17, 2008 12:10 pm
Posts: 47
Alright, I will try that. I will have to create a test case because I changed the way I was working with the example I posted above. This application is constantly changing ;). Thanks for your help!!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 03, 2009 2:15 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
<id name="id" type="int" unsaved-value="0">


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 03, 2009 3:59 pm 
Beginner
Beginner

Joined: Wed Dec 17, 2008 12:10 pm
Posts: 47
This did not work. It is still zero after the parent object has been saved. Is there anything else you can think of?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 03, 2009 4:03 pm 
Beginner
Beginner

Joined: Wed Dec 17, 2008 12:10 pm
Posts: 47
Here are the full mappings:

Parent:

Code:
<hibernate-mapping package="somePackage">
<class catalog="SomeCatalog" name="Page" table="Pages">
  <id name="id" type="java.lang.Integer">
   <column name="PageID"/>
   <generator class="identity"/>
  </id>
  <many-to-one class="Folder" fetch="select" name="folder">
   <column name="FolderFK" not-null="false"/>
  </many-to-one>
  <many-to-one class="Layout" fetch="select" name="layout">
   <column name="LayoutFK" not-null="true"/>
  </many-to-one>
  <many-to-one class="Template" fetch="select" name="template">
   <column name="TemplateFK" not-null="true"/>
  </many-to-one>
  <many-to-one class="Site" fetch="select" name="site">
   <column name="SiteFK" not-null="true"/>
  </many-to-one>
  <property generated="never" lazy="false" name="name" type="string">
   <column length="50" name="Name" not-null="true"/>
  </property>
  <property generated="never" lazy="false" name="url" type="string">
   <column length="200" name="Url"/>
  </property>
  <map cascade="all-delete-orphan" catalog="SomeCatalog" inverse="true"
   name="revisions" sort="unsorted" table="SomeTable">
   <key column="PageFK"/>
   <index column="Revision" type="int"/>
   <one-to-many class="PageRevision" not-found="ignore"/>
  </map>
  <property generated="never" lazy="false" name="title" type="string">
   <column length="200" name="Title"/>
  </property>
  <property generated="never" lazy="false" name="metaDescription" type="string">
   <column name="MetaDescription"/>
  </property>
  <many-to-one class="User" name="lockedBy">
   <column name="LockedBy"/>
  </many-to-one>
</class>
</hibernate-mapping>


Child:

Code:
<hibernate-mapping package="com.verani.common.cms">
<class catalog="CMS" name="PageRevision" table="PageRevisions">
  <id column="PageRevisionID" name="id" type="int" unsaved-value="0">
   <generator class="native"/>
  </id>
  <many-to-one class="com.verani.common.cms.Page" fetch="select" name="page">
   <column name="PageFK" not-null="true"/>
  </many-to-one>
  <property generated="never" lazy="false" name="revision" type="int">
   <column name="Revision" not-null="true"/>
  </property>
  <property generated="never" lazy="false" name="published" type="boolean">
   <column name="IsPublished" not-null="true"/>
  </property>
  <property generated="never" lazy="false" name="date" type="timestamp">
   <column length="19" name="Date" not-null="true"/>
  </property>
  <many-to-one class="com.verani.common.backPage.User" column="UserFK"
   lazy="proxy" name="user"/>
  <set cascade="all,delete-orphan" catalog="CMS" inverse="true"
   name="fields" sort="unsorted" table="PageFields">
   <key column="PageRevisionFK"/>
   <one-to-many class="com.verani.common.cms.PageField"/>
  </set>
  <property generated="never" lazy="false" name="committed" type="boolean">
   <column name="IsCommitted"/>
  </property>
</class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 04, 2009 3:59 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
Do you also set the parent of the child-objects like child.setParent(parent)? You need to do this, as you have mapped the association as inverse in parent, so the child is the owner of it.

Also something that makes me wonder: In your parent mapping the class name is just "Page", but in your child you map it as "com.verani.common.cms.Page" try to use the same name on both sides.

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 04, 2009 8:56 am 
Beginner
Beginner

Joined: Wed Dec 17, 2008 12:10 pm
Posts: 47
Thanks for your help. I think that may be it, I might not be setting the parent in the child object. Also, I was trying to remove all of the package names to simplify the mapping files haha.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 04, 2009 10:39 am 
Beginner
Beginner

Joined: Wed Dec 17, 2008 12:10 pm
Posts: 47
Ah, nuts. I confirmed that I am actually setting the parent. I'm really baffled as to why this is happening? The parent itself has its id set properly when it is created, just not the child.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 04, 2009 10:43 am 
Beginner
Beginner

Joined: Wed Dec 17, 2008 12:10 pm
Posts: 47
I see why it is happening. The cascade isn't being triggered by Session.saveOrUpdate. It's only being triggered once I execute a query or use the criteria API. So basically, the insert has not run for the child object yet, even though I've saved the parent object. Is this expected behavior?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 04, 2009 11:21 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
Did you flush the session or commit the transaction?

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 18 posts ]  Go to page 1, 2  Next

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.