03hibernate_basemapping_uuid_native_assigned

hibernate基本映射:主键生成策略:uuid、native和assigned

实体类---表
实体类中的普通属性---表字段

采用<class>标签映射成数据库表,通过<property>标签将普通属性映射成表字段
所谓普通属性指不包括自定义类、集合和数组等

注意:如果实体类和实体类中的属性和sql中的关键字重复,必须采用table或column重新命名

实体类的设计原则:
    * 实现一个默认的(即无参数的)构造方法(constructor)
    * 提供一个标识属性(identifier property)(可选)
    * 使用非final的类 (可选)
    * 为持久化字段声明访问器(accessors)    

主键生成策略:uuid、native和assigned

a)ExportDB    
create table t_user1 (user_id varchar(32) not null, name varchar(20) not null unique, password varchar(10) not null, create_time datetime, expire_time datetime, primary key (user_id))

create table t_user2 (user_id integer not null auto_increment, name varchar(20) not null unique, password varchar(10) not null, createtime datetime, expiretime datetime, primary key (user_id))

create table t_user3 (user_id varchar(32) not null, name varchar(20) not null unique, password varchar(10) not null, create_time datetime, expire_time datetime, primary key (user_id))

b)BaseMappingTest:testSave
Hibernate: insert into t_user1 (name, password, create_time, expire_time, user_id) values (?, ?, ?, ?, ?)
Hibernate: insert into t_user2 (name, password, createtime, expiretime) values (?, ?, ?, ?)
Hibernate: insert into t_user3 (name, password, create_time, expire_time, user_id) values (?, ?, ?, ?, ?)

c)、uuid、native和assigned分别对应以下三张表:
public class User1 {
    
    private String id;
    
    private String name;
    
    private String password;
    
    private Date createTime;
    
    private Date expireTime;
<hibernate-mapping package="com.bjsxt.hibernate">
    <class name="User1" table="t_user1">
        <id name="id" column="user_id" length="32">
            <generator class="uuid"/>
        </id>
        <property name="name" unique="true" not-null="true" length="20"/>
        <property name="password" not-null="true" length="10"/>
        <property name="createTime" column="create_time"/>
        <property name="expireTime" column="expire_time"/>
    </class>
</hibernate-mapping>
mysql> select *  from t_user1;
+----------------------------------+--------+----------+---------------------+---------------------+
| user_id                          | name   | password | create_time         | expire_time         |
+----------------------------------+--------+----------+---------------------+---------------------+
| 402881e538dd22d20138dd22d4c50001 | 白龙马 | 123      | 2012-07-31 21:04:03 | 2012-07-31 21:04:03 |
+----------------------------------+--------+----------+---------------------+---------------------+
1 row in set (0.00 sec)


public class User2 {
    
    private int id;
    
    private String name;
    
    private String password;
    
    private Date createTime;
    
    private Date expireTime;
    
    <hibernate-mapping package="com.bjsxt.hibernate">
    <class name="User2" table="t_user2">
        <id name="id" column="user_id">
            <generator class="native"/>
        </id>
        <property name="name" unique="true" not-null="true" length="20"/>
        <property name="password" not-null="true" length="10"/>
        <property name="createTime" column="createtime"/>
        <property name="expireTime" column="expiretime"/>
    </class>
</hibernate-mapping>


------------------------------------------------------------------------------------
mysql> select *  from t_user2;
+---------+--------+----------+---------------------+---------------------+
| user_id | name   | password | createtime          | expiretime          |
+---------+--------+----------+---------------------+---------------------+
|       1 | 孙悟空 | 123      | 2012-07-31 21:04:04 | 2012-07-31 21:04:04 |
+---------+--------+----------+---------------------+---------------------+
1 row in set (0.00 sec)

public class User3 {
    
    private String id;
    
    private String name;
    
    private String password;
    
    private Date createTime;
    
    private Date expireTime;
<hibernate-mapping package="com.bjsxt.hibernate">
    <class name="User3" table="t_user3">
        <id name="id" column="user_id" length="32">
            <generator class="assigned"/>
        </id>
        <property name="name" unique="true" not-null="true" length="20"/>
        <property name="password" not-null="true" length="10"/>
        <property name="createTime" column="create_time"/>
        <property name="expireTime" column="expire_time"/>
    </class>
</hibernate-mapping>

------------------------------------------------------------------------------------
mysql> select *  from t_user3;
+---------+--------+----------+---------------------+---------------------+
| user_id | name   | password | create_time         | expire_time         |
+---------+--------+----------+---------------------+---------------------+
| 001     | 猪八戒 | 123      | 2012-07-31 21:04:04 | 2012-07-31 21:04:04 |
+---------+--------+----------+---------------------+---------------------+
1 row in set (0.00 sec)
原文地址:https://www.cnblogs.com/alamps/p/2617402.html