Hibernate和Spring DataJPA无法处理具有特殊字符的Oracle表名?

时间:2016-09-23 10:48:27

标签: spring oracle hibernate spring-boot spring-data-jpa

当我使用Spring datajpa和Hibenate来阅读Oracle时,我遇到了一个错误,我会说明这个:

  1. Oracle表名包含特殊字符(双引号),因此,我必须使用SELECT * FROM "Graph" WHERE "ID"=1

  2. 因此,当我使用hibernate编写实体类时,我必须指出它的特殊名称。

  3. 这是用于创建"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

2 个答案:

答案 0 :(得分:0)

删除列名中的特殊字符\",这会导致问题。 JPA和Oracle驱动程序将正确处理所有内容。

您可以省略",ANSI SQL也可以。

CREATE TABLE NEW_TABLE (ID NUMBER(19) NOT NULL , NAME VARCHAR2(19) NOT NULL);

答案 1 :(得分:0)

假设您的表格中的列名称为" id" ,如下所示:

"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;
}

要从Graph表中检索数据,请使用Hibernate中的以下代码:

Query query=session.createSQLQuery("SELECT * FROM `\" Graph \"` WHERE `\"ID\"`=1 ");

希望这会对你有所帮助。