Search code examples
javaspring-boothibernatespring-data-jpamariadb-11

Hibernate(?) quotes datatype when created database


I fear, I could not find anything helpful googling, for the following issue.

Issue

in my Spring Boot (3.3.3) app the table creation in MariaDB (Docker, mariadb:11.2.2-jammy) fails for the following entity:

    @Entity
    public final class Answer {
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        private Long id;
    
        @NotNull
        @Column(columnDefinition="LONGTEXT")
        private String text;
    }

Logs

Hibernate: create table `answer` (`id` bigint not null, `text` `LONGTEXT` not null, primary key (`id`)) engine=InnoDB
2024-09-19 23:59:21.367 [main] WARN  o.m.jdbc.message.server.ErrorPacket : Error: 4161-HY000: Unknown data type: 'LONGTEXT'
2024-09-19 23:59:21.367 [main] WARN  o.h.t.s.i.ExceptionHandlerLoggedImpl: GenerationTarget encountered exception accepting command : Error executing DDL "create table `answer` (`id` bigint not null, `text` `LONGTEXT` not null, primary key (`id`)) engine=InnoDB" via JDBC [(conn=415) Unknown data type: 'LONGTEXT']

Cause

The issue here is, that in the generated SQL code the LONGTEXT comes in quotes. I can manually run the code successful when removing the quotes. I don't understand why the quotes are there.

Root cause?

Any ideas?

Additional information:

pom.xml

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope> <!-- for local development -->
        </dependency>
        <dependency>
            <groupId>org.mariadb.jdbc</groupId>
            <artifactId>mariadb-java-client</artifactId>
            <version>3.4.1</version>
        </dependency>

Environment config lines in Dockerfile:

      - SPRING_DATASOURCE_URL=jdbc:mariadb://mariadb:3306/${MYSQL_DATABASE}
      - SPRING_DATASOURCE_DRIVER_CLASS_NAME=org.mariadb.jdbc.Driver
      - SPRING_DATASOURCE_INITIALIZATION_MODE=always
      - SPRING_H2_CONSOLE_ENABLED=false
      - SPRING_JPA_HIBERNATE_DDL_AUTO=${SPRING_JPA_HIBERNATE_DDL_AUTO}
      - SPRING_JPA_SHOW_SQL=true
      - SPRING_JPA_DATABASE_PLATFORM=org.hibernate.dialect.MariaDBDialect

.env file for Dockerfile

MYSQL_DATABASE=redacted
SPRING_JPA_HIBERNATE_DDL_AUTO=create
SPRING_H2_CONSOLE_ENABLED=false

Solution

  • The problem was self-made. I overlooked this config line in a run configuration.

    spring.jpa.properties.hibernate.globally_quoted_identifiers
    

    If I understand this post correctly, if you would need the property you could avoid my issue with the quote-keeping by adding:

    spring.jpa.properties.hibernate.globally_quoted_identifiers_skip_column_definitions=true