hibernate 用註解方式生成uuid方法


//配置uuid,原本jpa是不支持uuid的,但借用hibernate的方法能夠實現。java

@GeneratedValue(generator = "uuid")mysql

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

加在id的get方法上面數據庫

下面具體操做下:session

 

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

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

3.能夠發如今src目錄下有了一個包 還有一個類。ide

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文件:ui

<?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的

相關文章
相關標籤/搜索