Search code examples
hibernatejpaspring-data-jpaspring-dataspring-jdbc

Jakarta Persistence - create table


I have this entity:

   @Entity
    @Table(name = "t_message",
            uniqueConstraints = {
                    @UniqueConstraint(columnNames = "key"),
                    @UniqueConstraint(columnNames = "lang")
            })
    @Getter
    @Setter
    @AllArgsConstructor
    @NoArgsConstructor
    @Builder
    public class Message {
    
        @Id
        private String key;
        @Column(length = 10000)
        private String description;
        @Column(length = 2)
        private String lang;
    
    }

but when I start the app I have this error:

Caused by: java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'varchar(255) not null, description varchar(10000), lang varchar(2), primary key ' at line 1
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:121)
    at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
    at com.mysql.cj.jdbc.StatementImpl.executeInternal(StatementImpl.java:763)
    at com.mysql.cj.jdbc.StatementImpl.execute(StatementImpl.java:648)
    at com.zaxxer.hikari.pool.ProxyStatement.execute(ProxyStatement.java:94)
    at com.zaxxer.hikari.pool.HikariProxyStatement.execute(HikariProxyStatement.java)
    at org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.accept(GenerationTargetToDatabase.java:78)
    ... 132 common frames omitted

Solution

  • MySQL has 'key' listed as a reserved word, which is causing table generation to fail. You can define a different column for JPA to use for the table with the column annotation:

        @Id
        @Column(name="message_id")
        private String key;