0

I use spring boot/jpa/hibernate/MYSQL to generate schema and have table orders. I want generate 2 sequence in one table. First generated second ignored. How I could achieve that?

@Entity
@Data
@Table(name = "orders")
public class Orders {
    @Id
    @GeneratedValue(strategy= GenerationType.AUTO)
    private Long id;

    @Column(name = "order_number")
    @GeneratedValue(strategy= GenerationType.AUTO)
    private Long orderNumber;
}
Togrul Sadigov
  • 231
  • 4
  • 10
  • Does this answer your question? [Multiple Hibernate sequence generators for one entity with PostgreSQL](https://stackoverflow.com/questions/34528450/multiple-hibernate-sequence-generators-for-one-entity-with-postgresql) – Marc Sances Aug 19 '20 at 21:30

1 Answers1

1

The implementation of @GeneratedValue with the AUTO strategy in your actual SQL database will likely be to use an auto increment column. Most databases do not even allow having two auto increment columns, and in general, there should not be a need why you even would need this. I suggest just deleting one of either the id or orderNumber fields, and keeping the other one as the only auto increment column in your entity/table.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • I want generate second sequence "order_number" to use it in other place as order number. When I add Order I could add UUID but it too long(useless). It will be good if it generated like this 000001. – Togrul Sadigov Aug 19 '20 at 17:07
  • @TogrulSadigov Then add a computed column which formats the auto increment value the way you want to see it. – Tim Biegeleisen Aug 19 '20 at 17:10