*
The moose likes Object Relational Mapping and the fly likes Save parent and children on cascade - parent key not found Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Save parent and children on cascade - parent key not found" Watch "Save parent and children on cascade - parent key not found" New topic
Author

Save parent and children on cascade - parent key not found

Sheng Loong Su
Greenhorn

Joined: Jun 24, 2010
Posts: 3
I have a parent which consists of a set of children. The relationship in the database is maintained by having a foreign key column in the child table. I am using Oracle database and the primary keys are generated by using sequence. I am trying to add a new parent object together with the associated set of children objects to the database by using the hibernate cascade save but I have no success up to now.

In the parent class (Product.java)


In the child class (Picture.java)


FYI, this is a bi-directional one-to-many mapping. "lse09lse06id" is the foreign key column in the child entity table.

My driver class:


I truncated my code a bit for brevity sake. I actually execute the driver code in a Spring MVC controller. Anyway my problem is whenever the code is executed I will have the following exception:

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.dao.DataIntegrityViolationException: could not insert: [ghash.lse.domain.Picture]; SQL [insert into lse09pictures (lse09content, lse09date_created, lse09date_deleted, lse09date_updated, lse09is_deleted, lse09lse06id, lse09id) values (?, ?, ?, ?, ?, ?, ?)]; constraint [CSSE3005GG.LSE09PICTURES_FK]; nested exception is org.hibernate.exception.ConstraintViolationException: could not insert: [ghash.lse.domain.Picture]


One of the nested exceptions caught:
java.sql.SQLException: ORA-02291: integrity constraint (CSSE3005GG.LSE09PICTURES_FK) violated - parent key not found


Any help is very much appreciated! If you need more details I am more than willing to post them up.
Justin Chi
Greenhorn

Joined: Sep 09, 2009
Posts: 25
In the code , you are saving 2 objects in the same time , how child suppose to know id of parent which will be dynamically generated?

It properly should be done in a way that save parent first then get its id and set this id as child's foreign key.
Sheng Loong Su
Greenhorn

Joined: Jun 24, 2010
Posts: 3
Justin Chi wrote:In the code , you are saving 2 objects in the same time , how child suppose to know id of parent which will be dynamically generated?

It properly should be done in a way that save parent first then get its id and set this id as child's foreign key.


Thanks for the reply. However isn't cascade supposed to allow the 2 objects to be saved at the same time?

I just realised I forgot to embed my service and DAO code.

In my service class:

In my DAO class:

Sheng Loong Su
Greenhorn

Joined: Jun 24, 2010
Posts: 3
Can anyone please help? This is really urgent now.
 
Don't get me started about those stupid light bulbs.
 
subject: Save parent and children on cascade - parent key not found
 
Similar Threads
hibernate 1:M relationships & cascade
what changes should i made in save method in one-to-many relationships
new to Hibernate - cascading update question
retrieving object hierarchy - many-to-one?
Hibernate 3.0 : Where am I going wrong?