Hibernate配置

1.部署

  將Hibernate解壓後lib下required的所有jar包導入工程中,其餘文件夾的jar包可根據實際需要導入。

 

2.在src下創建hibernate.cfg.xml配置文件

 hibernate.cfg.xml

<!DOCTYPE hibernate-configuration PUBLIC  
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
        <property name="hibernate.connection.url">jdbc:sqlserver://localhost:1433;DatabaseName=goodsmessage</property>
        <property name="hibernate.connection.username">mike</property>
        <property name="hibernate.connection.password">mike</property>
        <property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
        <mapping resource="conn/User.hbm.xml"/>
        <mapping resource="conn/Goods.hbm.xml"/>
        <mapping resource="conn/Orders.hbm.xml"/>
        <mapping resource="conn/Cart.hbm.xml"/>
        <mapping resource="conn/SuperType.hbm.xml"/>
        <mapping resource="conn/SubType.hbm.xml"/>
    </session-factory>
</hibernate-configuration>

3.創建數據庫表所對應的類,並編寫或使用工具生成對應的hbm.xml文件

 User.java 

package conn;

public class User {
	private String username;
	private String pwd;
	
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPwd() {
		return pwd;
	}
	public void setPwd(String pwd) {
		this.pwd = pwd;
	}

}

User.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">

<hibernate-mapping>
    <class name="conn.User" table="users">
        <id name="username" type="java.lang.String">
            <column name="username" />
        <generator class="assigned"/>
        </id>
        <property name="pwd" type="java.lang.String">
            <column name="pwd" />
        </property>
    </class>
</hibernate-mapping>

<generator class="assigned"/>指定了主鍵生成方式。

   native:由數據庫對id賦值,Hibernate根據數據庫適配器的定義,自動採用identity、hilo、sequence的其中一種作爲主鍵生成方式

   hilo:用過hi/lo算法實現的主鍵生成機制,需要額外的數據庫表保存主鍵生成歷史狀態

   seqhilo:與hilo類似,通過hi/lo算法實現的之間生成機制,只是主鍵歷史狀態保存在Sequence中,適用於支持Sequence的數據庫,如Oracle

   increment:主鍵按數值順序遞增

   identity:採用數據爲提供的主鍵生成機制,如SQL Server、MySQL中的自增主鍵生成機制

   sequence:採用數據庫提供的sequence機制生成主鍵,如Oracle Sequence

   uuid.hex:由Hibernate基於128位唯一值產生算法生成十六進制數值作爲主鍵,在最大限度上保證產生的id的唯一性

   foreign:使用外部表的字段作爲主鍵

4.應用

//加載配置文件
Configurationconfiguration = new Configuration().configure();

//向Hibernate註冊相關服務
@SuppressWarnings("deprecation")
ServiceRegistryserviceRegistry= newServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();

//創建會話工廠
SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);

Session session= null;

//一個典型的事務應使用try{}catch{}finally{}
try{
        //打開會話
        session = factory.openSession();

       //開始事務
        session.beginTransaction();

        /*
           處理事務...
        */

        //提交事務
        session.getTransaction().commit();         
}catch(Exception e){
        e.printStackTrace();
        //事務回滾
        session.getTransaction().rollback();
}finally{
        if(session != null){
            if(session.isOpen()){
                 //關閉session
                 session.close();
            }
        }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章