-->
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.  [ 5 posts ] 
Author Message
 Post subject: Cascade save does not work
PostPosted: Tue May 01, 2007 10:31 pm 
Newbie

Joined: Tue May 01, 2007 5:48 pm
Posts: 6
Hi All,

I have a parent table - ENDPOINT - primary Key is ENDPOINT_ID and a bunch of other fields.
There is a child table SYNONYMS which have only 2 fields - ENDPOINT_ID (Foreign Key) and SYNONYM_TEXT. The primary key for this child table is a composite key made up of (ENDPOINT_ID and SYNONYM_TEXT).

Endpoint class has a Set of Synonyms. When an Endpoint instance is persisted then all the Synonyms should also be saved to SYNONYMS table.

Following is the hibernate XML mapping:
----------------------------------------------------
<hibernate-mapping package="com.pfizer.pgrd.like.endpoint.model">
<class name="Endpoint" table="ENDPOINT">
<!-- Primary key -->
<id name="epID" unsaved-value="null" type="long">
<column name="ENDPOINT_ID" not-null="true"/>
<generator class="increment"/>
</id>
<property name="epName" type="string" column="ENDPOINT_NAME" not-null="true"/>

........

<!-- Association: Synonym -->
<set name="epSynonyms" inverse="true" table="ENDPOINT_SYNONYM" cascade="all-delete-orphan">
<key>
<column name="ENDPOINT_ID"/>
</key>
<one-to-many class="EndpointSynonym"/>
</set>
</class>
-------------------------------------------------

<hibernate-mapping package="com.pfizer.pgrd.like.endpoint.model">
<class name="EndpointSynonym" table="ENDPOINT_SYNONYM">
<composite-id>
<key-property name="epID" type="long" column="ENDPOINT_ID"/>
<key-property name="synonymText" type="string" column="SYNONYM_TEXT"/>
</composite-id>
<many-to-one
name="endpoint"
class="Endpoint"
column="ENDPOINT_ID"
update="false"
insert="false"
not-null="true"
cascade="save-update"/>
</class>
-----------------------------------------------

Following has been done:
--------------------------------

1. Defined a persistence class for Endpoint
2. Defined a persistence class for Synonym.
3. Since synonym has a composite key therefore equals() and hashCode() methods have been overridden.


I am using Spring along with hibernate. When i construct the Endpoint object after adding all the child objects in a set (Synonymns) and call
getHibernateTemplate().save(endpoint) - It only persists fields in Endpoint table and does not add any child to Synonyms table

What am i doing wrong? Am i missing something here?

Any help will be greatly appreciated.

Thanks,
Madhav


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 02, 2007 12:17 am 
Expert
Expert

Joined: Tue Jan 30, 2007 12:45 am
Posts: 283
Location: India
Hi unmarshall,

change to this.
<set name="epSynonyms" inverse="true" table="ENDPOINT_SYNONYM" cascade="all,delete-orphan">

That would work.

_________________
Dharmendra Pandey


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 02, 2007 2:59 am 
Newbie

Joined: Tue May 01, 2007 5:48 pm
Posts: 6
Thanks for the reply.

I am using Hibernate 2.x and Spring 1.2.x
the problem is that it does not allow me to put cascade="all, delete-orphan"

It gives me the following exception:
nested exception is net.sf.hibernate.MappingException: invalid mapping - with nested exception: [org.xml.sax.SAXParseException: Attribute "cascade" with value "all,delete-orphan" must have a value from the list "none all save-update delete all-delete-orphan delete-orphan


If for the parent table mapping XML if inverse="false" and cascade="all" is given then it becomes a responsilibility of the parent to persist children as well and update any referential integrity constraints as well.

So hibernate fires 2 queries:

1. Insert query which inserts values into the parent table (ENDPOINT)
2. Update query on the child table updating the foreign key (ENDPOINT_ID)

However it does not fire the query for inserting values into the child table. Which is a little strange.

While adding the children to the Set contained in the parent object i am also setting the parent for each child that is getting added.

The option suggested by you - turning inverse="true" and cascade="all, delete-orphan"


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 02, 2007 3:14 am 
Expert
Expert

Joined: Tue Jan 30, 2007 12:45 am
Posts: 283
Location: India
Hi unmarshall,


I don't have exp in 2.x so not sure to tell you any specific behavior if insert is not working then check your id generator class and check unsaved value. by using save update or all it should work.

_________________
Dharmendra Pandey


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 02, 2007 5:35 am 
Regular
Regular

Joined: Thu Dec 22, 2005 7:47 am
Posts: 62
Location: Czech Republic
i'm not using composite keys, but are you sure you have a correct mapping? -- i would expect that you should define the whole key in the <key> section of the <set> in the _Endpoint_ class mapping file.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.