前言:java
最近因爲有點時間,就像深刻的學習一下Hibernate.以前只是簡單的使用,並沒領會它的妙處。這裏就趁着分享的機會,好好整理一下。web
這篇主要講到了下面幾個部分:spring
Hibernate框架sql
Hibernate發展背景數據庫
Hibernate使用介紹apache
Hibernate Tools安裝編程
Hibernate那些必要的包緩存
首先說一下,這個Hibernate是作什麼的。Hibernate官網上面羅列了它的基本框架安全
咱們最經常使用的應該就是Hibernate ORM了。下面簡單的說下幾個框架的做用:數據結構
Hibernate ORM 主要用於持久化對象。
Hibernate Search 用於對對象進行搜索,底層基於Apache Lucene作的。
Hibernatte Validator 用於進行校驗
Hibernate OGM 用於Nosql的持久化
Hibernate Tools 是一款基於Eclipse的插件,用於快捷生成xxx.hbm.xml 以及 hibernate配置文件等。
下面簡單的介紹下ORM,最古老的編程模式沒有出現時,程序猿會把全部的內容寫在一塊兒,好比一個jsp,既有前臺頁面的內容,也有後來邏輯,甚至數據的查詢。對於開發者來講,痛不勘言,這樣分層的架構就出現了。
一般的web應用都會採用後者的模型來設計,展示層用於頁面的展示以及用戶的交互,好比struts,webwork,spring mvc等等。業務邏輯層用於對業務邏輯進行處理,這裏負責複雜的邏輯設計。數據層用於與數據庫進行交互。
通過這樣一個演變,層次化區分清晰了,可是編程卻比較痛苦。
業務邏輯或者前臺的數據,都是存儲在內存中,若是想要存儲在數據庫中,須要進行復雜的處理。好比JDBC來講,須要加載驅動,申請鏈接,建立執行計劃,最後還要不斷的釋放。操做數據常常變更,就要常常性的進行上述的處理。並且,內存中的數據,有可能由於斷電或者關機沒來得及存儲,致使丟失。
在此種狀況下,持久化應運而生。一方面負責把內存中的對象,隨時的與本地文件或者數據庫存儲關聯,另外一方面幫助管理JDBC鏈接,避免每次複雜的操做。
Hibernate跟JDBC相同,也須要一些操做步驟:
1 建立Configuration對象,配置hibernate。
2 建立SessionFactory,初始化Hibernate。這個SessionFactory是重量級的,也就是說一個應用能夠用這一個SessionFactory,由於SessionFatory一般做爲緩存存儲了實體對象的關係映射,所以儘可能不要頻繁的申請釋放SessionFactory。並且,SessionFactory是線程安全的,能夠多線程共用一個SessionFactory。
3 Session,保存更新刪除加載查詢對象。這個Session,就是一個相對輕量級的了,並且不是線程安全的,因此在每次操做結束應該釋放。
4 Transaction,用於管理事務。
5 Query,Criteria,用於進行Hql等的查詢。
在Eclipse的Help-->Install new Software
點擊Add,輸入下面網址:http://download.jboss.org/jbosstools/updates/stable/
點擊OK
查找Jboss tools裏面,點擊Hibernate Tools下載便可。
最小量的hibernate,須要如下幾個jar包:
hibernate.jar _ hibernate核心包
antlr.jar _ hql語法解析
asm.jar _ 字節碼相關
cglib.jar _ 生成實體對象相關
commons-logging.jar _ 日誌相關
dom4j.jar _ 解析xml
commons-collections.jar _ 集合框架
jta.jar _ 事務相關
Exception in thread "main" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ java.lang.NoClassDefFoundError: antlr/ANTLRException at org.hibernate.hql.ast.ASTQueryTranslatorFactory.createQueryTranslator(ASTQueryTranslatorFactory.java:35) at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:74) at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:56) at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:72) at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:133) at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:112) at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1623) at com.test.orm.Service.findAll(Service.java:37) at com.test.orm.Service.main(Service.java:23) Caused by: java.lang.ClassNotFoundException: antlr.ANTLRException at java.net.URLClassLoader$1.run(URLClassLoader.java:202) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:190) at java.lang.ClassLoader.loadClass(ClassLoader.java:306) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301) at java.lang.ClassLoader.loadClass(ClassLoader.java:247) ... 9 more
與字節碼操做有關
若是沒有這個jar包會報錯
java.lang.NoClassDefFoundError: org/objectweb/asm/Type at net.sf.cglib.core.TypeUtils.parseType(TypeUtils.java:180) at net.sf.cglib.core.KeyFactory.<clinit>(KeyFactory.java:66) at net.sf.cglib.proxy.Enhancer.<clinit>(Enhancer.java:69) at org.hibernate.proxy.pojo.cglib.CGLIBLazyInitializer.getProxyFactory(CGLIBLazyInitializer.java:117) at org.hibernate.proxy.pojo.cglib.CGLIBProxyFactory.postInstantiate(CGLIBProxyFactory.java:43) at org.hibernate.tuple.entity.PojoEntityTuplizer.buildProxyFactory(PojoEntityTuplizer.java:162) at org.hibernate.tuple.entity.AbstractEntityTuplizer.<init>(AbstractEntityTuplizer.java:135) at org.hibernate.tuple.entity.PojoEntityTuplizer.<init>(PojoEntityTuplizer.java:55) at org.hibernate.tuple.entity.EntityEntityModeToTuplizerMapping.<init>(EntityEntityModeToTuplizerMapping.java:56) at org.hibernate.tuple.entity.EntityMetamodel.<init>(EntityMetamodel.java:295) at org.hibernate.persister.entity.AbstractEntityPersister.<init>(AbstractEntityPersister.java:434) at org.hibernate.persister.entity.SingleTableEntityPersister.<init>(SingleTableEntityPersister.java:109) at org.hibernate.persister.PersisterFactory.createClassPersister(PersisterFactory.java:55) at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:226) at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1294) at com.test.orm.Service.<clinit>(Service.java:16) Caused by: java.lang.ClassNotFoundException: org.objectweb.asm.Type at java.net.URLClassLoader$1.run(URLClassLoader.java:202) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:190) at java.lang.ClassLoader.loadClass(ClassLoader.java:306) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301) at java.lang.ClassLoader.loadClass(ClassLoader.java:247) ... 16 more Exception in thread "main"
實現持久化對象的字節碼生成,用於生成code的類庫
若是沒有這個jar包,會報錯
java.lang.NoClassDefFoundError: net/sf/cglib/proxy/CallbackFilter at org.hibernate.bytecode.cglib.BytecodeProviderImpl.getProxyFactoryFactory(BytecodeProviderImpl.java:33) at org.hibernate.tuple.entity.PojoEntityTuplizer.buildProxyFactoryInternal(PojoEntityTuplizer.java:182) at org.hibernate.tuple.entity.PojoEntityTuplizer.buildProxyFactory(PojoEntityTuplizer.java:160) at org.hibernate.tuple.entity.AbstractEntityTuplizer.<init>(AbstractEntityTuplizer.java:135) at org.hibernate.tuple.entity.PojoEntityTuplizer.<init>(PojoEntityTuplizer.java:55) at org.hibernate.tuple.entity.EntityEntityModeToTuplizerMapping.<init>(EntityEntityModeToTuplizerMapping.java:56) at org.hibernate.tuple.entity.EntityMetamodel.<init>(EntityMetamodel.java:295) at org.hibernate.persister.entity.AbstractEntityPersister.<init>(AbstractEntityPersister.java:434) at org.hibernate.persister.entity.SingleTableEntityPersister.<init>(SingleTableEntityPersister.java:109) at org.hibernate.persister.PersisterFactory.createClassPersister(PersisterFactory.java:55) at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:226) at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1294) at com.test.orm.Service.<clinit>(Service.java:16) Caused by: java.lang.ClassNotFoundException: net.sf.cglib.proxy.CallbackFilter at java.net.URLClassLoader$1.run(URLClassLoader.java:202) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:190) at java.lang.ClassLoader.loadClass(ClassLoader.java:306) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301) at java.lang.ClassLoader.loadClass(ClassLoader.java:247) ... 13 more Exception in thread "main"
Apache標準日誌接口
若是沒有這個jar包,會報錯
java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory at org.hibernate.cfg.Configuration.<clinit>(Configuration.java:120) at com.test.orm.Service.<clinit>(Service.java:14) Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory at java.net.URLClassLoader$1.run(URLClassLoader.java:202) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:190) at java.lang.ClassLoader.loadClass(ClassLoader.java:306) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301) at java.lang.ClassLoader.loadClass(ClassLoader.java:247) ... 2 more Exception in thread "main"
若是沒有這個jar包,會報錯
java.lang.NoClassDefFoundError: org/dom4j/DocumentException at com.test.orm.Service.<clinit>(Service.java:14) Caused by: java.lang.ClassNotFoundException: org.dom4j.DocumentException at java.net.URLClassLoader$1.run(URLClassLoader.java:202) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:190) at java.lang.ClassLoader.loadClass(ClassLoader.java:306) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301) at java.lang.ClassLoader.loadClass(ClassLoader.java:247) ... 1 more Exception in thread "main"
集合與數據結構的加強包。Apache的集合框架,比jdk的功能完善。
若是沒有這個jar包,會報錯
java.lang.NoClassDefFoundError: org/apache/commons/collections/SequencedHashMap at org.hibernate.mapping.Table.<init>(Table.java:33) at org.hibernate.cfg.Mappings.addTable(Mappings.java:165) at org.hibernate.cfg.HbmBinder.bindRootPersistentClassCommonValues(HbmBinder.java:290) at org.hibernate.cfg.HbmBinder.bindRootClass(HbmBinder.java:273) at org.hibernate.cfg.HbmBinder.bindRoot(HbmBinder.java:144) at org.hibernate.cfg.Configuration.add(Configuration.java:669) at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:504) at org.hibernate.cfg.Configuration.addResource(Configuration.java:536) at org.hibernate.cfg.Configuration.addClass(Configuration.java:586) at com.test.orm.Service.<clinit>(Service.java:15) Caused by: java.lang.ClassNotFoundException: org.apache.commons.collections.SequencedHashMap at java.net.URLClassLoader$1.run(URLClassLoader.java:202) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:190) at java.lang.ClassLoader.loadClass(ClassLoader.java:306) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301) at java.lang.ClassLoader.loadClass(ClassLoader.java:247) ... 10 more Exception in thread "main"
事務處理
若是沒有這個jar包,會報錯
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment). log4j:WARN Please initialize the log4j system properly. Exception in thread "main" java.lang.NoClassDefFoundError: javax/transaction/Synchronization at org.hibernate.impl.SessionImpl.<init>(SessionImpl.java:213) at org.hibernate.impl.SessionFactoryImpl.openSession(SessionFactoryImpl.java:473) at org.hibernate.impl.SessionFactoryImpl.openSession(SessionFactoryImpl.java:497) at org.hibernate.impl.SessionFactoryImpl.openSession(SessionFactoryImpl.java:505) at com.test.orm.Service.findAll(Service.java:34) at com.test.orm.Service.main(Service.java:26) Caused by: java.lang.ClassNotFoundException: javax.transaction.Synchronization at java.net.URLClassLoader$1.run(URLClassLoader.java:202) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:190) at java.lang.ClassLoader.loadClass(ClassLoader.java:306) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301) at java.lang.ClassLoader.loadClass(ClassLoader.java:247) ... 6 more