我有以下数据库序列
CREATE SEQUENCE phonebook_id_seq INCREMENT BY 5;
select nextval('phonebook_id_seq'); // 1,6,11,....
爪哇
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "phoneSequenceGenerator")
@SequenceGenerator(name = "phoneSequenceGenerator", sequenceName = "phonebook_id_seq")
private Long id;
然而,对于Hibernate,我遇到了以下错误,即hibernate期望增加50。 为什么会这样?
Caused by: javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.tool.schema.spi.SchemaManagementException: Schema-validation: sequence [phonebook_id_seq] defined inconsistent increment-size; found [5] but expecting [50]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:396)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:371)
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:336)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1692)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1630)
... 41 common frames omitted
Caused by: org.hibernate.tool.schema.spi.SchemaManagementException: Schema-validation: sequence [phonebook_id_seq] defined inconsistent increment-size; found [5] but expecting [50]
at org.hibernate.tool.schema.internal.AbstractSchemaValidator.validateSequence(AbstractSchemaValidator.java:191)
at org.hibernate.tool.schema.internal.AbstractSchemaValidator.performValidation(AbstractSchemaValidator.java:100)
at org.hibernate.tool.schema.internal.AbstractSchemaValidator.doValidation(AbstractSchemaValidator.java:68)
at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.performDatabaseAction(SchemaManagementToolCoordinator.java:191)
at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.process(SchemaManagementToolCoordinator.java:72)
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:313)
at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:452)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:889)
at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:60)
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:360)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:384)
... 45 common frames omitted
EDIT1 添加了java代码。
答案 0 :(得分:2)
错误信息非常清楚。您已启用(或未禁用)架构验证,因此当应用程序启动时,Hibernate会将数据库与其对注释和/或配置的期望进行比较。 phonebook_id_seq
序列在数据库中以增量5定义,但注释表示50(或者什么也没说,默认值为50)。更改注释以匹配数据库或更改序列或禁用验证。
我猜测基于标记使用spring.jpa.hibernate.ddl-auto=validate
启用了验证,但是没有看到您的代码就很难确定。
答案 1 :(得分:0)
通常,您必须匹配数据库中的序列增量大小和 allocationSize
中的属性 @SequenceGenerator
。
在您的示例中,它将是:
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "phoneSequenceGenerator")
@SequenceGenerator(name = "phoneSequenceGenerator",
sequenceName = "phonebook_id_seq",
allocationSize = 5)
private Long id;
Hibernate 将使用 org.hibernate.dialect.Dialect#getQuerySequencesString
定义的查询检查数据库中的序列定义。
例如对于 PostgreSQL,它将是
SELECT * FROM information_schema.sequences;
从 Hibernate 5.4 开始,我们还可以配置检测到不匹配时的行为。
默认情况下,它会抛出一个 org.hibernate.MappingException
。
这个新属性:hibernate.id.sequence.increment_size_mismatch_strategy (doc)(由 org.hibernate.cfg.AvailableSettings#SEQUENCE_INCREMENT_SIZE_MISMATCH_STRATEGY
映射)可以改变这种行为:
hibernate.id.sequence.increment_size_mismatch_strategy(例如 LOG、FIX 或 EXCEPTION(默认值))
此设置定义了 org.hibernate.id.SequenceMismatchStrategy,当 Hibernate 检测到实体映射中的序列配置与其对应的数据库序列对象之间不匹配时使用。
默认值由 org.hibernate.id.SequenceMismatchStrategy#EXCEPTION 给出,意味着检测到此类冲突时会抛出异常。
因此,如果您将其设置为“FIX”,Hibernate 会自动将 allocationSize 配置为 5。 还会记录警告,如下所示:
<块引用>WARN ...enhanced.SequenceStyleGenerator (SequenceStyleGenerator.java:270) - HHH000497:实体映射中[SEQ_NAME]序列的增量大小设置为[5],而关联的数据库序列增量大小为[5] .数据库序列增量大小将优先,以避免标识符分配冲突。
是的,在'FIX'值的情况下,日志消息似乎是错误的,它应该为第一个值打印50...
答案 2 :(得分:-1)
strategy = GenerationType.SEQUENCE 的默认增量值为50。您在SQLScript /数据库内部定义了增量逻辑,因此休眠模式验证器与增量值不匹配并给出错误。
解决方案: 删除
spring.jpa.hibernate.ddl-auto=validate
来自application.properties的配置。