简体   繁体   English

Spring Hibernate @GeneratedValue & @SequenceGenerator 在本地查询中没有得到正确的序列

[英]Spring Hibernate @GeneratedValue & @SequenceGenerator didn't get correct sequence as querying by the native query

Below is the entity configuration:下面是实体配置:

@Entity
@SequenceGenerator(
    name = "sessionInfoIdSeq",
    sequenceName = "SESSIONINFO_ID_SEQ"
)
public class SessionInfo implements Transformable {
    @Id
    @GeneratedValue(
        strategy = GenerationType.AUTO,
        generator = "sessionInfoIdSeq"
    )
    private Long id;

This means when inserting data into the database, the id will be fetched from the SESSIONINFO_ID_SEQ:这意味着在向数据库中插入数据时,将从 SESSIONINFO_ID_SEQ 中获取 id:

select nextval ('SESSIONINFO_ID_SEQ')

But the problem is, the next sequence number get by Spring boot app + Hibernate is not being the same as when we run the native query in DataGrip or DBeaver, although I've seen the app used the same query that used in Datagrip.但问题是,Spring 启动应用程序 + Hibernate 获得的下一个序列号与我们在 DataGrip 或 DBeaver 中运行本机查询时不同,尽管我已经看到该应用程序使用了与 Datagrip 中使用的相同查询。

Spring boot + hibernate at runtime: 12749 Running native query in DataGrip: 12797 Spring boot + 运行时休眠:12749 在 DataGrip 中运行本机查询:12797

I'm not sure why this is appearing.我不确定为什么会出现这种情况。 But the question is how can I sync the sequence number, to when the app takes a new one, we can see the same on Datagrip or DBeaver.但问题是我如何同步序列号,当应用程序采用新的序列号时,我们可以在 Datagrip 或 DBeaver 上看到相同的内容。

Let me know if the question is existing or not correct.让我知道问题是否存在或不正确。 Thank you in advance for all your support.在此先感谢您的支持。

Use can use attribute level annotations as follows:使用可以使用属性级别的注解如下:

@Id
@GeneratedValue(generator = "sequence-generator")
@GenericGenerator(
  name = "sequence-generator",
  strategy = "org.hibernate.id.enhanced.SequenceStyleGenerator",
  parameters = {
    @Parameter(name = "sequence_name", value = "SESSIONINFO_ID_SEQ"),
    @Parameter(name = "initial_value", value = "4"),
    @Parameter(name = "increment_size", value = "1")
    }
)
private long id;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 注释快捷方式(Hibernate 的@Id、@GeneratedValue、@SequenceGenerator) - Annotation shortcut (Hibernate's @Id, @GeneratedValue, @SequenceGenerator) hibernate @GeneratedValue对不对? - hibernate @GeneratedValue correct? 有什么办法从Spring上下文中获取Hibernate SequenceGenerator? - Any way to get a Hibernate SequenceGenerator from spring context? 在多线程中休眠@GeneratedValue序列 - Hibernate @GeneratedValue Sequence in multi threading Spring 3应用程序中的Hibernate @SequenceGenerator问题 - Issue with Hibernate @SequenceGenerator in Spring 3 application 带有 SequenceGenerator 和 GeneratedValue 的 JPA - JPA with SequenceGenerator and GeneratedValue 如何在spring-jpa中使用@GeneratedValue获取序列中的下一个值 - How to get the next value in a sequence with @GeneratedValue in spring-jpa 使用Spring Data Jpa和Hibernate时,JSR303验证不验证@Id是否不是@GeneratedValue - JSR303 validation doesn't validate if @Id is not @GeneratedValue with Spring Data Jpa and Hibernate JPA 2 @SequenceGenerator @GeneratedValue 产生唯一约束违规 - JPA 2 @SequenceGenerator @GeneratedValue producing unique constraint violation MappedSuperclass - 更改子类中的 SequenceGenerator 而不在超类上使用 @GeneratedValue - MappedSuperclass - Change SequenceGenerator in Subclass without @GeneratedValue on the Superclass
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM