Search code examples
hibernatespring-mvcannotationsuuidauto-generate

Two auto generated IDs in Spring single entity


I need to generate two IDs (column values) in Spring with Annotations. What would be the solutions if I need 2 IDs auto generated with annotations, ID and UUID, I would use UUID generated with the following:

@Id 
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid2")
@Column(name = "uuid", unique = true)
private String uuid;

but I would not be able to use the @Id annotation at the same time and can not auto generate the ID (second generated field)

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long id;

What would be the best solution to auto generate two columns ID and UUID, since with only one @Id only one is created and the other is null? I need some solution with Hibernate, not programatically generating one of them.


Solution

  • I have temporary decided to use UUID.randomUUID(), but I am still eager to hear about other possible solutions for generating both ids through hibernate.