簡體   English   中英

如何修復 org.h2.jdbc.JdbcSQLSyntaxErrorException:SQL 語句中的語法錯誤需要“標識符”

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

我正在為我的 springboot 應用程序使用 H2 內存數據庫。 我啟用hibernate.ddl-auto地方。 當 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]
'''

下面是實體 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;
   }
}

下面是數據庫配置文件:

@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);
        }
    }

下面是 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"

嘗試糾正這一點:

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

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

由於OFFSET保留關鍵字,您應該強制 Hibernate 在生成的 SQL 中引用標識符,方法是在映射文檔中將列名括在反引號中。 請參閱 hibernate 文檔的這一部分

我遇到了同樣的問題,但沒有一個解決方案有效,但如果我執行以下操作,它就可以正常工作。

從“spring.jpa.database-platform=org.hibernate.dialect.H2Dialect”更新方言

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM