File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes Constraint Violation Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Constraint Violation Question" Watch "Constraint Violation Question" New topic
Author

Constraint Violation Question

Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1066
Hello,

I’m trying to create a simple example of a M:M relationship in Hibernate using a STUDENTS-COURSES database.

The single table queries/updates work fine, but when trying to add a record to the M:M resolver table, I get this exception:

Exception: Cannot add or update a child row: a foreign key constraint fails (`student_courses`.`studentcourse`, CONSTRAINT `studentcourse_ibfk_2` FOREIGN KEY (`COURSE_ID`) REFERENCES `COURSE` (`COURSE_ID`))
at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:74)I’

and also,

Caused by: javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (`student_courses`.`studentcourse`, CONSTRAINT `studentcourse_ibfk_2` FOREIGN KEY (`COURSE_ID`) REFERENCES `COURSE` (`COURSE_ID`))

-----------

I can add a M:M record manually, however so the database allows records being added to the STUDENT_COURSE (the M:M resolver) table.

Here’s the code from my "main" method:



Here’s the SQL that defines the simple database:



The entity class for StudentCourse is:



I don’t have a standard DAO object and am using the EntityManager and related classes as shown above. Don’t think that’s the issue, however, since the single table queries work fine.

Look forward to any suggestions and replies.

- Mike
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2370
    
  28

First of all, JPA supports ManyToMany relationship. You shouldn't need to create an Entity class for StudentCourse table. You just need Student and COurse, and in the annotation you can use JPA to use the StudentCourse relationship table

Secondly, in your code you set calCourse to StudentCOurse, and then set a new Course to StudentCourse. Is that what you intended to do. The problem could be that the new COurse is not being saved properly. Can you post your COurse class? Is the ID tag placed on Course correctly?

Third, your naming conventions are a bit off. Usually, you would name your methods as StudentCourse.getStudent and StudentCourse.getCourse, not StudentCourse.getStudentbyStudentId and StudentCourse.getCourseByCourseId,

Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1066
Jayesh A Lalwani wrote:First of all, JPA supports ManyToMany relationship. You shouldn't need to create an Entity class for StudentCourse table. You just need Student and COurse, and in the annotation you can use JPA to use the StudentCourse relationship table

Secondly, in your code you set calCourse to StudentCOurse, and then set a new Course to StudentCourse. Is that what you intended to do. The problem could be that the new COurse is not being saved properly. Can you post your COurse class? Is the ID tag placed on Course correctly?

Third, your naming conventions are a bit off. Usually, you would name your methods as StudentCourse.getStudent and StudentCourse.getCourse, not StudentCourse.getStudentbyStudentId and StudentCourse.getCourseByCourseId,



What I was trying to do was to get course and student object references to "feed" to a Hibernate method to then persist the new record in the M:M table.

The naming was done by Intellij 12, not me, and it doesn't create DAO objects as far as I can see.

Here's the Course Class generated by Intellij Entity reverse engineering.



---------------

I made these changes to the "main" class and it now works:



Do you know if there's any way to generate DAOs using Intellij? I can't find a plugin that works.

Thanks,

Mike
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Constraint Violation Question