posted 12 years ago
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