hibernate4.0+版本和3.0+版本的區別總結
<property name=”dialect”>org.hibernate.dialect.MySQL5Dialect</property>
在3.3版本中連線MySQL資料庫只需要指明MySQLDialect即可。在4.1版本中可以指出MySQL5Dialect
2.buildSessionFactory
4.1版本中buildSessionFactory()已經被buildSessionFactory(ServiceRegistry ServiceRegistry)取代
解決辦法:
Configuration cfg = new Configuration().configure();
serviceRegistry = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()).build();
sessionFactory = cfg.buildSessionFactory(serviceRegistry);
3.annotation
org.hibernate.cfg.AnnotationConfiguration;這個註解讀取配置的class已經廢棄,現在讀取配置不需要特別註明是註解,
直接用Configuration cfg = new Configuration();就可以讀取註解。
Hibernate4.1版本中推薦使用annotation配置,所以在引進jar包時把requested裡面的包全部引進來就已經包含了annotation必須包了
Hibernate4 環境搭建:
匯入必須的Hibernate4 JAR包
${hibernate_home}\hibernate-release-4.0.0.Final\lib\required\*
資料庫驅動包(Oracle)
ojdbc14.jar
快取(可選)
${hibernate_home}\hibernate-release-4.0.0.Final\lib\optional\ehcache\*
${hibernate_home}\hibernate-distribution-3.6.0.Final\lib\required\slf4j-api-1.6.1.jar
C3P0連線池(可選)
${hibernate_home\hibernate-release-4.0.0.Final\lib\optional\c3p0\*
編寫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>
<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
<property name="hibernate.connection.username">scott</property>
<property name="hibernate.connection.password">tiger</property>
<property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<property name="hibernate.hbm2ddl.auto">none</property>
<property name="hibernate.jdbc.fetch_size">100</property>
<property name="hibernate.jdbc.batch_size">30</property>
<!-- 配置二級快取 -->
<property name="hibernate.cache.use_second_level_cache">true</property>
<property name="hibernate.cache.use_query_cache">true</property>
<!-- Hibernate4 這裡和Hibernate3不一樣 要特別注意!!!-->
<property name="hibernate.cache.region.factory_class">org.hibernate.cache.EhCacheRegionFactory</property>
<!-- Hibernate3 -->
<!-- <property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property> -->
<!-- 配置C3P0 -->
<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
<property name="hibernate.c3p0.max_size">10</property>
<property name="hibernate.c3p0.min_size">1</property>
<property name="hibernate.c3p0.max_statements">3</property>
<property name="hibernate.c3p0.timeout">30</property>
<property name="hibernate.c3p0.acquire_increment">1</property>
<property name="hibernate.c3p0.idle_test_periodt">10</property>
<mapping class="com.many2manay.Student" />
<mapping class="com.many2manay.teacher" />
<mapping class="com.one2many.Dept" />
<mapping class="com.one2many.Emp" />
<mapping class="com.one2one.Dept" />
<mapping class="com.one2one.Emp" />
</session-factory>
</hibernate-configuration>
hibernate使用版本是:hibernate-release-4.2.5.Final
需要的jar包:hibernate-release-4.2.5.Final\lib\required下所有jar包
ehcache jar包:hibernate-release-4.2.5.Final\lib\optional\ehcache下所有包
junit:junit-4.10.jar和mysql-connector-java-5.1.15-bin.jar
注:hibernate 4.2.5版本ehcache快取不依賴commons-logging-1.1.1.jar,需要的是slf4j-api-1.6.1.jar
===============================獲取Session=============================================:
Session session = sessionFactory.getCurrentSession();//推薦使用
getCurrentSession()與openSession()的區別
1、getCurrentSession () 使用當前的session openSession() 重新建立一個新的session
* 採用getCurrentSession()建立的session會繫結到當前執行緒中,而採用openSession()建立的session則不會
* 採用getCurrentSession()建立的session在commit或rollback時會自動關閉,
而採用openSession()建立的session必須手動關閉
2、使用getCurrentSession()需要在hibernate.cfg.xml檔案中加入如下配置:
* 如果使用的是本地事務(jdbc事務)
<property name="hibernate.current_session_context_class">thread</property>
* 如果使用的是全域性事務(jta事務)java分散式事務管理(多資料庫訪問)
jta由中介軟體提供(jboss WebLogic等,tomcat不支援)
<property name="hibernate.current_session_context_class">jta</property>
getCurrentSession()就是把事務和session繫結在了一起,所以就算是查詢這些操作也得開啟事務。
否則報:org.hibernate.HibernateException: get is not valid without active transaction
getCurrentSession()使用方法
例子之一:
Session session=HibernateSessionFactory.getSessionFactory().getCurrentSession();
session.beginTransaction();
StudentVo vo=(StudentVo)session.get(StudentVo.class, 1L);
session.getTransaction().commit();
例子之二:
Session session = sessionFactory.getCurrentSession();
session.beginTransaction();
session.save(t);
session.getTransaction().commit();
openSession()使用方法
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
session.save(t);
tx.commit();
session.close();