-->
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: Parent & Child with Composite Keys - One-to-many Save is
PostPosted: Wed Jan 11, 2006 1:23 pm 
Newbie

Joined: Wed Jan 11, 2006 1:04 pm
Posts: 5
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

All classes are Serializable, equals and hashcode methods are available
[b]Hibernate version:[/b]
2.1.7
[b]Mapping documents:[/b]
<hibernate-mapping>
<class name="Parent" table="Parent_Table">
<composite-id name="key" class="ParentKey" unsaved-value="any">
<key-property name="dealNumber" type="long" column="DEAL_ID"/>
<key-property name="decisionId" type="long" column="DECISION_ID"/>
</composite-id>
<list name="children" table="Child_Table" inverse="true" cascade="all-delete-orphan">
<key>
<column name="DEAL_ID"/>
<column name="DCSN_ID"/>
</key>
<index column="ADJ_DATA_SEQ"/>
<one-to-many class="Child"/>
</list>
</class>

</hibernate-mapping>


<hibernate-mapping>
<class name="Child" table="Child_Table">
<composite-id name="key" class="ChildKey" unsaved-value="any">
<key-many-to-one name="parent" class ="Parent">
<column name="DEAL_ID"/>
<column name="DCSN_ID"/>
</key-many-to-one>
<key-property name="adjustmentSequenceNo" type="int" column="ADJ_DATA_SEQ"/>
</composite-id>

</class>
</hibernate-mapping>
[b]Code between sessionFactory.openSession() and session.close():[/b]
Parent data = new Parent();
ParentKey key = new ParentKey();

//set the Keys
key.setDecisionId(119);
key.setDealNumber(1000);

//save Parent
data.setKey(key);
hibernateTemplate.save(data);
hibernateTemplate.flush();

//set the Child Key
Child child = new ChildData ();
ChildKey childKey = new ChildKey();
childKey .setAdjustmentSequenceNo(200);

//SAVE CHILD
Parent parent = (Parent )template.load(Parent .class, key);
child .getKey().setParent(parent);
parent.getChildren().add(child );
hibernateTemplate.save(child );
hibernateTemplate.flush();
[b]Full stack trace of any exception that occurs:[/b]
Hibernate: insert into Parent(PREM_RATE1, PREM_RATE2, PREM_RATE3, PREM_RATE4, TAX_RATE, APP_FEE, TIME_STAMP,
Hibernate: insert into Child(ADJ_RULE_ID, ADJ_VER_ID, ADJ_TYPE, ADJ_VAL1, ADJ_VAL2, ADJ_VALM3, ADJ_VAL4,
Jan 11, 2006 11:58:02 AM net.sf.hibernate.impl.SessionImpl
SEVERE: Could not synchronize database state with session
[b]Name and version of the database you are using:[/b]
DB2
[b]The generated SQL (show_sql=true):[/b]
Hibernate: insert into Parent_Table(......)
Hibernate: select parent_.DEAL_ID as DEAL_ID0_,.....
Hibernate: insert into Child_Table(ADJ_RULE_ID, ........
Jan 11, 2006 11:58:02 AM net.sf.hibernate.impl.SessionImpl
SEVERE: Could not synchronize database state with session

[b]Debug level Hibernate log excerpt:[/b]


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 12, 2006 4:23 am 
Expert
Expert

Joined: Thu May 26, 2005 9:19 am
Posts: 262
Location: Oak Creek, WI
Hi,

I think you have closed the session propely.

_________________
RamnathN
Senior Software Engineer
http://www.linkedin.com/in/ramnathn
Don't forget to rate.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 12, 2006 4:24 am 
Expert
Expert

Joined: Thu May 26, 2005 9:19 am
Posts: 262
Location: Oak Creek, WI
Sorry not closed the session properly

_________________
RamnathN
Senior Software Engineer
http://www.linkedin.com/in/ramnathn
Don't forget to rate.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 12, 2006 11:31 am 
Newbie

Joined: Wed Jan 11, 2006 1:04 pm
Posts: 5
Thank you for your response.

I am using Spring -Hibernate framework. Instead of Session using HibernateTemplate Class. It manages the session for me. One-to-many is working in following combination.

Parent has 1 primary key, child has composite keys.

But not working Parent and Child containg composite keys combination.

Please help!!!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 12, 2006 1:10 pm 
Expert
Expert

Joined: Thu May 26, 2005 9:19 am
Posts: 262
Location: Oak Creek, WI
Since both are in different transactions you get a problem there.

So can u make the relationship like this
Parent data = new Parent();
ParentKey key = new ParentKey();

//set the Keys
key.setDecisionId(119);
key.setDealNumber(1000);

//set the Child Key
Child child = new ChildData ();
ChildKey childKey = new ChildKey();
childKey.setAdjustmentSequenceNo(200);

//save Parent
data.setKey(key);
data.setChildren(child); // list of childs

hibernateTemplate.save(data);
hibernateTemplate.flush();

So now its in the same transaction.

_________________
RamnathN
Senior Software Engineer
http://www.linkedin.com/in/ramnathn
Don't forget to rate.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 17, 2006 10:26 am 
Newbie

Joined: Wed Jan 11, 2006 1:04 pm
Posts: 5
Thanks for your response. It did not work. Will post as soon as I get the resolution. Currently using "composite-element" instead of "one-to-many".


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.