When I try to make a column value, unique = true I get the following error.
org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "
alter table student
add constraint UK_fe0i52si7ybu0wjedj6motiim unique (email)" via JDBC Statement
....
....
Caused by: java.sql.SQLSyntaxErrorException: BLOB/TEXT column 'email' used in key specification without a key length
This is my application properties file.
spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/dataJpaDB
spring.datasource.username=root
spring.datasource.password=1234
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.show-sql=true
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL8Dialect
spring.jpa.properties.hibernate.format_sql=true
server.error.include-message=always
I'm using MySQL version 8.
This is my entity class
@Data
@Entity(name = "Student")
public class Student {
@Id
@SequenceGenerator(
name = "student_sequence",
sequenceName = "student_sequence",
allocationSize = 1
)
@GeneratedValue(
strategy = GenerationType.SEQUENCE,
generator = "student_sequence"
)
@Column(
name = "id",
updatable = false
)
private Long id;
@Column(
name = "first_name",
nullable = false,
columnDefinition = "TEXT"
)
private String firstName;
@Column(
name = "last_name",
nullable = false,
columnDefinition = "TEXT"
)
private String lastName;
@Column(
name = "email",
nullable = false,
columnDefinition="TEXT",
unique = true /// this is causing the problem
)
private String email;
@Column(
name = "age",
nullable = false,
columnDefinition = "INTEGER"
)
private Integer age;
public Student(Long id, String firstName, String lastName, String email, Integer age) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
this.age = age;
}
}
When I remove the "unique = true" everything seems to work fine. What is causing this problem and how to resolve it.
P.S. sorry for the long post.