//配置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驱动导入项目。
- 在项目中新建一个文件夹
- 将驱动放入文件夹
- 选中驱动鼠标右键 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的
本文介绍如何在Hibernate中配置UUID作为主键,包括创建项目、配置文件及测试代码。

48

被折叠的 条评论
为什么被折叠?



