Hibernate框架介绍:
Hibernate ORM 主要用于持久化对象(最常用的框架)
Hibernate Search 用于对对象进行搜索,底层基于Apache Lucene做的
Hibernate Validator 用于进行校验
Hibernate OGM 用于Nosql的持久化
Hibernate Tools 是一款基于Eclipse的插件,用于快捷生成xxx.hbm.xml以及hibernate配置文件等
Hibernate产生的背景
在最古老的编程模式还没出现的时候,程序猿会把所有的内容都写在一起,比如一个jsp文件,既有前台页面又有后台逻辑,甚至数据查询,对于开发者而言,苦不堪言,这样分层的架构应运而生
通常的web应用都会采用后者的模型来设计,展现层用于显示页面以及用户的交互,比如Struts,webwork,springmvc等,业务逻辑层用于对业务逻辑进行处理,这里负责复杂的逻辑设计,数据层用于与数据库进行交互。
经过这样的一个演变,层次化区分清晰了,但是编程却比较痛苦。
业务逻辑或者前台数据都是存储在内存中的,如果想要存储在数据库中,需要进行复杂的处理。比如JDBC来说,需要加载驱动,申请链接,创建执行计划,最后还要不断的释放,操作数据经常变动,就要经常性的进行上述的处理,而且,内存中的数据,有可能因为断电或者关机没来得及存储,导致丢失。
在这种情况下,持久化应运而生,一方面负责把内存中的对象,随时的与本地文件或者数据库存储关联,另一方面帮助管理JDBC连接,避免每次复杂的操作。
Hibernate入门使用
Hibernate跟JDBC一样需要进行一些操作步骤:
1) 创建Configuration对象,配置Hibernate
2) 创建SessionFactory,初始化Hibernate,这个SessionFactory是重量级的,也就是说一个应用可以用这一个SessionFactory,因为SessionFactory通常作为缓存存储了实体对象的关系映射,因此进来不要频繁的申请释放SessionFactory
3)Session,保存更新删除加载查询对象,这个session就是一个相对轻量级的了,而且不是线程安全的,所以每次结束操作之后应该释放
4)Transaction,用于事务管理
5)Query,Criteria,用于进行Hql等的查询
Eclipse在线安装使用Hibernate Tools
在Eclipse的Help-->Install new Software
点击Add,输入下面网址:http://download.jboss.org/jbosstools/updates/stable/
点击OK
查找Jboss tools里面,点击Hibernate Tools下载即可。
Hibernate用的包及作用
最小量的hibernate,需要以下几个jar包:
hibernate.jar_hibernate核心包
actlr.jar_hql语法解析
asm.jar _ 字节码相关
cglib.jar _ 生成实体对象相关
commons-logging.jar _ 日志相关
dom4j.jar _ 解析xml
commons-collections.jar _ 集合框架
jta.jar _ 事务相关
antlr.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
asm.jar
与字节码操作有关
如果没有这个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"
cglib.jar
实现持久化对象的字节码生成,用于生成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"
commons-logging.jar
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"
dom4j.jar
如果没有这个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"
commons-collections.jar
集合与数据结构的增强包。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"
jta.jar
事务处理
如果没有这个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