hibernate 用注解方式生成uuid方法



//配置uuid,本來jpa是不支持uuid的,但借用hibernate的方法可以實現。

@GeneratedValue(generator = "uuid")

@GeneratedValue(generator = "uuid") @GenericGenerator(name = "uuid", strategy = "uuid")

加在id的get方法上面

下面具體操作下:

 

1.同樣的 先新建一個java project。

2.導入hibernate插件(選中項目單擊鼠標右鍵-->my eclipse-->project  facets-->hibernate-->next-->新建一個包選中-->next-->去掉上面那個勾-->finsish)。

3.可以發現在src目錄下有了一個包 還有一個類。

4.新建一個Teacher類  代碼如下:

package com.cqvie;

import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import org.hibernate.annotations.GenericGenerator;

@javax.persistence.Entity
@Table
public class Teacher {
private String id;
private String name;
private String title;
//設置主鍵
@Id
//配置uuid,本來jpa是不支持uuid的,但借用hibernate的方法可以實現。 @GeneratedValue(generator
= "uuid") @GenericGenerator(name = "uuid", strategy = "uuid") public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } }

5.配置hibernate.cfg.xml文件:

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>

     <session-factory>

        <!-- Database connection settings 用到的驅動、數據庫名、用戶名、密碼 -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost/text</property>
        <property name="connection.username">root</property>
        <property name="connection.password">root</property>

        <!-- JDBC connection pool (use the built-in) -->
       <!--  <property name="connection.pool_size">1</property>-->

        <!-- SQL dialect  數據庫方言-->
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

        <!-- Enable Hibernate's automatic session context management -->
        <property name="current_session_context_class">thread</property>

        <!-- Disable the second-level cache  -->
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

        <!-- Echo all executed SQL to stdout-->
        <property name="show_sql">true</property>
       

        <!-- Drop and re-create the database schema on startup -->
        <property name="hbm2ddl.auto">update</property>

        <!-- 將有映射的類告訴配置文件 -->
        <mapping class="com.cqvie.Teacher"/>
    </session-factory>
</hibernate-configuration>

6.將mysql驅動導入項目。

  1.    在項目中新建一個文件夾 
  2. 將驅動放入文件夾
  3. 選中驅動鼠標右鍵 build Path -->add

8.在com.cqvie 包下新建一個測試類TeacherTest

package com.cqvie;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;

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

        Teacher t= new Teacher();
        //uuid已經自動生成,不需要手動添加了
        //t.setId(1);
        t.setName("s1");
        t.setTitle("教授");
        
//        Session session=HibernateSessionFactory.getSession();
        
        
        
        
        
        
        Configuration cfg= new Configuration();
        SessionFactory sf=cfg.configure().buildSessionFactory();
        Session session=sf.openSession();
        session.beginTransaction();
        session.save(t);
        session.getTransaction().commit();
        session.close();    
    }
}

 

9.運行結果如下:

這樣uuid就生成了!

需要注意的是:

1.id不能再用int類型,而是改用string類型,因為uuid很長而且有字母。

2.需要將映射告訴配置文件

3.注解中添加的包一般都是javax的而不是hibernate的


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM