jpa生成uuid

使用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; // 密码
}
原文地址:https://www.cnblogs.com/q1359720840/p/10760698.html