使用jpa可以生成uuid,但是我直接添加數據沒有id值會報錯,只在程序中有效,如果直接修改數據庫需要手動填寫,另外長度不要亂填 ,之前填了200,找了半天才找到原因。
package com.java1234.entity; import lombok.Data; import org.hibernate.annotations.GenericGenerator; import org.hibernate.validator.constraints.NotEmpty; import javax.persistence.*; @Entity @Table(name = "ip_user") @Data /** * uuid關鍵 */ @GenericGenerator(name = "jpa-uuid", strategy = "uuid") public class User2 { /** * uuid關鍵 */ @Id @GeneratedValue(generator = "jpa-uuid") @Column(length = 32) private String id; @NotEmpty(message="請輸入用戶名!") @Column(length=50) private String userName; // 用戶名 @NotEmpty(message="請輸入密碼!") @Column(length=50) private String password; // 密碼 }