阅读 427

如何使用JPA的UUID主键生成策略

原文链接: www.jianshu.com

这篇文章只写给主键用uuid并且用jpa的小伙伴。

1. 数据实体类

@Entity
@Table(name = "ip_user")
@GenericGenerator(name = "jpa-uuid", strategy = "uuid")
public class User  implements Serializable {
    @Id
    @GeneratedValue(generator = "jpa-uuid")
    @Column(length = 32)
    private String userId;
    ...
}
复制代码

注意@GenericGenerator(name = "jpa-uuid", strategy = "uuid")@GeneratedValue(generator = "jpa-uuid") 两个注解是生成策略核心注解。

2. 数据库字段