Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Constraint Violation Question

 
Bartender
Posts: 1832
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 2759
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 1832
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic