简体   繁体   English

如何修复 org.h2.jdbc.JdbcSQLSyntaxErrorException:SQL 语句中的语法错误需要“标识符”

[英]How to fix org.h2.jdbc.JdbcSQLSyntaxErrorException: Syntax error in SQL statement expected "identifier"

I am using H2 in-memory database for my springboot application.我正在为我的 springboot 应用程序使用 H2 内存数据库。 Where I have enabled hibernate.ddl-auto .我启用hibernate.ddl-auto地方。 I am getting below exception while hibernate is creating a schema当 hibernate 正在创建模式时,我遇到异常

Caused by: org.h2.jdbc.JdbcSQLSyntaxErrorException:
Syntax error in SQL statement "CREATE TABLE PRODUCT_OFFSET_INFO (ID BIGINT NOT NULL, MODIFIED_TIMESTAMP TIMESTAMP, OFFSET[*] BIGINT, TOPIC_NAME VARCHAR(255), PRIMARY KEY (ID))"; expected "identifier"; SQL statement:
create table PRODUCT_OFFSET_INFO (ID bigint not null, MODIFIED_TIMESTAMP timestamp, OFFSET bigint, TOPIC_NAME varchar(255), primary key (ID)) [42001-200]
'''

Below is the Entity class:下面是实体 class:

@Entity
@Table(name="PRODUCT_OFFSET_INFO")
public class ProductOffsetInfo implements Serializable
{
   private static final long serialVersionUID = -2147468513335906679L;
    
   @Id
   @Column(name="ID")
   private Long ProductId;
    
   @Column(name="TOPIC_NAME")
   private String topic_name;
    
   @Column(name="OFFSET")
   private Long offset;
    
   @Column(name = "MODIFIED_TIMESTAMP")
   private Date modified;
    
   public Long getProductId() {
       return ProductId;
   }
   public void setProductId(Long ProductId) {
       this.ProductId = ProductId;
   }
    
   public String getTopic_name() {
       return topic_name;
   }
   public void setTopic_name(String topic_name) {
       this.topic_name = topic_name;
   }
    
   public Long getOffset() {
       return offset;
   }
   public void setOffset(Long offset) {
       this.offset = offset;
   }
    
   public Date getModified() {
       return modified;
   }
    
   public void setModified(Date modified) {
       this.modified = modified;
   }
}

Below is the database config file:下面是数据库配置文件:

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
    entityManagerFactoryRef = "productEntityManagerFactory",
    transactionManagerRef = "productTransactionManager",
    basePackages = {"com.product.repository.product"}
)
@ComponentScan({"com.product.repository.product.impl"})
@EntityScan({"com.product.commons.entities.product.models","com.product.models.product"})
public class ProductDbConfig {

        @Autowired
        private JpaProperties jpaProperties;
        @Primary
        @Bean("productHikariConfig")
        @ConfigurationProperties(prefix = "spring.datasource")
        public HikariConfig hikariConfig() {
            return new HikariConfig();
        }
    
        @Primary
        @Bean(name = "productDataSource")
        @DependsOn("productHikariConfig")
        public DataSource dataSource(@Qualifier("productHikariConfig") HikariConfig hikariConfig) {
            return new HikariDataSource(hikariConfig);
        }
    
        @Primary
        @Bean(name = "productEntityManagerFactory")
        @PersistenceContext(unitName = "product")
        public LocalContainerEntityManagerFactoryBean entityManagerFactory(
                EntityManagerFactoryBuilder builder, @Qualifier("productDataSource") DataSource datasource) {
            return builder
                    .dataSource(datasource).properties(jpaProperties.getProperties())
                    .packages("com.product.commons.entities.product.models","com.product.models.product")
                    .persistenceUnit("product")
                    .build();
        }
    
        @Primary
        @Bean(name = "productTransactionManager")
        public PlatformTransactionManager transactionManager(
                @Qualifier("productEntityManagerFactory") EntityManagerFactory entityManagerFactory) {
            return new JpaTransactionManager(entityManagerFactory);
        }
    }

Below is the application.yml content for DB下面是 DB 的application.yml内容

    spring:
      profiles: mock
      jpa:
        database-platform: org.hibernate.dialect.H2Dialect
        generate-ddl: true
        hibernate:
          ddl-auto: create
      datasource:
        jdbcUrl: jdbc:h2:mem:PRODUCT
        driver-class-name: org.h2.Driver
        maximumPoolSize: 10
        minimumIdle: 5
        idleTimeout: 60000
        maxLifetime: 120000
        leakDetectionThreshold: 180000
        poolName: "product"

Try to correct this:尝试纠正这一点:

@Column(name="OFFSET")
private Long offset;

to

@Column(name="`OFFSET`")
private Long offset;

As OFFSET is reserved keyword you should force Hibernate to quote an identifier in the generated SQL by enclosing the column name in backticks in the mapping document.由于OFFSET保留关键字,您应该强制 Hibernate 在生成的 SQL 中引用标识符,方法是在映射文档中将列名括在反引号中。 See this section of hibernate documentation.请参阅 hibernate 文档的这一部分

I faced this same but none of the solutions worked, but if i did the below it worked fine.我遇到了同样的问题,但没有一个解决方案有效,但如果我执行以下操作,它就可以正常工作。

Updated the dialect from " spring.jpa.database-platform=org.hibernate.dialect.H2Dialect"从“spring.jpa.database-platform=org.hibernate.dialect.H2Dialect”更新方言

to "spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.H2Dialect"到“spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.H2Dialect”

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 org.h2.jdbc.JdbcSQLSyntaxErrorException:找不到列; SQL 声明 [SPRINGBOOT] - org.h2.jdbc.JdbcSQLSyntaxErrorException: Column not found; SQL statement [SPRINGBOOT] org.h2.jdbc.JdbcSQLSyntaxErrorException:找不到列“USER0_.PROFILE_ID”; SQL 声明: - org.h2.jdbc.JdbcSQLSyntaxErrorException : Column "USER0_.PROFILE_ID" not found; SQL statement: 插入时间戳记时,H2 org.h2.jdbc.JdbcSQLSyntaxErrorException - H2 org.h2.jdbc.JdbcSQLSyntaxErrorException when inserting a Timestamp org.h2.jdbc.JdbcSQLSyntaxErrorException h2 数据库 java - org.h2.jdbc.JdbcSQLSyntaxErrorException h2 database java H2版本升级后org.h2.jdbc.JdbcSQLSyntaxErrorException - org.h2.jdbc.JdbcSQLSyntaxErrorException after H2 version upgrade 如何修复错误<identifier>预期的?</identifier> - How to fix the error <identifier> expected? org.h2.jdbc.JdbcSQLDataException:数据转换错误转换“'MONTH'”; SQL 声明: - org.h2.jdbc.JdbcSQLDataException: Data conversion error converting “'MONTH'”; SQL statement: org.h2.jdbc.JdbcSQLException:未找到架构“MYAPP”; SQL语句 - org.h2.jdbc.JdbcSQLException: Schema “MYAPP” not found; SQL statement org.h2.jdbc.JdbcSQLException:未找到表“ PACCHETTIVISITETURISTICHE”; SQL语句: - org.h2.jdbc.JdbcSQLException: Table “PACCHETTIVISITETURISTICHE” not found; SQL statement: Spring 引导 JPA 初始化数据 - SQL 语句中的语法错误...预期“标识符” - Spring Boot JPA initialize data - Syntax error in SQL statement ... expected "identifier"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM