简体   繁体   中英

Auto generate number spring boot jpa hibernate annotation gives always null value

I want to have a unique number besides my string id. So I thought I could use Generated from Hibernate but the value is always null.

My entity:

@Entity(name = "user")
public class UserEntity {
    @Id
    @GeneratedValue(generator = "system-uuid")
    @GenericGenerator(name = "system-uuid", strategy = "uuid")
    @Column(updatable = false, nullable = false)
    private String id;

    ....

    @org.hibernate.annotations.Generated(GenerationTime.ALWAYS)
    @Column(name = "identifier_id", insertable = false, updatable = false)
    private Long identifierId;
 }

Could someone please help me?

Thanks to Dmitrii.

I solved this as follows:

@GeneratorType(type = UUIDGenerator.class, when = GenerationTime.INSERT)
@Column(name = "identifier_id")
private String identifierId;


public class UUIDGenerator implements ValueGenerator<String> {
    public String generateValue(Session session, Object owner) {
        return UUID.randomUUID().toString().replace("-", "");
    }
}

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