简体   繁体   中英

Spring data JPA generated uuid field is null

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?

you can do a fill with @PrePersist in your entity class

@PrePersist
public void autofill() {
    this.setUuid(UUID.randomUUID().toString());
}

i created an example project for your question https://github.com/zz-chen/Sample-spring-data-jpa-generate-uuid

you can run it with command

gradle bootRun

and you will get some message like this

2017-06-26 11:12:16.118 INFO 78681 --- [ main] hello.Application : Customer found with findOne(1L): 2017-06-26 11:12:16.119 INFO 78681 --- [ main] hello.Application : -------------------------------- 2017-06-26 11:12:16.120 INFO 78681 --- [ main] hello.Application : Customer[id=1, firstName='Jack', lastName='Bauer',uuid='44969325-c31f-4b8e-96d6-a59ff9b845b6'] 2017-06-26 11:12:16.122 INFO 78681 --- [ main] hello.Application : 2017-06-26 11:12:16.122 INFO 78681 --- [ main] hello.Application
: Customer found with findByLastName('Bauer'): 2017-06-26 11:12:16.122 INFO 78681 --- [ main] hello.Application
: -------------------------------------------- 2017-06-26 11:12:16.158 INFO 78681 --- [ main] hello.Application
: Customer[id=1, firstName='Jack', lastName='Bauer',uuid='44969325-c31f-4b8e-96d6-a59ff9b845b6'] 2017-06-26 11:12:16.158 INFO 78681 --- [ main] hello.Application : Customer[id=3, firstName='Kim', lastName='Bauer',uuid='0cef24ad-ce97-4c79-b8a4-69ff575326fb']

with the message we can confirm that the uuid field works perfect !!!

You will only need to add the following on the field in your entity:

import java.util.UUID;

@Column(unique = true, name = "uuid", nullable = false)
private String uuid = UUID.randomUUID().toString().toUpperCase();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM