Use uuid as primary key generation strategy

The previous Hibernate used int as the primary key, and its primary key generation strategy (defined in the mapping file) was native, which was a method of automatically selecting the primary key strategy based on the database currently in use. Now it uses uuid as the primary key generation strategy.

If uuid is used as the primary key generation strategy The generation strategy of the primary key, the premise is that the primary key should be defined as String type, and the mapping file should write:
<generator class = "uuid"></generator>


Here are the persistent classes and mapping files:

persistent classes:
package hiber1;

public class User {
	private String uid;
	private String username;
	private String password;
	private String address;
	public String getUid() {
		return uid;
	}
	public void setUid(String uid) {
		this.uid = uid;
	}
	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 getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	
	

	

}



Mapping file:
<?xml version="1.0" encoding="UTF-8"?>

<!--Introduce dtd constraints-->
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
    
<hibernate-mapping>
    <class name = "hiber1.User" table = "newtableTest2"><!--The full path of the class written in class-->
        <id name = "uid" column = "uid">
            <generator class = "uuid"></generator>
        </id>
        
        <property name="username" column = "username"></property>
        <property name="password" column = "password"></property>
        <property name="address" column = "address"></property>
    </class>
</hibernate-mapping>



Mainly these two changes.
After viewing the generated table,

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326036369&siteId=291194637