简体   繁体   中英

JHipster Post REST API Displays SQL Error

i am creating a project with using JHipster. I have 2 entity(Course,Student) and they have many-to many relation. So there is an another entity called Course_Student and it has 2 column(student_id,course_id). Students(users) need to register course with clicking Register Course button on their own panel. So i wrote a query and rest API function.

Here is my Query function:

@Modifying
@Query(value="insert into COURSE_STUDENT (STUDENTS_ID, COURSES_ID) VALUES (:studentId,:courseId)",nativeQuery = true) 
@Transactional
void registerCourse(@Param("studentId") Long studentId, @Param("courseId") Long courseId);

And Rest API function:

@PostMapping("/registercourse/{studentId}/{courseId}")
@Timed
public ResponseEntity<Void> registerCourse(@PathVariable Long studentId,@PathVariable Long courseId) {
    log.debug("REST request to register {} Course : {} Student : {}", courseId,studentId);
    courseRepository.registerCourse(studentId,studentId);
    return ResponseEntity.ok().headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, courseId.toString())).build();
}

But when i try to run registerCourse API with Using Swagger console displays these errors: (i entered student id:33, course id: 1)

c.m.m.w.rest.errors.ExceptionTranslator  : An unexpected error occurred: could not execute statement; SQL [n/a]; constraint ["FK_COURSE_STUDENT_COURSES_ID: PUBLIC.COURSE_STUDENT FOREIGN KEY(COURSES_ID) REFERENCES PUBLIC.COURSE(ID) (33)"; SQL statement:
insert into COURSE_STUDENT (STUDENTS_ID, COURSES_ID) VALUES (?,?) [23506-196]]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement
org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint ["FK_COURSE_STUDENT_COURSES_ID: PUBLIC.COURSE_STUDENT FOREIGN KEY(COURSES_ID) REFERENCES PUBLIC.COURSE(ID) (33)"; SQL statement:
insert into COURSE_STUDENT (STUDENTS_ID, COURSES_ID) VALUES (?,?) [23506-196]]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement
        at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:278)
Caused by: org.hibernate.exception.ConstraintViolationException: could not execute statement
        at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:112)
        ... 145 common frames omitted
Caused by: org.h2.jdbc.JdbcSQLException: Referential integrity constraint violation: "FK_COURSE_STUDENT_COURSES_ID: PUBLIC.COURSE_STUDENT FOREIGN KEY(COURSES_ID) REFERENCES PUBLIC.COURSE(ID) (33)"; SQL statement:
insert into COURSE_STUDENT (STUDENTS_ID, COURSES_ID) VALUES (?,?) [23506-196]
        at org.h2.message.DbException.getJdbcSQLException(DbException.java:345)
        ... 163 common frames omitted
2017-09-08 08:58:57.099  WARN 7528 --- [  XNIO-2 task-8] .m.m.a.ExceptionHandlerExceptionResolver : Resolved exception caused by Handler execution: org.springframework.

You are passing the student_id as the course_id, and no course exists with the id of 33. See the 3rd line of your Rest API's registerCourse :

    courseRepository.registerCourse(studentId,studentId);

change to:

    courseRepository.registerCourse(studentId,courseId);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM