能够生成 UUID 但无法将其插入数据库

Able to generate UUID but was not able to insert the same into database

我尝试使用 Hibernate 在 MySQL 中生成 GUID/UUID。下面是相同的代码:任何人都可以告诉我哪里出错了。

     public class MainClassLists {
        public static void main(String[] args){

        Configuration configuration = new Configuration().configure();
        ServiceRegistryBuilder registry = new ServiceRegistryBuilder();
        registry.applySettings(configuration.getProperties());
        ServiceRegistry serviceRegistry = registry.build();
        SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);

        Session session = sessionFactory.openSession();
        Users userNew = new Users();
                    userNew.setUserName("StyuytSra");
        userNew.setPassword("pyuass");
        userNew.setFirstName("sryuyaxcva");
        userNew.setLastName("sayuynxca");
        userNew.setBirthDate("2000-08-12");
        userNew.setGender("f");
        userNew.setEmailId("tesuyut@email.com");           
        userNew.setPhoneNumber(92672);
        userNew.setAccountNumber(8878);
        userNew.setAddress1("add1");
        userNew.setAddress2("add2");
        userNew.setCity("SD");
        userNew.setCountry("US");
        userNew.setZipcode(765);
        session.beginTransaction();
        session.save(userNew);
       Serializable uid= session.getIdentifier(userNew);

        System.out.println("User ID : "+userNew.getUserId()+""+uid);
               System.out.println();
        session.close();
        System.exit(0);
    } 
  }

实体Class:

  @Entity
  @Table(name = "users", catalog = "test", schema = "")
 public class Users implements Serializable {
   @Id
@Column(name = "user_id")

@GeneratedValue(generator = "GUID")
@GenericGenerator(name = "GUID", strategy =  "com.List_Test.mainClass.InquisitiveUUIDGenerator")

private String userId;
@Basic(optional = false)
@Column(name = "user_name", unique = true)
private String userName;
@Basic(optional = false)
@Column(name = "password")
private String password;
@Basic(optional = false)
@Column(name = "first_name")
private String firstName;
@Basic(optional = false)
@Column(name = "last_name")
private String lastName;
@Column(name = "birth_date")
private String birthDate;
@Column(name = "gender")
private String gender;
@Column(name = "email_id", unique = true)
private String emailId;
@Column(name = "phone_number")
private Integer phoneNumber;
@Column(name = "account_number", unique = true)
private Integer accountNumber;
@Column(name = "address_1")
private String address1;
@Column(name = "address_2")
private String address2;
@Column(name = "city")
private String city;
@Column(name = "state")
private String state;
@Column(name = "zipcode")
private Integer zipcode;
@Column(name = "country")
private String country;
 public String getUserId() {
    return userId;
}

public void setUserId(String userId) {
    this.userId = userId;
}

public String getUserName() {
    return userName;
}

public void setUserName(String userName) {
    this.userName = userName;
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}

public String getFirstName() {
    return firstName;
}

public void setFirstName(String firstName) {
    this.firstName = firstName;
}

public String getLastName() {
    return lastName;
}

public void setLastName(String lastName) {
    this.lastName = lastName;
}

public String getBirthDate() {
    return birthDate;
}

public void setBirthDate(String birthDate) {
    this.birthDate = birthDate;
}

public String getGender() {
    return gender;
}

public void setGender(String gender) {
    this.gender = gender;
}

public String getEmailId() {
    return emailId;
}

public void setEmailId(String emailId) {
    this.emailId = emailId;
}

public Integer getPhoneNumber() {
    return phoneNumber;
}

public void setPhoneNumber(Integer phoneNumber) {
    this.phoneNumber = phoneNumber;
}

public Integer getAccountNumber() {
    return accountNumber;
}

public void setAccountNumber(Integer accountNumber) {
    this.accountNumber = accountNumber;
}

public String getAddress1() {
    return address1;
}

public void setAddress1(String address1) {
    this.address1 = address1;
}

public String getAddress2() {
    return address2;
}

public void setAddress2(String address2) {
    this.address2 = address2;
}

public String getCity() {
    return city;
}

public void setCity(String city) {
    this.city = city;
}

public String getState() {
    return state;
}

public void setState(String state) {
    this.state = state;
}

public Integer getZipcode() {
    return zipcode;
}

public void setZipcode(Integer zipcode) {
    this.zipcode = zipcode;
}

public String getCountry() {
    return country;
}

public void setCountry(String country) {
    this.country = country;
}
 }

ID 生成器 class:

 public class InquisitiveUUIDGenerator extends UUIDGenerator {

private String entityName;

public void configure(Type type, Properties params, Dialect dialect) {
    entityName = params.getProperty(ENTITY_NAME);
    super.configure((org.hibernate.type.Type) type, params, dialect);
}

@Override
public Serializable generate(SessionImplementor session, Object object) {
    Serializable id = session
            .getEntityPersister(entityName, object)
            .getIdentifier(object, session);

    if (id == null) {
        return super.generate(session, object);
    } else {
        return id;
    }
}
}

此外,我尝试在为用户 table 插入 MySQL 数据库之前使用触发器插入数据,并尝试将其映射到实体 class。我映射了它,但是我为生成器提供密钥的 @NaturalId(unique) 密钥不是唯一的。因此会生成重复的映射。 我对生成 GUID 或 UUID 的正确方法是什么感到困惑。感谢,如果有人能给我一个例子。下面是 table

的 id DB 脚本
 CREATE TABLE `users` (
 `user_id` varchar(36) NOT NULL,
 `user_name` varchar(20) NOT NULL,
 `password` varchar(25) NOT NULL,
 `first_name` varchar(25) NOT NULL,
`last_name` varchar(25) NOT NULL,
 `birth_date` varchar(100) DEFAULT NULL,
 `gender` varchar(15) DEFAULT NULL,
`email_id` varchar(50) DEFAULT NULL,
`phone_number` int(10) DEFAULT NULL,
`account_number` int(8) DEFAULT NULL,
`address_1` varchar(50) DEFAULT NULL,
`address_2` varchar(50) DEFAULT NULL,
`city` varchar(15) DEFAULT NULL,
`state` varchar(2) DEFAULT NULL,
`zipcode` int(8) DEFAULT NULL,
`country` varchar(15) DEFAULT NULL,
PRIMARY KEY (`user_id`),
 UNIQUE KEY `user_name` (`user_name`),
UNIQUE KEY `email_id` (`email_id`),
 UNIQUE KEY `phone_number` (`phone_number`),
 UNIQUE KEY `account_number` (`account_number`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

提供的代码编译成功,运行。 id 已生成但未插入数据库 table。 输出将是这样的: 'User ID : ab9ffdb1-67ba-4d14-9038-3901d5032ab5ab9ffdb1-67ba-4d14-9038-3901d5032ab5'

我没有使用单独的 class 来生成 UUID(如我的问题中所述),而是使用了在其他堆栈溢出答案中找到的以下注释。我之前试过但没有用,因为我忘记开始和结束并提交会话事务。尽管它正在生成 GUID,但它并未保存到数据库中。我必须提交交易才能保存它。下面的代码应该为 ID 注释。

  @Id
@GeneratedValue(generator = "system-uuid")
@GenericGenerator(name = "system-uuid", strategy = "org.hibernate.id.GUIDGenerator")
@Column(name = "user_id", unique = true)