• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

Save parent and children on cascade - parent key not found

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone please help? This is really urgent now.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic