Hibernate學習-5-設定檔詳解

來源:互聯網
上載者:User

標籤:


自動建表的配置
建完表後,最好關掉配置
<property name="hibernate.hbm2ddl.auto">create</property>
每次都重建立表,如果表已經存在就先刪除再建立。


<property name="hibernate.hbm2ddl.auto">create-drop</property>

每次在建立sessionFactory時執行建立表
當調用sessionFactory的close方法的時候,再刪除。

<property name="hibernate.hbm2ddl.auto">update</property>

如果不存在就建立;存在就不建立。

<property name="hibernate.hbm2ddl.auto">validate</property>

執行驗證,當對應檔與表結構不一樣,報錯

通過代碼自動建表,執行代碼後會根據class.hbm.xml檔案中的內容自動建表
package com.cx.hello;
import org.hibernate.cfg.Configuration;import org.hibernate.tool.hbm2ddl.SchemaExport;import org.junit.Test;/** * Created by cxspace on 16-7-23. */public class App_ddl { //自動建表 @Test public void testCreate() throws Exception{ //建立組態管理對象 Configuration config = new Configuration(); //載入主設定檔 config.configure(); //建立工具類對象 SchemaExport export = new SchemaExport(config); //建表,這兩個參數一個在控制台顯示指令碼,一個執行指令碼 export.create(true,true); }}

 對象映射設定檔詳解

<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"><!--   對應檔作用,映射一個實體類對象,描述一個對象最終實現可以直接儲存對象資料到資料庫--><!--    package 要映射的對象所在的包   (可選)如果不指定,下面的所有的類都需要指定全路徑    auto-import 預設為true,在寫hql的時候自動匯入    包名,如果指定為false,再寫hql的時候要寫上類的全名    如:    session.createQuery("from cn.cxspace.Employee").list();--><hibernate-mapping package="com.cx.hello" >    <!--       class 映射某一個對象的(一般情況,一個對象寫一個對應檔,小寫一個class節點)         name 指定要映射的物件類型         table 對象對應的表(如果沒有指定表名,預設和對象名一樣)    -->    <class name="Employee" table="employee">        <!--對象與表,欄位與屬性-->        <!--主鍵,映射-->        <id name="empId" column="id">            <!--                主鍵的建置原則                  identity 自增長 (mysql , db2)                  native 自增長                   [會根據資料庫自增長的方式選擇identity或sequence]                   如果是mysql資料庫,那麼使用identity實現自增長                   如果是oracle,那麼使用sequence實現自增長                   sequence 自增長(序列),oracle中自增長是以序列方式實現的                   increment 自增長(會有並發問題,一般在伺服器叢集環境使用會存在並發問題)                   assigned 指定主鍵建置原則為手動指定主鍵的值                   uuid 指定uuid隨機產生的惟一的值                   foreign  (外鍵方式)            -->            <generator class="native"/>        </id>        <!--非主鍵,映射-->        <!--                普通欄位對應                property                name     指定對象的屬性名稱                column 指定對象屬性對象表的欄位名稱,如果不寫預設與對象的屬性一致。                       (列名為關鍵字,需要加單引號或者改列名)                length 指定字元類型的長度,預設為255                type 指定表欄位的類型                     java類型 : 必須寫類全名                     hibernate類型 : 直接寫類型,都是小寫        -->        <property name="empName" column="empName" length="20"></property>        <property name="workDate" column="workDate" ></property>    </class></hibernate-mapping>

關於主鍵的一些問題

 

     一個表能否有多個主鍵 ?   不能

 

    為什麼要設定主鍵  ? 資料庫儲存的資料都是有效,必須保持唯一。

 

    為什麼把id作為主鍵 ?   id保證了記錄的唯一性。

 

    聯合主鍵

    [如果找不到合適的列作為主鍵,除了用id列已外,一般用聯合主鍵,即多列的值作為一個主鍵,從而保證記錄的唯一性]

 

複合主鍵的表建立(t1,t1聯合主鍵)CREATE TABLE t(       t1 int,     t2 int,     msg VARCHAR(20),    PRIMARY KEY(t1 , t2));

 

 

 

 

 

 

 

 









Hibernate學習-5-設定檔詳解

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.