0

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
v.ladynev
  • 19,275
  • 8
  • 46
  • 67
Pratyush
  • 41
  • 5
  • You are using Hibernate annotation "@Type(type="uuid-char")" on your other UUID mappings - why wouldn't you use it consistently on them all? Specifically, the UUID courseId is defining a column mapping, but not the type to use underneath. This forces the JPA provider to guess that the type based on the two mappings involved - and it seems to default to this column/string mapping instead of the ManyToOne definition.Adding type might fix it, but I'd also suggest using a String instead of UUID for the java type - it just makes things easier. – Chris Feb 11 '22 at 17:35
  • Thanks @Chris. I did the same as you sugguested and it worked. Also, I noticed that by default JPA by default create column of type VARCHAR(255) which means it is comsidering UUID type of java as simple String. ' @Column(name="course_id",insertable = false,updatable = false,nullable = false,columnDefinition = "CHAR(36)") @Type(type="uuid-char") private UUID courseId; ' This one worked for me. – Pratyush Feb 12 '22 at 12:22

1 Answers1

0

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;

}
v.ladynev
  • 19,275
  • 8
  • 46
  • 67