我有一个 SpringBoot 2.7.18 和 H2 2.3.232,使用以下 SQL 进行测试:
CREATE TABLE IF NOT EXISTS EXERCICE
(
EXERCICE_ID NUMBER AUTO_INCREMENT,
TYPE_SITE_ID NUMBER,
EXERCICE_META_ID NUMBER,
DESC_COURT VARCHAR(512),
DESC_LONG VARCHAR(2048),
DT_HORS_DELAI DATE,
DT_DEBUT DATE,
DT_FIN DATE,
DT_DEB_INSCRIPTION DATE,
DT_FIN_INSCRIPTION DATE,
DT_DEB_REINSCRIPTION DATE,
DT_FIN_REINSCRIPTION DATE,
DT_ANNULATION DATE,
COMMENTAIRE VARCHAR(2048),
KL_ID NUMBER,
CODE VARCHAR(64),
ORGANISATION_ID NUMBER
);
但是在运行测试时我遇到了这个错误:
Caused by: javax.persistence.PersistenceException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.7.15.v20240516-53511fdbd8): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: org.h2.jdbc.JdbcSQLSyntaxErrorException: Syntax error in SQL statement "CREATE TABLE IF NOT EXISTS EXERCICE\000d\000a(\000d\000a EXERCICE_ID NUMBER [*]AUTO_INCREMENT,\000d\000a TYPE_SITE_ID NUMBER,\000d\000a EXERCICE_META_ID NUMBER,\000d\000a DESC_COURT VARCHAR(512),\000d\000a DESC_LONG VARCHAR(2048),\000d\000a DT_HORS_DELAI DATE,\000d\000a DT_DEBUT DATE,\000d\000a DT_FIN DATE,\000d\000a DT_DEB_INSCRIPTION DATE,\000d\000a DT_FIN_INSCRIPTION DATE,\000d\000a DT_DEB_REINSCRIPTION DATE,\000d\000a DT_FIN_REINSCRIPTION DATE,\000d\000a DT_ANNULATION DATE,\000d\000a COMMENTAIRE VARCHAR(2048),\000d\000a KL_ID NUMBER,\000d\000a CODE VARCHAR(64),\000d\000a ORGANISATION_ID NUMBER\000d\000a)"; expected "(, ARRAY, INVISIBLE, VISIBLE, NOT NULL, DEFAULT, GENERATED, ON UPDATE, NOT NULL, DEFAULT ON NULL, SEQUENCE, SELECTIVITY, COMMENT, CONSTRAINT, COMMENT, PRIMARY KEY, UNIQUE, NOT NULL, CHECK, REFERENCES, ,, )"; SQL statement:
CREATE TABLE IF NOT EXISTS EXERCICE
(
EXERCICE_ID NUMBER AUTO_INCREMENT,
TYPE_SITE_ID NUMBER,
EXERCICE_META_ID NUMBER,
DESC_COURT VARCHAR(512),
DESC_LONG VARCHAR(2048),
DT_HORS_DELAI DATE,
DT_DEBUT DATE,
DT_FIN DATE,
DT_DEB_INSCRIPTION DATE,
DT_FIN_INSCRIPTION DATE,
DT_DEB_REINSCRIPTION DATE,
DT_FIN_REINSCRIPTION DATE,
DT_ANNULATION DATE,
COMMENTAIRE VARCHAR(2048),
KL_ID NUMBER,
CODE VARCHAR(64),
ORGANISATION_ID NUMBER
) [42001-232]
Error Code: 42001
at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:855)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.getAbstractSession(EntityManagerFactoryDelegate.java:222)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.getMetamodel(EntityManagerFactoryDelegate.java:679)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.getMetamodel(EntityManagerFactoryImpl.java:565)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.invokeProxyMethod(AbstractEntityManagerFactoryBean.java:519)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean$ManagedEntityManagerFactoryInvocationHandler.invoke(AbstractEntityManagerFactoryBean.java:734)
at com.sun.proxy.$Proxy32.getMetamodel(Unknown Source)
at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:195)
at java.base/java.util.Iterator.forEachRemaining(Iterator.java:133)
at java.base/java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801)
at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484)
at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474)
at java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:913)
at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.base/java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:578)
at org.springframework.data.jpa.repository.config.JpaMetamodelMappingContextFactoryBean.getMetamodels(JpaMetamodelMappingContextFactoryBean.java:106)
at org.springframework.data.jpa.repository.config.JpaMetamodelMappingContextFactoryBean.createInstance(JpaMetamodelMappingContextFactoryBean.java:80)
at org.springframework.data.jpa.repository.config.JpaMetamodelMappingContextFactoryBean.createInstance(JpaMetamodelMappingContextFactoryBean.java:44)
at org.springframework.beans.factory.config.AbstractFactoryBean.afterPropertiesSet(AbstractFactoryBean.java:142)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1863)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1800)
... 67 common frames omitted
Caused by: org.eclipse.persistence.exceptions.DatabaseException:
Internal Exception: org.h2.jdbc.JdbcSQLSyntaxErrorException: Syntax error in SQL statement "CREATE TABLE IF NOT EXISTS EXERCICE\000d\000a(\000d\000a EXERCICE_ID NUMBER [*]AUTO_INCREMENT,\000d\000a TYPE_SITE_ID NUMBER,\000d\000a EXERCICE_META_ID NUMBER,\000d\000a DESC_COURT VARCHAR(512),\000d\000a DESC_LONG VARCHAR(2048),\000d\000a DT_HORS_DELAI DATE,\000d\000a DT_DEBUT DATE,\000d\000a DT_FIN DATE,\000d\000a DT_DEB_INSCRIPTION DATE,\000d\000a DT_FIN_INSCRIPTION DATE,\000d\000a DT_DEB_REINSCRIPTION DATE,\000d\000a DT_FIN_REINSCRIPTION DATE,\000d\000a DT_ANNULATION DATE,\000d\000a COMMENTAIRE VARCHAR(2048),\000d\000a KL_ID NUMBER,\000d\000a CODE VARCHAR(64),\000d\000a ORGANISATION_ID NUMBER\000d\000a)"; expected "(, ARRAY, INVISIBLE, VISIBLE, NOT NULL, DEFAULT, GENERATED, ON UPDATE, NOT NULL, DEFAULT ON NULL, SEQUENCE, SELECTIVITY, COMMENT, CONSTRAINT, COMMENT, PRIMARY KEY, UNIQUE, NOT NULL, CHECK, REFERENCES, ,, )"; SQL statement:
CREATE TABLE IF NOT EXISTS EXERCICE
(
EXERCICE_ID NUMBER AUTO_INCREMENT,
TYPE_SITE_ID NUMBER,
EXERCICE_META_ID NUMBER,
DESC_COURT VARCHAR(512),
DESC_LONG VARCHAR(2048),
DT_HORS_DELAI DATE,
DT_DEBUT DATE,
DT_FIN DATE,
DT_DEB_INSCRIPTION DATE,
DT_FIN_INSCRIPTION DATE,
DT_DEB_REINSCRIPTION DATE,
DT_FIN_REINSCRIPTION DATE,
DT_ANNULATION DATE,
COMMENTAIRE VARCHAR(2048),
KL_ID NUMBER,
CODE VARCHAR(64),
ORGANISATION_ID NUMBER
) [42001-232]
Error Code: 42001
at
在H2文档中,它说
AUTO_INCREMENT 子句不会自动创建主键约束
因此我建议更改以下行:
EXERCICE_ID NUMBER AUTO_INCREMENT,
成为:
EXERCICE_ID PRIMARY KEY AUTO_INCREMENT,