到新公司(有自己的一套框架)已经半年有余了,一直没有关注过java开源框架了。这几天突然感觉自己在技术上没有核心竞争力,于是决定狂补……
打算一个月内把hibernate全面的学习用一下(一直不知道hibernate search,跟lucene的完美结合,挺屌的)。这里我是直接从jboss官网上去看的hibernate documentation,下面我说一下这几天的所得,当然hibernate的基本架构和原理就不说了(官网上写的清清楚楚)。下面是我遇到的问题:
2014-3-11 13:17:15 org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit> INFO: HCANN000001: Hibernate Commons Annotations {4.0.4.Final} 2014-3-11 13:17:15 org.hibernate.Version logVersion INFO: HHH000412: Hibernate Core {4.3.2.Final} 2014-3-11 13:17:15 org.hibernate.cfg.Environment <clinit> INFO: HHH000206: hibernate.properties not found 2014-3-11 13:17:15 org.hibernate.cfg.Environment buildBytecodeProvider INFO: HHH000021: Bytecode provider name : javassist 2014-3-11 13:17:15 org.hibernate.cfg.Configuration configure INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml 2014-3-11 13:17:15 org.hibernate.cfg.Configuration getConfigurationInputStream INFO: HHH000040: Configuration resource: /hibernate.cfg.xml 2014-3-11 13:17:16 org.hibernate.cfg.Configuration addResource INFO: HHH000221: Reading mappings from resource: org/hibernate/tutorial/domain/Event.hbm.xml 2014-3-11 13:17:16 org.hibernate.cfg.Configuration doConfigure INFO: HHH000041: Configured SessionFactory: null 2014-3-11 13:17:16 org.hibernate.engine.jdbc.connections.internal.ConnectionProviderInitiator initiateService WARN: HHH000181: No appropriate connection provider encountered, assuming application will be supplying connections Initial SessionFactory creation failed.org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set Exception in thread "main" java.lang.ExceptionInInitializerError at org.hibernate.tutorial.util.HibernateUtil.buildSessionFactory(HibernateUtil.java:28) at org.hibernate.tutorial.util.HibernateUtil.<clinit>(HibernateUtil.java:9) at org.hibernate.tutorial.EventManager.createAndStoreEvent(EventManager.java:22) at org.hibernate.tutorial.EventManager.main(EventManager.java:15) Caused by: org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set at org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.determineDialect(DialectFactoryImpl.java:104) at org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.buildDialect(DialectFactoryImpl.java:71) at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:209) at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:89) at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:206) at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:178) at org.hibernate.cfg.Configuration.buildTypeRegistrations(Configuration.java:1885) at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1843) at org.hibernate.tutorial.util.HibernateUtil.buildSessionFactory(HibernateUtil.java:15) ... 3 more
如果你要是看我的blog,到这里估计你会觉得楼主很蛋疼。不过我可以坦白的说,这是直接在官网上copy的demo所出的issues。官网上的这个例子用的数据库是HSQLDB(我以后简称内存数据库,我也是第一次接触,搞得又跑到hsqldb官网下载,看了一下使用文档)。这几天我一直被这个问题困扰我对比了以前的版本,并且自己按以前版本写了也不会出问题,可是这次官网上的老让我郁闷了。下面是官网上对Sessionfactory的再次封装:
package org.hibernate.tutorial.util; import org.hibernate.SessionFactory; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.Configuration; public class HibernateUtil { private static final SessionFactory sessionFactory = buildSessionFactory(); private static SessionFactory buildSessionFactory() { SessionFactory sessionFactory = null; try { // Create the SessionFactory from hibernate.cfg.xml sessionFactory = new Configuration().configure().buildSessionFactory( new StandardServiceRegistryBuilder().build() ); } catch (Throwable ex) { // Make sure you log the exception, as it might be swallowed System.err.println("Initial SessionFactory creation failed." + ex); throw new ExceptionInInitializerError(ex); } return sessionFactory; } public static SessionFactory getSessionFactory() { return sessionFactory; } }
这样会报刚才提到的错误。
下面是我按以前版本,自己封装的:
package org.hibernate.tutorial.util; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class HibernateUtil { private static final SessionFactory sessionFactory = buildSessionFactory(); private static SessionFactory buildSessionFactory() { SessionFactory sessionFactory = null; try { // Create the SessionFactory from hibernate.cfg.xml sessionFactory = new Configuration().configure().buildSessionFactory(); } catch (Throwable ex) { // Make sure you log the exception, as it might be swallowed System.err.println("Initial SessionFactory creation failed." + ex); throw new ExceptionInInitializerError(ex); } return sessionFactory; } public static SessionFactory getSessionFactory() { return sessionFactory; } }
在hibernate4.3.X中buildSessionFactory()方法已经过时,但是能正常运行。
下面是用Hibernate4.3.x的api结合网上修改后的:
package org.hibernate.tutorial.util; import org.hibernate.SessionFactory; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.boot.registry.internal.StandardServiceRegistryImpl; import org.hibernate.cfg.Configuration; public class HibernateUtil { private static final SessionFactory sessionFactory = buildSessionFactory(); private static SessionFactory buildSessionFactory() { SessionFactory sessionFactory = null; try { // Create the SessionFactory from hibernate.cfg.xml Configuration configuration = new Configuration().configure(); StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder() .applySettings(configuration.getProperties()); StandardServiceRegistryImpl registry = (StandardServiceRegistryImpl) builder .build(); sessionFactory = configuration.buildSessionFactory(registry); } catch (Throwable ex) { // Make sure you log the exception, as it might be swallowed System.err.println("Initial SessionFactory creation failed." + ex); throw new ExceptionInInitializerError(ex); } return sessionFactory; } public static SessionFactory getSessionFactory() { return sessionFactory; } }
这个能正常运行,这个跟官网的代码一对比就显而易见的发现官网上为什么会报错了。
这个不得不提一下Hibernate4.x推荐使用Service Register机制。
以后我会详细记录什么是Service Register机制的。
相关推荐
hibernate事务管理机制.dochibernate事务管理机制.doc
hibernate缓存机制 hibernate缓存机制 hibernate缓存机制
首先说下Hibernate缓存的作用(即为什么要用缓存机制),然后再具体说说Hibernate中缓存的分类情况,最后可以举个具体的例子。 Hibernate缓存的作用: Hibernate是一个持久层框架
了解Hibernate的一个很好的文档,想学Hibernate的朋友可以载来看看。
Hibernate的缓存机制
对hibernate的flush机制有兴趣可以看看
Hibernate3核心机制 Hibernate 官方参考文档中文版
Hibernate延迟加载机制.zip
hibernate的缓存机制和session对象的产生方式案例,里面写到session的两种产生方式,和hibernate的缓存机制:一级缓存、二级缓存、查询缓存
基于hibernate的缓存机制,这个是打包后的jar包。
hibernate介绍与动手入门体验
Hibernate的缓存机制归纳.pdf
Hibernate 锁机制_悲观锁和乐观锁 Hibernate 锁机制是指在数据库访问中,为了维护数据的一致性和正确性,所采取的一些机制来防止数据的并发访问和修改。 Hibernate 中有两种锁机制:悲观锁和乐观锁。 一、悲观锁...
01 hibernate介绍与动手入门体验
传智播客\传智播客hibernate\01_传智播客hibernate教程_hibernate介绍与动手入门体验
本文详细介绍了Hibernate事务回滚失败的问题原因和解决方法,并对Hibernate的事务管理机制和事务回滚机制进行了详细说明。开发人员在使用Hibernate时,应该遵循Hibernate提供的API和事务管理机制,以确保数据库的...
Java框架篇—hibernate之缓存机制_.docx
Hibernate缓存机制探讨