time 
设为首页】【收藏本站
当前位置: 主页 > JAVA > Hibernate > Hibernate4出现Connection cannot be null when 'hibernate.dialect' not set的解决方法

Hibernate4出现Connection cannot be null when 'hibernate.dialect' not set的解决方法

时间:2013-10-17 13:45 点击:24099次 字体:[ ]




今天在做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中测试:

 

  1. import org.hibernate.Session;  
  2. import org.hibernate.SessionFactory;  
  3. import org.hibernate.cfg.Configuration;  
  4. import org.hibernate.service.ServiceRegistryBuilder;  
  5. import test.model.Blackey;  
  6. public class Test {  
  7.     public static void main(String [] args){  
  8.         Blackey bk = new Blackey();  
  9.         bk.setName("测试");  
  10.         bk.setVersion("1.0.4");  
  11.         bk.setUid("Test");  
  12.         bk.setKey("1fD46a709C920a01477632");  
  13.         bk.setDescribe("该注册码只能用于该版本!");  
  14.           
  15.         Configuration cfg = new Configuration().configure();  
  16.         SessionFactory sf = cfg.buildSessionFactory(new ServiceRegistryBuilder().buildServiceRegistry());  //以前的cfg.buildSessionFactory()方法被废弃,查网上大多使用该方法  
  17.         Session session = sf.openSession();  
  18.         session.beginTransaction();  
  19.         session.save(bk);  
  20.         session.getTransaction().commit();  
  21.         session.close();  
  22.         sf.close();  
  23.           
  24.     }  
  25. }  

  结果显示报错:

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

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

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

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

替换成

 

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

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

  1. Exception in thread "main" org.hibernate.HibernateException: Error applying BeanValidation relational constraints  
  2.     at org.hibernate.cfg.beanvalidation.BeanValidationIntegrator.applyRelationalConstraints(BeanValidationIntegrator.java:219)  
  3.     at org.hibernate.cfg.beanvalidation.BeanValidationIntegrator.integrate(BeanValidationIntegrator.java:126)  
  4.     at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:301)  
  5.     at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1750)  
  6.     at Test.main(Test.java:19)  
  7. Caused by: java.lang.reflect.InvocationTargetException  
  8.     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)  
  9.     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)  
  10.     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)  
  11.     at java.lang.reflect.Method.invoke(Method.java:597)  
  12.     at org.hibernate.cfg.beanvalidation.BeanValidationIntegrator.applyRelationalConstraints(BeanValidationIntegrator.java:208)  
  13.     ... 4 more  
  14. Caused by: org.hibernate.HibernateException: Unable to build the default ValidatorFactory  
  15.     at org.hibernate.cfg.beanvalidation.TypeSafeActivator.getValidatorFactory(TypeSafeActivator.java:524)  
  16.     at org.hibernate.cfg.beanvalidation.TypeSafeActivator.applyDDL(TypeSafeActivator.java:119)  
  17.     ... 9 more  
  18. Caused by: javax.validation.ValidationException: Unable to instantiate Configuration.  
  19.     at javax.validation.Validation$GenericBootstrapImpl.configure(Validation.java:272)  
  20.     at javax.validation.Validation.buildDefaultValidatorFactory(Validation.java:111)  
  21.     at org.hibernate.cfg.beanvalidation.TypeSafeActivator.getValidatorFactory(TypeSafeActivator.java:521)  
  22.     ... 10 more  
  23. Caused by: java.lang.NullPointerException  
  24.     at java.util.ResourceBundle.getBundle(ResourceBundle.java:960)  
  25.     at org.hibernate.validator.engine.ResourceBundleMessageInterpolator.loadBundle(ResourceBundleMessageInterpolator.java:202)  
  26.     at org.hibernate.validator.engine.ResourceBundleMessageInterpolator.getFileBasedResourceBundle(ResourceBundleMessageInterpolator.java:182)  
  27.     at org.hibernate.validator.engine.ResourceBundleMessageInterpolator.<init>(ResourceBundleMessageInterpolator.java:81)  
  28.     at org.hibernate.validator.engine.ResourceBundleMessageInterpolator.<init>(ResourceBundleMessageInterpolator.java:73)  
  29.     at org.hibernate.validator.engine.ConfigurationImpl.<init>(ConfigurationImpl.java:57)  
  30.     at org.hibernate.validator.HibernateValidator.createGenericConfiguration(HibernateValidator.java:43)  
  31.     at javax.validation.Validation$GenericBootstrapImpl.configure(Validation.java:269)  
  32.     ... 12 more  

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

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

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

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



本文地址 : http://www.fengfly.com/plus/view-213932-1.html
标签: Hibernate cannot be null hibernate.dialect connection
------分隔线----------------------------
最新评论 查看所有评论
发表评论 查看所有评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
验证码: