Hibernate Service Register机制初体验

      到新公司(有自己的一套框架)已经半年有余了,一直没有关注过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机制的。

猜你喜欢

转载自weizhilizhiwei.iteye.com/blog/2029392