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 (Hibernate operation: could not insert: 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 "(Hibernate operation: could not insert:" Watch "(Hibernate operation: could not insert:" New topic

(Hibernate operation: could not insert:

Neha Gap

Joined: May 30, 2007
Posts: 29

I have two tables with bi-directional association between the two. The hbm.xml files, DAOs, model classes and interfaces are all generated by middlegen. I'm constructing the objects of these generated model classes using Spring's ApplicationContext. The child object has a reference to the parent object in the spring config xml file. I try to insert an object of type child using hibernate's generated DAOs and get the following error

<error message="(Hibernate operation: could not insert: [child]): data integrity violated by SQL 'insert into child
nested exception is java.sql.SQLException: Cannot add or update a child row: a foreign key constraint fails (``, CONSTRAINT `FK9EB1B4A760778BE5` FOREIGN KEY (`parentID`) REFERENCES `parent` (`parent_id`))" type="org.springframework.dao.DataIntegrityViolationException">org.springframework.dao.DataIntegrityViolationException: (Hibernate operation: could not insert: []): data integrity violated by SQL '

Any and all help would be greatly appreciated.
- Neha.
Emanuel Kadziela
Ranch Hand

Joined: Mar 24, 2005
Posts: 186
Make sure you're setting the parent reference on the child object.
Neha Gap

Joined: May 30, 2007
Posts: 29
The DAOs are all middlegen generated. I am not writing the code for child-parent references.

- Neha.
I agree. Here's the link:
subject: (Hibernate operation: could not insert:
Similar Threads
Problem with iBATIS + MySQL + auto increment keys
Save parent and children on cascade - parent key not found
Getting an error when trying to insert data
Spring + Hibernate
insert new parent/child row. How to handle new primary/foreign key inserts