Hibernate 和 Spring Data JPA 无法处理带有特殊字符的 Oracle 表名?
Posted
技术标签:
【中文标题】Hibernate 和 Spring Data JPA 无法处理带有特殊字符的 Oracle 表名?【英文标题】:Hibernate and Spring DataJPA can not handle Oracle table name with special character? 【发布时间】:2017-02-01 04:54:24 【问题描述】:我在使用Spring datajpa和Hibenate读取Oracle的时候遇到了一个错误,我会这样描述:
Oracle 表名包含特殊字符(双引号),所以我必须使用SELECT * FROM "Graph" WHERE "ID"=1
。
所以,我在使用hibernate写实体类的时候,必须指出它的特殊名称。
这里是用来创建"Graph"
表的ddl:
CREATE TABLE "ATLASCOPCO_TOOLSNET"."Graph" (
"ID" NUMBER(19) NOT NULL ,
"ResultID" NUMBER(19) NOT NULL ,
"GraphTypeID" NUMBER(19) NOT NULL ,
"SampleTime" BINARY_FLOAT NOT NULL ,
"AngleOffset" BINARY_FLOAT NOT NULL ,
"GraphValues" BLOB NOT NULL ,
"AngleFactor" BINARY_DOUBLE NULL ,
"TorqueFactor" BINARY_DOUBLE NULL ,
"StartTime" BINARY_FLOAT NULL ,
"EndTime" BINARY_FLOAT NULL
)
LOGGING
NOCOMPRESS
NOCACHE
当我像这样使用普通表名时:
@Entity
@Table(name = "Graph")
@Getter
@Setter
public class Graph
@Id
@Column(name = "ID\"")
private Long id;
@Column(name = "ResultID")
private Long resultId;
@Column(name = "GraphTypeID")
private Long graphTypeID;
@Column(name = "SampleTime")
private BINARY_FLOAT sampleTime;
@Column(name = "AngleOffset")
private BINARY_FLOAT angleOffset;
@Column(name = "GraphValue")
private byte[] graphValue;
@Column(name = "AngleFactor")
private BINARY_DOUBLE angleFactor;
@Column(name = "TorqueFactor")
private BINARY_DOUBLE torqueFactor;
@Column(name = "StartTime")
private BINARY_FLOAT startTime;
@Column(name = "EndTime")
private BINARY_FLOAT endTime;
我收到错误:Caused by: java.sql.SQLSyntaxErrorException: ORA-00972: identifier is too long
当我使用这个时:
@Entity
@Table(name = "\"Graph\"")
@Getter
@Setter
public class Graph
@Id
@Column(name = "\"ID\"\"")
private Long id;
@Column(name = "\"ResultID\"")
private Long resultId;
@Column(name = "\"GraphTypeID\"")
private Long graphTypeID;
@Column(name = "\"SampleTime\"")
private BINARY_FLOAT sampleTime;
@Column(name = "\"AngleOffset\"")
private BINARY_FLOAT angleOffset;
@Column(name = "\"GraphValue\"")
private byte[] graphValue;
@Column(name = "\"AngleFactor\"")
private BINARY_DOUBLE angleFactor;
@Column(name = "\"TorqueFactor\"")
private BINARY_DOUBLE torqueFactor;
@Column(name = "\"StartTime\"")
private BINARY_FLOAT startTime;
@Column(name = "\"EndTime\"")
private BINARY_FLOAT endTime;
我收到了Caused by: java.sql.SQLSyntaxErrorException: ORA-00911: invalid character
【问题讨论】:
您的查询等于SELECT * FROM Graph WHERE ID=1
见***.com/a/13568581
【参考方案1】:
删除列名中的特殊字符\"
,这是导致问题的原因。 JPA 和 Oracle 驱动程序都会正确处理。
您可以省略"
,ANSI SQL 也可以。
CREATE TABLE NEW_TABLE (ID NUMBER(19) NOT NULL , NAME VARCHAR2(19) NOT NULL);
【讨论】:
【参考方案2】:假设您在一个名为 "id" 的表中有一个列,如下所示:
然后要使用特殊字符("),您需要添加如下转义序列:
@Column(name="`\"id\"`")
private int id;
结果将是一个名称为 ("id") 的列。 请找到以下更新的 Graph POJO 类:
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table (name="`\"Graph\"`")
@Getter
@Setter
public class Graph
@Id
@Column (name="`\"ID\"`")
private Long id;
@Column (name="`\"ResultID\"`")
private Long resultId;
@Column(name="`\"GraphTypeID\"`")
private Long graphTypeID;
@Column (name="`\"SampleTime\"`")
private BINARY_FLOAT sampleTime;
@Column (name="`\"AngleOffset\"`")
private BINARY_FLOAT angleOffset;
@Column (name="`\"GraphValue\"`")
private byte[] graphValue;
@Column (name="`\"AngleFactor\"`")
private BINARY_DOUBLE angleFactor;
@Column (name="`\"TorqueFactor\"`")
private BINARY_DOUBLE torqueFactor;
@Column (name="`\"StartTime\"`")
private BINARY_FLOAT startTime;
@Column (name="`\"EndTime\"`")
private BINARY_FLOAT endTime;
要从图表中检索数据,请在 Hibernate 中使用以下代码:
Query query=session.createSQLQuery("SELECT * FROM `\" Graph \"` WHERE `\"ID\"`=1 ");
希望对你有所帮助。
【讨论】:
以上是关于Hibernate 和 Spring Data JPA 无法处理带有特殊字符的 Oracle 表名?的主要内容,如果未能解决你的问题,请参考以下文章
Spring + Hibernate 审计(无 Spring Data)
spring-data-jpa+hibernate 各种缓存的配置演示
Hibernate 命名查询和 Spring Data SQL 注入
使用 Spring Boot 和 Spring Data JPA 的 Hibernate 拦截器或侦听器