正确决解Hibernate4.*中:Connection cannot be null when 'hibernate.dialect' not set

http://www.cnblogs.com/cnscoo/archive/2012/09/17/2689021.html

今天在做Hibernate4.*的项目中,使用了以下的配置:

hibernate.cfg.xml

 1  <?xml version='1.0' encoding='utf-8'?>
 2  <!DOCTYPE hibernate-configuration PUBLIC
 3      "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
 4      "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
 5  <hibernate-configuration>
 6      <session-factory>
 7          <property name="show_sql">true</property>
 8          <property name="format_sql">true</property>
 9          <property name="dialect">org.hibernate.dialect.SQLiteDialect</property>
10          <property name="connection.driver_class">org.sqlite.JDBC</property>
11          <property name="connection.url">jdbc:sqlite:D:\Documents\Dbs\test.db</property>
12          <property name="connection.username"></property>
13          <property name="connection.password"></property>
14  
15          <mapping resource="test/model/Blackey.hbm.xml" />
16      </session-factory>
17  </hibernate-configuration>    

然后在Text.java中测试:

 import org.hibernate.Session;
 import org.hibernate.SessionFactory;
 import org.hibernate.cfg.Configuration;
 import org.hibernate.service.ServiceRegistryBuilder;
 import test.model.Blackey;
 public class Test {
 	public static void main(String [] args){
 		Blackey bk = new Blackey();
 		bk.setName("测试");
 		bk.setVersion("1.0.4");
 		bk.setUid("Test");
 		bk.setKey("1fD46a709C920a01477632");
 		bk.setDescribe("该注册码只能用于该版本!");
 		
 		Configuration cfg = new Configuration().configure();
 		SessionFactory sf = cfg.buildSessionFactory(new ServiceRegistryBuilder().buildServiceRegistry());  //以前的cfg.buildSessionFactory()方法被废弃,查网上大多使用该方法
 		Session session = sf.openSession();
 		session.beginTransaction();
 		session.save(bk);
 		session.getTransaction().commit();
 		session.close();
 		sf.close();
 		
 	}
 }

  结果显示报错:

Exception in thread "main" org.hibernate.HibernateException: Connection cannot be null when 'hibernate.dialect' not set
 	at org.hibernate.service.jdbc.dialect.internal.DialectFactoryImpl.determineDialect(DialectFactoryImpl.java:97)
 	at org.hibernate.service.jdbc.dialect.internal.DialectFactoryImpl.buildDialect(DialectFactoryImpl.java:67)
 	at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:174)
 	at org.hibernate.service.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:75)
 	at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:159)
 	at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:131)
 	at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:73)
 	at org.hibernate.cfg.Configuration.buildSettingsInternal(Configuration.java:2283)
 	at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2279)
 	at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1748)
 	at Test.main(Test.java:19)

  一直让人很费解,因为显而易见,我的dialect是明确定义了的(说明一下:我个人已经把org.hibernate.dialect.SQLiteDialect文件打包到了hibernate-core-4.1.*.jar中,所以不会有此类不存在的可能),但为什么说我的dialect未定义呢?

  在网上找相关资料,发现很多人都存在这个问题,可惜没有发现很好的解决办法,有人只是测试使用 hibernate.properties来进行配置,则可以成功。个人还是觉得使用xml配置好,转向国外网站查看,终于看到了一个老外的解决办法:

SessionFactory sf = cfg.buildSessionFactory(new ServiceRegistryBuilder().buildServiceRegistry());

替换成

SessionFactory sf = cfg.buildSessionFactory(new ServiceRegistryBuilder().applySettings(cfg.getProperties()).buildServiceRegistry());

表示应用hibernate.cfg.xml中的属性配置,接着运行,发现又错了:

 Exception in thread "main" org.hibernate.HibernateException: Error applying BeanValidation relational constraints
 	at org.hibernate.cfg.beanvalidation.BeanValidationIntegrator.applyRelationalConstraints(BeanValidationIntegrator.java:219)
 	at org.hibernate.cfg.beanvalidation.BeanValidationIntegrator.integrate(BeanValidationIntegrator.java:126)
 	at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:301)
 	at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1750)
 	at Test.main(Test.java:19)
 Caused by: java.lang.reflect.InvocationTargetException
 	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
 	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
 	at java.lang.reflect.Method.invoke(Method.java:597)
 	at org.hibernate.cfg.beanvalidation.BeanValidationIntegrator.applyRelationalConstraints(BeanValidationIntegrator.java:208)
 	... 4 more
 Caused by: org.hibernate.HibernateException: Unable to build the default ValidatorFactory
 	at org.hibernate.cfg.beanvalidation.TypeSafeActivator.getValidatorFactory(TypeSafeActivator.java:524)
 	at org.hibernate.cfg.beanvalidation.TypeSafeActivator.applyDDL(TypeSafeActivator.java:119)
 	... 9 more
 Caused by: javax.validation.ValidationException: Unable to instantiate Configuration.
 	at javax.validation.Validation$GenericBootstrapImpl.configure(Validation.java:272)
 	at javax.validation.Validation.buildDefaultValidatorFactory(Validation.java:111)
 	at org.hibernate.cfg.beanvalidation.TypeSafeActivator.getValidatorFactory(TypeSafeActivator.java:521)
 	... 10 more
 Caused by: java.lang.NullPointerException
 	at java.util.ResourceBundle.getBundle(ResourceBundle.java:960)
 	at org.hibernate.validator.engine.ResourceBundleMessageInterpolator.loadBundle(ResourceBundleMessageInterpolator.java:202)
 	at org.hibernate.validator.engine.ResourceBundleMessageInterpolator.getFileBasedResourceBundle(ResourceBundleMessageInterpolator.java:182)
 	at org.hibernate.validator.engine.ResourceBundleMessageInterpolator.<init>(ResourceBundleMessageInterpolator.java:81)
 	at org.hibernate.validator.engine.ResourceBundleMessageInterpolator.<init>(ResourceBundleMessageInterpolator.java:73)
 	at org.hibernate.validator.engine.ConfigurationImpl.<init>(ConfigurationImpl.java:57)
 	at org.hibernate.validator.HibernateValidator.createGenericConfiguration(HibernateValidator.java:43)
 	at javax.validation.Validation$GenericBootstrapImpl.configure(Validation.java:269)
 	... 12 more

从网上搜了一下,发现还需要在hibernate.cfg.xml中加入一条配置:

<property name="javax.persistence.validation.mode">none</property>

关键在于Unable to instantiate Configuration, 如不添加默认会要求Validation provider。

做完这些,然后运行Test.java,一切正常。 OK,问题解决。

posted @ 2012-09-17 17:10  雨秋  阅读(5494)  评论(0编辑  收藏  举报