0

I'm writing a JPA entity and want it's id to be generated automatically by Hibernate. I'm using @GenericGenerator annotation with some parameters managing strategy and increment. I want ids to be generated consequently (1, 2, 3, 4, ...), but somehow Hibernate decides that it must be generated by incrementing previous id by 100 (1, 101, 201, ...). BTW I'm using Derby Embedded.

I found that strategy = "increment" fits my aim but as I understand it's type isn't thread safe.

@Table(name = "ACCOUNTS")
data class Account(
        @Id
        @GenericGenerator(name = "ACCOUNT_SEQ",
                strategy = "enhanced-sequence",
                parameters = [
                    Parameter(name = "initial_value", value = "1"),
                    Parameter(name = "increment_size", value = "1")]
        )
        @GeneratedValue(generator = "ACCOUNT_SEQ")
        @Column(name = "ID")
        val id: Long
)

So, how can I use strategy = "enhanced-sequence" and increment id by 1? Is it possible?

  • Have you looked at https://stackoverflow.com/questions/39861098/replace-sequencegenerator-since-its-deprecated – Atul Jan 08 '19 at 22:15
  • For advice on controlling the sequence number preallocation behavior of Derby, see: https://stackoverflow.com/a/31804619/193453 – Bryan Pendleton Jan 09 '19 at 02:17

2 Answers2

1

You need to check the INCREMENT BY value of your database sequence in your database. Hibernate only request the next sequence value from the database. The database then increments it by the configure step size (in your case 100) and returns the new value.

Before I explain Hibernate's increment_size parameter, please take a look at the @GeneratedValue(strategy = GenerationType.SEQUENCE) which I explain in this article and this video. It's a much easier way to tell Hibernate to use a database sequence to generate primary keys.

The increment_size that you configure for your @GenericGenerator is only used internally to reduce the number of database roundtrips. It needs to be the same as configured for your database sequence. The configured increment_size tells Hibernate how often it can increase a retrieved sequence value internally before it has to fetch another value from the sequence. Here is a quick example of what happens if you set the increment_size to 5:

    1. Entity: Hibernate retrieves a new sequence value from the database. The database returns 1 and increments its internal value by 5. --- Value used by Hibernate: 1 Value in database: 6
    1. Entity: Hibernate increments the id value internally --- Value used by Hibernate: 2 Value in database: 6
    1. Entity: Hibernate increments the id value internally --- Value used by Hibernate: 3 Value in database: 6
    1. Entity: Hibernate increments the id value internally --- Value used by Hibernate: 34Value in database: 6
    1. Entity: Hibernate increments the id value internally --- Value used by Hibernate: 5 Value in database: 6
    1. Entity: Hibernate retrieves the next sequence value from the database. The database returns 6 and increments its internal value by 5. --- Value used by Hibernate: 6 Value in database: 11
Thorben Janssen
  • 3,012
  • 9
  • 23
0

Thanks to this answer https://stackoverflow.com/a/31804619/9161059. Autoincrement adds 100 by default if Derby db was terminated. Here is an explanation of preallocator: https://db.apache.org/derby/docs/10.9/ref/rrefproperpreallocator.html