Hibernate(?) 在创建数据库时引用数据类型

问题描述 投票:0回答:1

我担心,对于以下问题,我找不到任何有用的谷歌搜索。

问题

在我的 Spring Boot (3.3.3) 应用程序中,以下实体在 MariaDB(Docker、mariadb:11.2.2-jammy)中创建表失败:

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

日志

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']

原因

这里的问题是,在生成的 SQL 代码中,LONGTEXT 用引号引起来。删除引号时我可以手动成功运行代码。我不明白为什么要加引号。

根本原因?

有什么想法吗?

附加信息:

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>

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

Dockerfile 的 .env 文件

MYSQL_DATABASE=redacted
SPRING_JPA_HIBERNATE_DDL_AUTO=create
SPRING_H2_CONSOLE_ENABLED=false
spring-boot hibernate spring-data-jpa mariadb-11
1个回答
0
投票

所以,我相信你可以做到以下几点。

package com.gpt.testmaridb;


import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Lob;
import jakarta.persistence.Table;

@Entity
@Table(name = "answer")
public final class Answer {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Lob
    @Column(columnDefinition="LONGTEXT")
    private String text;
}

我实际上继续启动了一个应用程序进行测试,并确认这确实有效

longtext
@Lob
本身没有列定义
tinytext
。我不确定为什么你的代码不起作用。

这些是我的app.props。

spring.application.name=test-maridb
spring.datasource.url=jdbc:mariadb://mariadb:3306/mydatabase
spring.datasource.username=myuser
spring.datasource.password=secret
spring.datasource.driver-class-name=org.mariadb.jdbc.Driver
spring.jpa.hibernate.ddl-auto=update

还有我的 pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>3.3.4</version>
    <relativePath/> <!-- lookup parent from repository -->
  </parent>
  <groupId>com.gpt</groupId>
  <artifactId>test-maridb</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>test-maridb</name>
  <description>test-maridb</description>
  <url/>
  <licenses>
    <license/>
  </licenses>
  <developers>
    <developer/>
  </developers>
  <scm>
    <connection/>
    <developerConnection/>
    <tag/>
    <url/>
  </scm>
  <properties>
    <java.version>17</java.version>
  </properties>
  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-devtools</artifactId>
      <scope>runtime</scope>
      <optional>true</optional>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-docker-compose</artifactId>
      <scope>runtime</scope>
      <optional>true</optional>
    </dependency>
    <dependency>
      <groupId>org.mariadb.jdbc</groupId>
      <artifactId>mariadb-java-client</artifactId>
      <scope>runtime</scope>
    </dependency>
    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <optional>true</optional>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-jpa</artifactId>
      <version>RELEASE</version>
      <scope>compile</scope>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <configuration>
          <excludes>
            <exclude>
              <groupId>org.projectlombok</groupId>
              <artifactId>lombok</artifactId>
            </exclude>
          </excludes>
        </configuration>
      </plugin>
    </plugins>
  </build>

</project>

我建议使用像 Liquibase 这样的数据库管理系统。这确实是处理迁移、数据库结构和更改的好方法,并且可以提供更好的数据库和更轻松的开发人员/维护人员体验。

© www.soinside.com 2019 - 2024. All rights reserved.