Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Constraint Violation Question

 
Mike London
Ranch Hand
Posts: 1192
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 2756
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1192
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic