-->
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.  [ 4 posts ] 
Author Message
 Post subject: Don't want to save into Parent but want to save Only in Chil
PostPosted: Fri Nov 07, 2008 12:14 pm 
Newbie

Joined: Fri Nov 07, 2008 12:00 pm
Posts: 3
Need help with Hibernate? Read this first:

http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:
3.0
Mapping documents:

Code between sessionFactory.openSession() and session.close():
Using Springs plug-in
Full stack trace of any exception that occurs:
NA
Name and version of the database you are using:
DB2

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:


Hi

I am using Hibernate3.0 plugging in into Spring. Now, The below are the sequence of actions I am doing.
1. Getting parent object info using getById() method.
2. Setting the Parent object into Child object and then adding child to Parent.
3. In hbm.xml files.
a) Parent: <set .... lazy="false" cascade="all" inverse="true" ....
<one-to-many>
</set> . Added to this the Id has declared as "native"
b) Child: <many-to-one ......./> . Added to this the Id has declared as "native"

Now, when I am trying to save the by calling getHibernateTemplet().saveorupdate(child). The save is failing as DataIntigrityException as the parent id and the group Id are going as "null".

But when I put cascade="all" in child, it is going fine and saving. The problem here is it is saving in both parent and child tables. but I don't want to save in parent table.

Please help me on this.

Read this: http://hibernate.org/42.html


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 07, 2008 7:45 pm 
Expert
Expert

Joined: Mon Nov 26, 2007 2:29 pm
Posts: 443
You cannot ask Hibernate to commit a violation of referential intergity.
Children have a reference (FK) to the parent, and that is a restriction imposed by your database, and by the definition of FK itself!

_________________
Gonzalo Díaz


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 08, 2008 5:54 pm 
Newbie

Joined: Fri Nov 07, 2008 12:00 pm
Posts: 3
Thanks for the reply Diaz.

Let me put more details about the requirements below.

1. We have Utility(Parent) Object and Group (Child) object.
Now, we have Utility_id in both the tables (TB_UTILITY, TB_GROUP) with a FK ( from group to Utility).
The relationship between Utility to Group is one-to-many and from Group to Utility is many-to-one.

**** The Utility information is static in table.

Now, the user tries creating a new group. As part of Group POJO, I have setUtility() and getUtilty() and in mapping we specified <many-to-one name="utility" ..../>. In this case if I specify cascade="all" in Group.hbm.xml then hibernate is saving group object in (TB_Group table) and Utility object info in (TB_UTILITY) which is proper.

But in my requirement we have to save Group information alone in (TB_GROUP table) with Utility_ID information (which is available.). Should not generate a new UTILITY_ID and insert a new record in (TB_UTILITY) table.

I was exploring is there any option in hibernate to turn off saving Utility object but save the Utility_Id in Group table along with Group inofrmation.

Now, finally I have taken off the set/getUtility method and added set/getUtility_Id() and also taken the <many-to-one .............> relationship entry in Group.hbm.xml file and added utility_id as a group property.

I will be grateful, if you could suggest me this the right way and the best way to achieve this objective


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 08, 2008 8:06 pm 
Expert
Expert

Joined: Mon Nov 26, 2007 2:29 pm
Posts: 443
Show me your mapping file, the table structure, and the client code you are using to change one group's utility.

_________________
Gonzalo Díaz


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