Auto increment non-key attribute for an entity

This has been asked before, but years ago, here. The links are dead. I searched the documentation for auto-incrementing, and have come up with nothing useful.

I’m adding an additional attribute to a pre-existing entity, and I want to populate the attribute with numerical values that will auto-increment. Essentially a tracking number.

I’d love some help regarding this. Thanks!

Just a bit of advice regarding the direction I should go would be helpful. I plan to do a SQL update for the current entries. I need to find a way to get the code to pick up where the numbers left off, up to an infinite amount of entries.

Thank again.

You may take a look at db sequences and try using it with annotation from javax.persistence

@SequenceGenerator(name = "seq_generator",
        sequenceName = "my_sequence",
        allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE,
        generator = "seq_generator")
@Column(name = "track_no")
protected Integer trackNo;
1 Like

Hi,
I would go this way:

1 Like

Thank you Alex,

This is exactly the type of solution I was hoping for.

While I had some strange issues attempting to update the UniqueNumbersAPI currentNumber value (I set a conditional loop to evaluate if the current number was less than my manually updated value and increment the value if true (it looped to eternity)), I was able to get it working by manually setting the sequence number once. Then I inserted a new value and got rid of my statement setting the current number. I couldn’t figure out why that wasn’t working, but I’m happy that it is now functioning correctly.

Thanks again! You da real MVP!