【问题标题】:Spring data issue when upgrading spring boot to 2.3.0 [duplicate]将 Spring Boot 升级到 2.3.0 时出现 Spring 数据问题 [重复]
【发布时间】:2020-05-26 07:41:58
【问题描述】:

该项目目前在 spring-boot 2.27 上(它是相应的 spring-data-jdbc 版本)在 maven 中升级版本并运行时,我收到以下信息:

bad SQL grammar [INSERT INTO "PLAYLIST" ("allocated_funds", "file_uri", "received") VALUES (?, ?, ?)]; nested exception is org.postgresql.util.PSQLException: ERROR: relation "PLAYLIST" does not exist

org.springframework.data.relational.core.conversion.DbActionExecutionException: Failed to execute DbAction.InsertRoot(entity=Playlist(id=null, fileUri=gs://playlists/success.csv, received=2020-05-26T09:34:45.778327, allocatedFunds=20000))

我正在使用 flyway 来管理我的数据库,它正在被测试执行:

09:34:43.868 [main] INFO  com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Start completed.
09:34:43.897 [main] INFO  o.f.c.i.database.DatabaseFactory - Database: jdbc:postgresql://localhost:32883/test (PostgreSQL 12.3)
09:34:43.947 [main] INFO  o.f.core.internal.command.DbValidate - Successfully validated 4 migrations (execution time 00:00.019s)
09:34:43.970 [main] INFO  o.f.c.i.s.JdbcTableSchemaHistory - Creating Schema History table "public"."flyway_schema_history" ...
09:34:44.010 [main] INFO  o.f.core.internal.command.DbMigrate - Current version of schema "public": << Empty Schema >>
INFO  o.f.core.internal.command.DbMigrate - Migrating schema "public" to version 1 - init
INFO  o.f.core.internal.command.DbMigrate - Migrating schema "public" to version 2 - playlist entry
INFO  o.f.core.internal.command.DbMigrate - Migrating schema "public" to version 3 - splits
INFO  o.f.core.internal.command.DbMigrate - Migrating schema "public" to version 4 - allocated funds
INFO  o.f.core.internal.command.DbMigrate - Successfully applied 4 migrations to schema "public" (execution time 00:00.255s)

实体看起来像:

import java.io.Serializable;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.Id;
import org.springframework.data.relational.core.mapping.Column;
import org.springframework.data.relational.core.mapping.Table;

@Data
@Builder
@AllArgsConstructor
@Table("PLAYLIST")
public class Playlist implements Serializable {
    @Id
    private Long id;

    @Column("file_uri")
    private String fileUri;

    private LocalDateTime received;

    @Column("allocated_funds")
    private BigDecimal allocatedFunds;
}

创建脚本如下:

CREATE TABLE PLAYLIST(id BIGSERIAL PRIMARY KEY, file_uri VARCHAR(400) NOT NULL, received TIMESTAMP NOT NULL);
ALTER TABLE PLAYLIST ADD COLUMN allocated_funds DECIMAL(10,2) NOT NULL;

WRT datetime 或 bigdecimal 处理是否存在一些潜在变化或缺少 ID?

  • 更新 - 添加存储库代码

import org.springframework.data.jdbc.repository.query.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;
import treeline.entities.Playlist;

public interface PlaylistRepository extends CrudRepository<Playlist, Long> {
    @Query("SELECT * FROM Playlist p WHERE p.file_uri = :fileUri")
    Playlist findByFileUri(@Param("fileUri") final String fileUri);
}

【问题讨论】:

  • 引用的对象名称区分大小写,因此您可能有一个表playlist(小写),但您插入的PLAYLIST 不存在。禁用引用,或尝试删除@Table 注释,或将表格放入小写。
  • 表格播放列表的创建语句已提供为大写
  • 是的,但是它是不带引号的,并且不带引号的对象名称不区分大小写,这意味着 PostgreSQL 会将其存储为 小写 (其他数据库系统例如将不带引号的名称存储在大写)。这意味着PLAYLIST 实际上是playlist,而"PLAYLIST" 将不匹配。
  • @Table 中改为小写并已排序

标签: java spring-boot spring-data-jdbc


【解决方案1】:

看起来像

INSERT INTO "PLAYLIST"

是问题所在。您可以尝试不使用引号吗?只需将查询编写如下

INSERT INTO PLAYLIST

【讨论】:

  • spring-data CrudRepository 正在生成查询。 - 添加了用于保存的存储库代码
  • 您粘贴了选择查询而不是插入。无论如何,查询在您的控制之中。我建议您从查询中删除引号。
  • save 功能是 CrudRepository 的一部分。我只是展示我对那个实体的看法。我已经解决了这个问题。将表名改为小写
猜你喜欢
  • 1970-01-01
  • 2022-12-04
  • 1970-01-01
  • 1970-01-01
  • 2023-04-01
  • 2020-10-09
  • 2022-01-18
  • 1970-01-01
  • 2019-06-08
相关资源
最近更新 更多