【Hibernate那点事儿】—— 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入门使用

Hibernate跟JDBC相同,也需要一些操作步骤:

1 创建Configuration对象,配置hibernate

2 创建SessionFactory,初始化Hibernate。这个SessionFactory是重量级的,也就是说一个应用可以用这一个SessionFactory,因为SessionFatory通常作为缓存存储了实体对象的关系映射,因此尽量不要频繁的申请释放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核心包

antlr.jar _ hql语法解析

asm.jar _ 字节码相关

cglib.jar _ 生成实体对象相关

commons-logging.jar _ 日志相关

dom4j.jar _ 解析xml

commons-collections.jar _ 集合框架

jta.jar _ 事务相关

  antlr.jar

  JPA标准中,面向对象的语法检查、识别。语法分析生成器,转换hql语法。如果没有此包 hql会出错。比如from com.person  –> select * from PERSON;
  如果没有这个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

  解析读取xml

如果没有这个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


相关文章

发表评论

Comment form

(*) 表示必填项

还没有评论。

跳到底部
返回顶部