Hibernate事務代碼規範寫法
----------------siwuxie095
事務相關概念
1、什麽是事務
邏輯上的一組操作,構成這組操作的各個單元,要麽一起成功,
要麽一起失敗
2、事務的四個特性
1)原子性
2)一致性
3)隔離性
4)持久性
3、不考慮隔離性,產生的問題
1)臟讀
2)不可重復讀
3)幻讀(也稱 虛讀)
4、設置事務隔離級別,即可解決不考慮隔離性所產生的問題
「MySQL 默認的隔離級別:Repeatable Read(可重復讀)」
5、Hibernate 也可以在核心配置文件中配置事務隔離級別
<!-- 事務隔離級別: hibernate.connection.isolation = 4
1 - Read uncommitted isolation(未提交讀:臟讀、不可重復讀、幻讀均可能發生) 2 - Read committed isolation(已提交讀:防止臟讀發生,不可重復讀、幻讀均可能發生) 4 - Repeatable read isolation 8 - Serializable isolation(可串行化:防止臟讀、不可重復讀、幻讀發生) --> <property name="hibernate.connection.isolation">4</property> |
Hibernate 事務代碼規範寫法
1、代碼結構
2、具體代碼
主要函數
testTx():
//事務代碼規範寫法 @Test public void testTx(){
SessionFactory sessionFactory=null; Session session=null; Transaction tx=null;
try {
sessionFactory=HibernateUtils.getSessionFactory(); session=sessionFactory.openSession(); //開啟事務 tx=session.beginTransaction();
//添加操作 User user=new User(); user.setUsername("小明"); user.setPassword("8888"); user.setAddress("中國"); session.save(user);
//提交事務 tx.commit(); } catch (Exception e) { //回滾事務 tx.rollback(); } finally { //關閉資源 session.close(); sessionFactory.close(); }
} |
其中,HibernateUtils.java 和 User.java 分別如下:
HibernateUtils.java:
package com.siwuxie095.utils;
import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration;
public class HibernateUtils {
static Configuration cfg=null; static SessionFactory sessionFactory=null;
//或: 加上 private final 亦可,不過此時不能等於 null // private static final Configuration cfg; // private static final SessionFactory sessionFactory;
//靜態代碼塊 static { //加載核心配置文件 cfg=new Configuration(); cfg.configure(); sessionFactory=cfg.buildSessionFactory();
}
//提供方法返回 sessionFactory public static SessionFactory getSessionFactory() { return sessionFactory; }
} |
User.java:
package com.siwuxie095.entity;
public class User {
//Hibernate 要求實體類有一個屬性唯一,即 主鍵 private int uid; private String username; private String password; private String address;
public int getUid() { return uid; } public void setUid(int uid) { this.uid = uid; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; }
@Override public String toString() { return "User [uid=" + uid + ", username=" + username + ", password=" + password + ", address=" + address + "]"; }
} |
另:
User.hbm.xml:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd
該 dtd 文件是用於提示的文件,聯網即有提示信息,也可 手動添加:Window->Preferences->XML->XML Catalog, 點擊 Add 添加即可,Location 即路徑,Key 即上面的鏈接, Key type 即 URI -->
<!-- 根標簽 --> <hibernate-mapping>
<!-- (1) class 標簽:配置實體類和數據庫表的對應; name 屬性:實體類的全路徑,即 全限定名; table 屬性:數據庫表的名稱(數據庫表由 Hibernate 自動生成) --> <class name="com.siwuxie095.entity.User" table="t_user">
<!-- (2) id 標簽:配置實體類 id 和表 id 對應(主鍵); name 屬性:實體類裏 id 屬性名稱; column 屬性:生成表中 id 字段名稱 --> <!-- Hibernate 要求實體類有一個屬性唯一值, Hibernate 要求表中字段有一個屬性唯一值 --> <id name="uid" column="uid"> <!-- 設置數據庫表 id 的增長策略, native:主鍵 id 值自動增長 --> <generator class="native"></generator> </id>
<!-- (3) property 標簽:配置其它屬性和表中字段對應; name 屬性:實體類屬性名稱; column 屬性:生成表中字段名稱 --> <property name="username" column="username"></property> <property name="password" column="password"></property> <property name="address" column="address"></property> </class>
</hibernate-mapping> |
hibernate.cfg.xml:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<!-- 根標簽 --> <hibernate-configuration> <session-factory>
<!-- 第一部分:配置數據庫信息(必須) -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <!-- 或使用 jdbc:mysql:///hibernate_db 代替,省略 localhost --> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate_db</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">8888</property>
<!-- 第二部分:配置 Hibernate 信息(可選) -->
<!-- 輸出底層 sql 語句 --> <property name="hibernate.show_sql">true</property> <!-- 輸出底層 sql 語句格式 --> <property name="hibernate.format_sql">true</property> <!-- Hibernate 幫助創建表,不是自動創建,而需要配置之後。 update:如果已經有表,就更新,如果沒有,就自動創建 --> <property name="hibernate.hbm2ddl.auto">update</property> <!-- 配置數據庫方言,讓 Hibernate 框架識別不同數據庫自己特有的語句。 如:在 MySQL 中實現分頁的關鍵字 limit,只能在 MySQL 中使用,而 在 Oracle 中實現分頁的關鍵字則是 rownum --> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <!-- 事務隔離級別: hibernate.connection.isolation = 4
1 - Read uncommitted isolation(未提交讀:臟讀、不可重復讀、幻讀均可能發生) 2 - Read committed isolation(已提交讀:防止臟讀發生,不可重復讀、幻讀均可能發生) 4 - Repeatable read isolation(可重復讀:防止臟讀、不可重復讀發生,幻讀可能發生) 8 - Serializable isolation(可串行化:防止臟讀、不可重復讀、幻讀發生) --> <property name="hibernate.connection.isolation">4</property>
<!-- 第三部分:引入映射配置文件,把映射配置文件放到核心配置文件(必須) --> <mapping resource="com/siwuxie095/entity/User.hbm.xml"/> </session-factory> </hibernate-configuration> |
【made by siwuxie095】
Hibernate事務代碼規範寫法