0

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.

Hung Bang Quan
  • 151
  • 2
  • 9
  • Solved! I found similar article here https://stackoverflow.com/a/42237105/8084747 – Hung Bang Quan Jul 22 '21 at 01:56
  • By the way, you guys can read the official docs to get more detail about these strategies https://docs.jboss.org/hibernate/orm/5.2/userguide/html_single/Hibernate_User_Guide.html#identifiers-generators-auto – Hung Bang Quan Jul 22 '21 at 01:58

1 Answers1

0

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;