之前的Hibernate使用int作为主键,其主键生成策略(在映射文件中定义)为native,这是一种根据当前使用的数据库自动选择主键策略的方法.现在使用uuid作为主键生成策略.
若使用uuid作为主键的生成策略,前提是主键要定义为String型,且映射文件中写:
下面是持久类和映射文件:
持久类:
映射文件:
主要就是这两处改动.
之后查看生成的表格,
[img]http://dl2.iteye.com/upload/attachment/0128/7061/2532c6e4-54c7-3601-aeda-a32b519d8fd0.png[/img]
若使用uuid作为主键的生成策略,前提是主键要定义为String型,且映射文件中写:
<generator class = "uuid"></generator>
下面是持久类和映射文件:
持久类:
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;
}
}
映射文件:
<?xml version="1.0" encoding="UTF-8"?>
<!--引入dtd约束-->
<!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"><!--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>
主要就是这两处改动.
之后查看生成的表格,
[img]http://dl2.iteye.com/upload/attachment/0128/7061/2532c6e4-54c7-3601-aeda-a32b519d8fd0.png[/img]