How to make string primary key hibernate. @GeneratedValue strategies

前端 未结 3 597
走了就别回头了
走了就别回头了 2020-12-10 16:23

My goal is to create an entity Device that has a unique field IMEI and I would like to use it as a primary key, and specify it at device registration time (manually specifie

3条回答
  •  隐瞒了意图╮
    2020-12-10 17:12

    A straightforward solution could be to use the @PrePersist annotation on your entity class.

    Simply add the method

    @PrePersist
    private void ensureId(){
        this.setId(UUID.randomUUID().toString());
    }
    

    and get rid of the @GeneratedValue annotation.

    PrePersist documentation: http://docs.oracle.com/javaee/5/api/javax/persistence/PrePersist.html

    Stefano

提交回复
热议问题