【问题标题】:Spring Boot/JPA: Quoted reserved word column name not workingSpring Boot/JPA:引用的保留字列名不起作用
【发布时间】:2018-06-15 05:44:07
【问题描述】:

我有一个 Oracle 表,其中有一列以保留字 (TYPE) 命名

这在实体类中定义为

@Entity
@Table(name="PROCESS_STORAGE")
.....
@Column(name="\"TYPE\"")
private String type;

但是尝试从数据库中检索记录会导致错误:

2018-01-05 11:50:54.139  WARN 9340 --- [http-nio-8080-exec-3] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 904, SQLState: 42000
2018-01-05 11:50:54.139 ERROR 9340 --- [http-nio-8080-exec-3] o.h.engine.jdbc.spi.SqlExceptionHelper   : ORA-00904: "PROCESSSTO0_"."type": invalid identifier
2018-01-05 11:50:54.144 ERROR 9340 --- [http-nio-8080-exec-3] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessResourceUsageException: could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet] with root cause

java.sql.SQLSyntaxErrorException: ORA-00904: "PROCESSSTO0_"."type": invalid identifier

不太清楚我在这里缺少什么。好像是引用了列名,但是没有找到?

编辑 我刚刚意识到这是一个区分大小写的问题——该列在 DB (TYPE) 和实体定义中是大写的,但由于某种原因,它在查询中被转换为小写。仍然不确定为什么会发生这种情况

【问题讨论】:

  • SQL 关键字不区分大小写
  • 但这不是关键字,而是列名,在Oracle 中显然区分大小写。我已经通过直接查询数据库来确认: SELECT "type" FROM PROCESS_STORAGE;失败,从 PROCESS_STORAGE 中选择“类型”;作品

标签: oracle jpa spring-boot


【解决方案1】:

认为我找到了解决方案 here

在application.properties中,需要指定

spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

【讨论】:

    【解决方案2】:

    您需要用反引号(`)将保留关键字括起来以将其用作列

    TYPEOracle 中保留,所以应该是

    @Column(name="`TYPE`")
    private String type;
    

    【讨论】:

    • 谢谢,试过了,但它仍然将查询的列名转换为小写并给出相同的错误
    【解决方案3】:

    如果您使用的是 Hibernate 3.5+,请尝试:

    hibernate.globally_quoted_identifiers=true 引用所有数据库标识符,这是为 JPA 2.0 添加的。

    在 JPA 2.0 中,语法被标准化为:

    @Column(name="\"TYPE\"")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-07-11
      • 2018-12-09
      • 2018-07-26
      • 2020-05-09
      • 1970-01-01
      • 1970-01-01
      • 2020-04-10
      相关资源
      最近更新 更多