L
L
lidiya112018-12-26 01:35:59
MySQL
lidiya11, 2018-12-26 01:35:59

How to make autoincrement in MySQL column with Spring annotations?

How to make a column in a mySQL table using Spring annotations so that it auto-increments and it is not an id? That is, I already have an id, and I need one more column, which will be with auto-increment. If this is not possible, how can it be done in another way?
Like this:

//тут все работает:
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", unique = true, nullable = false)
private long id;

@Column(name = "isfree", nullable = false, length = 2)
private byte isfree = 1;

//а тут тоже надо увеличивать значение на один:
@Column(name = "number", length = 15)
private long number;

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Kosarev, 2018-12-27
@lidiya11

Option 1: Within an open transaction, you can request the latest value and manually set the value of the new record.
Option 2: write a stored procedure in MySQL that will calculate the new value of the number property, and pull it when a new record is created.
Option 3: remove number and use id if possible

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question