简体   繁体   中英

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:

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 boot + hibernate at runtime: 12749 Running native query in 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.

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM