I have an entity called Booking, which has a generated id field what I use as primary key. It works fine. I want to add another field, uuid which I'll use as resource identifier in a REST API. I have a Postgres DB and set up the field to be auto generated:
uuid character varying(36) not null default uuid_generate_v1mc()
On creation, the DB generates the id and uuid just fine, but in the Java code, the uuid field is null. My entity looks like this:
@Entity
public class Booking {
@Id
@SequenceGenerator(name="booking_id_seq", sequenceName="booking_id_seq", allocationSize=1)
@GeneratedValue(strategy= GenerationType.IDENTITY, generator = "booking_id_seq")
private Long id;
@Column(nullable = false)
private String uuid;
(...)
}
What am I doing wrong?
Follow up: Thank you for everybody for the comments and answers. It looks like I have two choices, either reload the entity from the DB to have the uuid generated by the DB or generate it myself in the code. Which one is preferred? I guess the latter is more performant, but is there anything else to consider?
@Idfield is automatically synchronized with the DB other generated fields aren't. You will have to reload the entity from the db.