首页 > 其他 > 详细

020_ID生成策略_XML_配置

时间:2016-01-04 02:05:30      阅读:299      评论:0      收藏:0      [点我收藏+]

星期六, 一月 02, 2016 ?14:04:55

?

?

id生成策略

1.对应项目:hibernate_0400_ID

2.注意:

? ? a)我们观察hibernate生成表的结构并不是为了将来就用它生成,(可能还有自己的扩展,比如index等)

而是为了明白我们应该建立什么样的表和实体类映射。

?

?

3.

?

?

?

id主键:

1)在mysql用自增字段,用auto increatment

? ?在oracle ?用 sequence

注意:

? ? ? ?对于类里面的对象里的这个值就不可以指定它了。得靠程序(数据库)帮我自动生成;

?

hibernate或JPA已实现这样的能力,就是通过设置-->告诉它id怎么生成,这样的话,你写程序的时候就不用设这个id了。

?

----id的生成策略。

?

?

??

? ??

测试类:

使用junit进行

约定俗成的 ?在类的后面加Test是测试类HibernateIDTest

在方法的前面加Test是测试方法

?

?

?

案例:

1.查看文档自动生成id的

看文档的习惯是,先找目录,找不到再进行搜索。

?

对象/关系数据库映射基础(Basic O/R Mapping)有一个id

? <generator class="generatorClass"/>?

可的<generator>子元素是一个Java类的名字, 用来为该持久化类的实例生成唯一的标识。

?

uuid ?university ?Unicode id ?全球唯一的id-----type string

native ?会根据数据库为oracle或是mysql进行使用sequence 或是auto_increment

?

?

设置了generator,在测试类中就不需要再进行设置了。

?

对于xml配置文件进行生成uuid

?

生成的sql

id varchar(255) not nul

?

?

代码案例:

/hibernate_0400_ID/src/com/zhuhw/hibernate/model/Student.hbm.xml

?

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- 找不到entity,是因为这个类没改包名 -->
<hibernate-mapping package="com.zhuhw.hibernate.model">
<class name="Student">
<!-- id主键;name=id对应的是Student中的getid() -->
<id name="id"  >
<generator class="uuid"></generator>
</id>
<property name="name"/>
<property name="age" />
<!-- hibernater知道了怎么将class与表中的字段对应到一起了 -->
</class>
</hibernate-mapping>

?

?

?

?

/hibernate_0400_ID/src/com/zhuhw/hibernate/model/Student.java

?

package com.zhuhw.hibernate.model;
 
public class Student {
/*private int id;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}*/
private String id;
 
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 int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
private String name;
private int age;
}

?

?

?

/hibernate_0400_ID/src/hibernate.cfg.xml

?

<?xml version=‘1.0‘ encoding=‘utf-8‘?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
 
<hibernate-configuration>
 
    <session-factory>
 
        <!-- Database connection settings -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost/hibernate</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>
        <property name="format_sql">true</property>
 
        <!-- Drop and re-create the database schema on startup -->
        <property name="hbm2ddl.auto">update</property>
      
        <mapping resource="com/zhuhw/hibernate/model/Student.hbm.xml"/>
 
   <mapping class="com.zhuhw.hibernate.model.Teacher"/>  
    </session-factory>
 
</hibernate-configuration>

?

?

?

/hibernate_0400_ID/test/com/zhuhw/hibernate/model/HibernateIDTest.java

?

package com.zhuhw.hibernate.model;
 
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
 
public class HibernateIDTest {
 
public static SessionFactory sf = null;
@BeforeClass
public  static void beforeClass(){
sf = new AnnotationConfiguration().configure().buildSessionFactory();
}
 
@Test
public void TestID(){
Student s = new Student();
/*配置文件中使用generator
* s.setId(9);
* */
s.setName("yuzhou1");
s.setAge(1);
 
 
Session session = sf.openSession();
 
session.beginTransaction();
session.save(s);
session.getTransaction().commit();
session.close();
}
 
@AfterClass
public static void afterClass(){
sf.close();
}
}
 

?

?

运行结果:

id varchar(255)?

将id生成的是String进行存储的。

?

先将student表drop掉

?

使用native

配置文件

<id name="id" ?>

<generator class="native"></generator>

</id>

?

在java 类中

将主键设置为int类型即可。

?

运行结果:

create table Student (

? id integer not null auto_increment, ?

? varchar(255), age integer, primary key (id))

020_ID生成策略_XML_配置

原文:http://yuzhouxiner.iteye.com/blog/2268315

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!