Home > OS >  Incompatible type referencing and referenced column in Spring Data JPA
Incompatible type referencing and referenced column in Spring Data JPA

Time:02-12

I want my IDs to be uuid and there should be Many-To-One relationship between Student and course. I am getting incompitable types referencing column and referenced column.

Student.java

package com.test.model;

import java.util.UUID;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.hibernate.annotations.Type;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
@Entity
@Table(name="student",schema="mydb")
public class Student {
    
    @Id
    @Type(type="uuid-char")
    @Column(name="student_id")
    private UUID studentId;
    
    @Column(name="name")
    private String sudentName;
    
    @ManyToOne
    @JoinColumn(name="course_id")
    private Course course;
    
    @Column(name="course_id",insertable = false,updatable = false,nullable = false)
    private UUID courseId;

}

Course.java

package com.test.model;

import java.util.UUID;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

import org.hibernate.annotations.Type;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
@Entity
@Table(name="course",schema="mydb")
public class Course {
    
    @Id
    @Type(type="uuid-char")
    @Column(name="id")
    private UUID id;
    
    @Column(name="courseName")
    private String courseName;

}

Exception

Caused by: java.sql.SQLException: Referencing column 'course_id' and referenced column 'id' in foreign key constraint 'FKdfypyqt0stgfc0aij9kcxm99s' are incompatible.
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129) ~[mysql-connector-java-8.0.28.jar:8.0.28]
    at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122) ~[mysql-connector-java-8.0.28.jar:8.0.28]
    at com.mysql.cj.jdbc.StatementImpl.executeInternal(StatementImpl.java:763) ~[mysql-connector-java-8.0.28.jar:8.0.28]
    at com.mysql.cj.jdbc.StatementImpl.execute(StatementImpl.java:648) ~[mysql-connector-java-8.0.28.jar:8.0.28]
    at com.zaxxer.hikari.pool.ProxyStatement.execute(ProxyStatement.java:94) ~[HikariCP-4.0.3.jar:na]
    at com.zaxxer.hikari.pool.HikariProxyStatement.execute(HikariProxyStatement.java) ~[HikariCP-4.0.3.jar:na]
    at org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.accept(GenerationTargetToDatabase.java:54) ~[hibernate-core-5.6.4.Final.jar:5.6.4.Final]
    ... 33 common frames omitted

CodePudding user response:

Please learn how to create associations

what is @JoinColumn and how it is used in Hibernate

Never use raw ids like courseId! You already have @ManyToOne association with @JoinColumn annotation. Hibernate is smart enough to understand that course_id will be used as a foreign key.

@Table(name="student",schema="mydb")
public class Student {
    
    @Id
    @Type(type="uuid-char")
    @Column(name="student_id")
    private UUID studentId;
    
    @Column(name="name")
    private String sudentName;
    
    @ManyToOne
    @JoinColumn(name="course_id")
    private Course course;

}
  • Related