【发布时间】:2019-08-19 09:52:45
【问题描述】:
我已经使用 Hibernate、PostgreSQL 和 Spring 设置了一个测试应用程序,但我一直遇到这个莫名其妙的错误。我希望这里有人能对这个问题有所了解。
我的实体如下所示:
@Entity
@Table(name = "something")
public class Something {
@Id
@SequenceGenerator(name = "somethingGen", sequenceName = "something_id_seq", allocationSize = 30)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "somethingGen")
@Column(name = "id", unique = true, nullable = false)
private long id;
public Something() {
}
public long getId() {
return id;
}
@Override
public String toString() {
return new StringJoiner(", ", Something.class.getSimpleName() + "[", "]")
.add("id=" + id)
.toString();
}
}
出于测试目的,我创建了一个 Spring Data Repository,插入了五个条目并将它们取回:
System.out.println("starting...");
for (int i = 0; i < 5; i++) {
repository.save(new Something());
}
repository.findAll().forEach(System.out::println);
我第一次启动这段代码时,它几乎按预期工作:
Something[id=1]
Something[id=2]
Something[id=3]
Something[id=4]
Something[id=5]
但是我第二次遇到了这个异常:org.hibernate.MappingException: The increment size of the [something_id_seq] sequence is set to [30] in the entity mapping while the associated database sequence increment size is [1].
所以我检查了 Postgres DB 中的序列:
uat=# \d something_id_seq
Sequence "public.something_id_seq"
Column | Type | Value
---------------+---------+---------------------
sequence_name | name | something_id_seq
last_value | bigint | 1
start_value | bigint | 1
increment_by | bigint | 1
max_value | bigint | 9223372036854775807
min_value | bigint | 1
cache_value | bigint | 1
log_cnt | bigint | 32
is_cycled | boolean | f
is_called | boolean | t
这是第一个问题。似乎 Hibernate 正在创建具有错误增量值的序列。所以我更改了 increment_by 的值:alter sequence something_id_seq increment 30 并再次启动了我的代码,最终得到了这个输出:
Something[id=1]
Something[id=2]
Something[id=3]
Something[id=4]
Something[id=5]
Something[id=901]
Something[id=902]
Something[id=903]
Something[id=904]
Something[id=905]
现在序列看起来像这样:
---------------+---------+---------------------
sequence_name | name | something_id_seq
last_value | bigint | 31
start_value | bigint | 1
increment_by | bigint | 30
所以在我看来,新 id 是通过 increment_by * allocation_size + last_value 计算的,但我希望新 id 为 30 (allocation_size * last_value),因为我怀疑 increment_by 和 allocationSize 是同一件事因为 MappingException。
这给我留下了两个问题:
我预计 id 大约是 0、30、60、90...,但我最终得到的 id 大约是 1 和 900。SequenceGenerator 的 javadoc 指出 allocationSize 是
the amount to increment by when allocating sequence numbers from the sequence。这是否意味着increment_by和allocationSize不是一回事?它们是如何联系在一起的(例如,为什么我会看到MappingException)以及如何实现预期的行为?MappingException 向我表明 Hibernate 正在创建具有错误增量值的序列。如何让 Hibernate 创建正确的?
我正在使用 Hibernate 5.4.2.Final 和 PostgreSql 9.6.12 以及这些设置:
HibernateJpaVendorAdapter hibernateJpaVendorAdapter = new HibernateJpaVendorAdapter();
hibernateJpaVendorAdapter.setDatabase(Database.POSTGRESQL);
hibernateJpaVendorAdapter.setDatabasePlatform(PostgreSQL9Dialect.class.getName());
hibernateJpaVendorAdapter.setGenerateDdl(true);
我怀疑问题可能出在 HiLo 生成器的某个地方,但对于我来说,我无法弄清楚:
hibernate.id.new_generator_mappings=true
hibernate.id.optimizer.pooled.preferred=hilo
hibernate.schema_update.unique_constraint_strategy=RECREATE_QUIETLY
【问题讨论】:
标签: java postgresql hibernate orm hibernate-mapping