1

I'm creating a Queue object.

public class Queue {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    @Column(name = "queue_number", unique = true, nullable = false, insertable = false, updatable = false)
    private Long queueNumber;
...
}

It has the id field as it's primary key. But as for queueNumber, I want it to auto increment like the id field. The code above just doesn't work. Is there any way to do this?

Rigo Sarmiento
  • 434
  • 5
  • 21
  • Do you want MySQL to generate the values? MySQL will not support two auto-increment columns in the same table. No way to do it, even with triggers. If you can write Java code to allocate new unique values you could insert them, but then it's not auto. – Bill Karwin Nov 25 '20 at 00:35
  • Possible duplicate of https://stackoverflow.com/q/38324201/20860 but that's about PHP & Laravel, not Java & Spring. – Bill Karwin Nov 25 '20 at 00:37
  • `MySQL will not support two auto-increment columns in the same table.` welp this is pretty much the answer I'm looking for. bummer. thanks! – Rigo Sarmiento Nov 25 '20 at 00:57
  • [MariaDB-10.3+ has Sequences](https://mariadb.com/kb/en/sequences/) that can be used like auto_increment values that don't have a limit on the number of them in a table. `CREATE SEQUENCE seq_t_i INCREMENT 1 START WITH 1` and the sequence can be used as the default value in a table `CREATE TABLE t( i integer DEFAULT nextval(seq_t_i), j integer );` – danblack Nov 25 '20 at 07:18

0 Answers0