如何修复 org.h2.jdbc.JdbcSQLSyntaxErrorException:SQL 语句中的语法错误预期 "identifier"
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
是休眠文档的 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. See this section。
我正在为我的 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
是休眠文档的 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. See this section。