020_ID生成策略_XML_配置

星期六, 一月 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))

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值