id生成策略java
1.對應項目:hibernate_0400_IDmysql
2.注意:sql
a)咱們觀察hibernate生成表的結構並非爲了未來就用它生成,(可能還有本身的擴展,好比index等)數據庫
而是爲了明白咱們應該創建什麼樣的表和實體類映射。session
3.oracle
id主鍵:app
1)在mysql用自增字段,用auto increatmentide
在oracle 用 sequence測試
注意:ui
對於類裏面的對象裏的這個值就不能夠指定它了。得靠程序(數據庫)幫我自動生成;
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))