This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Object Relational Mapping and the fly likes hibernateTemplate.saveOrUpdate(...) issue 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 "hibernateTemplate.saveOrUpdate(...) issue" Watch "hibernateTemplate.saveOrUpdate(...) issue" New topic
Author

hibernateTemplate.saveOrUpdate(...) issue

Benson Fung
Ranch Hand

Joined: Apr 10, 2003
Posts: 207
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
Hebert Coelho
Ranch Hand

Joined: Jul 14, 2010
Posts: 754

How are you saving this values?


[uaiHebert.com] [Full WebApplication JSF EJB JPA JAAS with source code to download] One Table Per SubClass [Web/JSF]
Benson Fung
Ranch Hand

Joined: Apr 10, 2003
Posts: 207
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.


abani patra
Ranch Hand

Joined: Oct 11, 2011
Posts: 70
Hi,
Please use cascade=all for the mapping to B class so your problem will be solved.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: hibernateTemplate.saveOrUpdate(...) issue
 
Similar Threads
Problem while using JPA, EJB 3.0
Problem in generating the Primary Key using a Sequence
iBatis and Bidirectional Association
hibernate cascading question
OneToOne composite key