Sridhar Karuppusamy
Sridhar Karuppusamy

Reputation: 422

Spring Boot JPA - SQLIntegrityConstraintViolationException

I'm Trying to save the value using jpa. I Got SQLIntegrityConstraintViolationException.

Database ER Diagram

My Model Class - (user_answers) Table:

import org.hibernate.annotations.Type
import java.io.Serializable
import java.util.*
import javax.persistence.Column
import javax.persistence.Entity
import javax.persistence.Id
import javax.persistence.Table

@Entity
@Table(name = "user_answers")
data class CustomUserAnswer (
    @Id
    @Type(type = "uuid-char")
    @Column(name = "id")
    var id: UUID = UUID.randomUUID(),
    @Column(name="answer")
    var answer: String = "",
    @Type(type = "uuid-char")
    @Column(name = "user_answer_id")
    var userAnswerId: UUID,
    @Column(name = "question_id")
    var questionId: UUID):Serializable{}

My Model Class - (user_answer_relt)

import org.hibernate.annotations.Type
import java.util.*
import javax.persistence.*

@Entity
@Table(name = "user_answer_relt")
data class UserAnswerRelt(
        @Id
        @Type(type = "uuid-char")
        @Column(name = "id")
        var id: UUID = UUID.randomUUID(),
        @Column(name="answer_submitted_by")
        var submittedUserName : String = "",
        @Type(type = "uuid-char")
        @Column(name="user_id")
        var userId : UUID
) {}

First I'm Save User_Answer_Relt Values and Get User_Answer_Id Then I Build User_Answer class with User_Answer_Id

So, Now I'm Trying to save the User_Answer Value That time I'm Getting the Below Issue.

Error :

> 19:49:43.079 [http-nio-9010-exec-1] ERROR
> o.h.e.jdbc.spi.SqlExceptionHelper - Cannot add or update a child row:
> a foreign key constraint fails (`test`.`user_answers`, CONSTRAINT
> `user_question_id_fk` FOREIGN KEY (`question_id`) REFERENCES
> `questions` (`id`)) 19:49:43.129 [http-nio-9010-exec-1] ERROR
> o.a.c.c.C.[.[.[.[dispatcherServlet] - Servlet.service() for servlet
> [dispatcherServlet] in context with path [] threw exception [Request
> processing failed; nested exception is
> org.springframework.dao.DataIntegrityViolationException: could not
> execute statement; SQL [n/a]; constraint [null]; nested exception is
> org.hibernate.exception.ConstraintViolationException: could not
> execute statement] with root cause
> java.sql.SQLIntegrityConstraintViolationException: Cannot add or
> update a child row: a foreign key constraint fails
> (`test`.`user_answers`, CONSTRAINT `user_question_id_fk` FOREIGN KEY
> (`question_id`) REFERENCES `questions` (`id`))

I know If The Question_Id is not available in the question table means it's throw the above exception but I double checked the data it's correct. but i don't know the exact reason.

I Also Tried the Join Column In the Model,But i Still got the same issue

import org.hibernate.annotations.Type
import java.util.*
import javax.persistence.*

@Entity
@Table(name = "user_answers")
data class UserAnswer(
        @Id
        @Type(type = "uuid-char")
        @Column(name = "id")
        var id: UUID = UUID.randomUUID(),
        @Column(name="answer")
        var answer: String = "",
        @Type(type = "uuid-char")
        @Column(name = "user_answer_id")
        var userAnswerId: UUID,
        @Column(name = "question_id")
        var questionId:UUID,
        @ManyToOne(fetch = FetchType.LAZY,cascade = arrayOf(CascadeType.ALL))
        @JoinColumn(name = "question_id",insertable = false,updatable = false)
        var question: Question?

){}

Upvotes: 0

Views: 1151

Answers (2)

Sridhar Karuppusamy
Sridhar Karuppusamy

Reputation: 422

Finally i found the actual issue.

import org.hibernate.annotations.Type
import java.io.Serializable
import java.util.*
import javax.persistence.Column
import javax.persistence.Entity
import javax.persistence.Id
import javax.persistence.Table

@Entity
@Table(name = "user_answers")
data class CustomUserAnswer (
    @Id
    @Type(type = "uuid-char")
    @Column(name = "id")
    var id: UUID = UUID.randomUUID(),
    @Column(name="answer")
    var answer: String = "",
    @Type(type = "uuid-char")
    @Column(name = "user_answer_id")
    var userAnswerId: UUID,
    @Column(name = "question_id")
    var questionId: UUID):Serializable{}

In the above code, I forgot to add Column internal type I made a fix by adding the following code.

     @Type(type = "uuid-char")
     @Column(name = "question_id")
     var questionId: UUID

Upvotes: 2

Chris
Chris

Reputation: 1804

Your entities are not reflecting the relationships between each other.

Hibernate will flush the changes in an order that does not necessarily reflect the order of your code, but if it knows how things are related it can send changes in a safe order.

CustomerUserAnswer should have properties of type UserAnswerRelt and Question rather than UUID and you will need @JoinColumn annotations.

Upvotes: 1

Related Questions