【问题标题】:issue with Hibernate allocationSize for SequenceGeneratorSequenceGenerator 的 Hibernate allocationSize 问题
【发布时间】:2019-10-02 04:26:23
【问题描述】:

我使用@SequenceGenerator 阅读了很多内容——它将真实数据库序列加一,将此值乘以 50(默认分配大小值)——然后将此值用作实体 ID。但是发现了一个问题,它没有执行 sequence.nextVal 并且没有更新序列。

使用休眠 - 5.0.12

顺序

CREATE SEQUENCE TEST_ENTITY_ID_SEQ INCREMENT BY 1 MAXVALUE 9999999999999999999999999999 CACHE 20;

这是我的实体 -

@Entity
@Table(name = "TEST_ENTITY")
public class TestEntity  implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @SequenceGenerator(name = "TEST_ENTITY_ID_SEQ", initialValue = 1, allocationSize = 2, sequenceName = "TEST_ENTITY_ID_SEQ")
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "TEST_ENTITY_ID_SEQ")
    @Column(name = "COMNT_ID")
    private Long comntId;
    @Column(name = "VIEW_ID")
    private Long viewId;

我的服务代码 -

TestEntity testEntity = new TestEntity();
testEntity.setViewId(11l);
TestEntity testEntity1 = testEntityRepo.save(testEntity);
return testEntity1.toString();

第一次重新启动我的应用程序我的序列当前 val 为 - 7

Attempt1: 当我保存 TestEntity 时,它生成 ID 为 - TestEntity{comntId=16, viewId=11}
Attempt2: 当我保存 TestEntity 时,它生成 ID 为 - @ 987654329@
Attempt3: 当我保存 TestEntity 时,它生成的 ID 为 - TestEntity{comntId=18, viewId=11}
Attempt4: 当我保存 TestEntity 时,它生成的 ID 为- TestEntity{comntId=19, viewId=11}
序列曲线仍然与 -7
重新启动应用程序后
Attempt5: 当我保存 TestEntity 时,它会生成 ID 为 - TestEntity{comntId=20, viewId=11}
Attempt6: 当我保存 TestEntity 时生成 ID 为 -TestEntity{comntId=21, viewId=11}
sequence currval 仍然与 -7
重新启动应用程序后
Attempt7: 当我保存 TestEntity 时,它会生成 ID 为 - TestEntity{comntId=22, viewId=11}
Attempt8: 当我保存 TestEntity 它时生成 ID 为 -TestEntity{comntId=23, viewId=11}
sequence currval 仍与 -7 相同

请注意,即使我将其更改为 allocationSize = 1

这是预期的行为吗?还是我在这里遗漏了什么?

【问题讨论】:

  • TEST_ENTITY_ID_SEQ 是如何定义的?
  • @SimonMartinelli 添加了序列的定义
  • 为什么当你增加 1 时分配大小为 2?
  • 我尝试将大小更改为 1,但它仍然不会影响我的序列值。
  • 生成的sql语句检查了吗?

标签: oracle hibernate spring-boot jpa spring-data-jpa


【解决方案1】:

问题是我使用select TEST_ENTITY_ID_SEQ.NEXTVAL from dual 从另一个会话中获取序列的当前值。

CURRVAL 是特定于 SESSION 的;也就是说,每个会话可能有一个 与其他会话不同的价值。

所以当我从我的应用程序中查询时,它开始给我正确的值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-09-26
    • 1970-01-01
    • 2011-02-20
    • 1970-01-01
    • 2015-07-23
    • 1970-01-01
    • 1970-01-01
    • 2011-11-19
    相关资源
    最近更新 更多