How to implement a custom String sequence identifier generator with Hibernate

后端 未结 2 1991
渐次进展
渐次进展 2020-12-08 18:03

I\'m using hibernate with spring, h2 and liquibase and I\'m trying to make a custom String id generator for my entities by taking example with this blog post but I\'m gettin

2条回答
  •  萌比男神i
    2020-12-08 18:47

    Yes, hibernate have prebuilt String generators. Just substitute your @GenericGenerator definition to another strategy.

    @Entity
    @Table(name = "contact")
    public class Contact implements Serializable, Identifiable {
    
        private static final long serialVersionUID = 1L;
    
        @Id
        @GeneratedValue(generator = "uuid")
        @GenericGenerator(name = "uuid", strategy = "uuid2")
        private String id;
    
        public String getId() {
            return id;
        }
    
        public void setId(String id) {
            this.id = id;
        }
    }
    

    For more information about different hibernate generators you can look at documentation.

提交回复
热议问题