• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

hibernateTemplate.saveOrUpdate(...) issue  RSS feed

 
Ranch Hand
Posts: 220
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I currently have a hibernate issue. Please help.

Scenario :

I have 2 tables like

Table A : pk = ida

Table B : pk = idb

In table A, there is a foreign key of Table B, called idfkb

And the object design is


Class A
{
Integer ida;
B b;
....
}


Class B
{
Integer idb;
.....
}

I would like to insert an A record into the database with the existence of B record.

When I call hibernateTemplate.saveOrUpdate(A record), it throws exception that there is a duplicate value constraints. I believe the reason is because B record is already exist in B table, and the method saveOrUpdate will insert a record into A table and also insert the child record into B table.


For this scenario, is there any alternatives to insert A record with the existence of B record? please help.


Thanks
Benson
 
Ranch Hand
Posts: 754
Eclipse IDE Java
 
Benson Fung
Ranch Hand
Posts: 220
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Assume the B record already exists in the table. I need to insert a new record of A with a foreign key to B record. Then use hibernateTemplate.saveOrUpdate(...) to save the values.


 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Please use cascade=all for the mapping to B class so your problem will be solved.
 
He loves you so much! And I'm baking the cake! I'm going to put this tiny ad in the cake:
ScroogeXHTML - small and flexible RTF to HTML converter library
https://coderanch.com/t/710903/ScroogeXHTML-RTF-HTML-XHTML-converter
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!