快捷搜索:  as  2018  FtCWSyGV  С˵  test  xxx  Ψһ  w3viyKQx

澳门新葡亰平台app_龟发之家论坛



本日在做Hibernate4.*的项目中,应用了以下的设置设置设备摆设摆设:

hibernate.cfg.xml

xml version='1.0' encoding='utf-8'?>

"-//Hibernate/Hibernate Configuration DTD 3.0//EN"

"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">hibernate-configuration>

session-factory>property name="show_sql">trueproperty>

property name="format_sql">trueproperty>property name="dialect">org.hibernate.dialect.SQLiteDialectproperty>

property name="connection.driver_class">org.sqlite.JDBCproperty>property name="connection.url">jdbc:sqlite:D:\Documents\Dbs\test.dbproperty>

property name="connection.username">property>property name="connection.password">property>

mapping resource="test/model/Blackey.hbm.xml" />

session-factory>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(DialectFactory澳门新葡亰平台appImpl.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 t澳门新葡亰平台apphread "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澳门新葡亰平台app: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 ValidatorFactoryat 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 moreCaused 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>(Configurati澳门新葡亰平台apponImpl.java:57)

at org.hibernate.validator.HibernateValidator.澳门新葡亰平台appcreateGenericConfiguration(HibernateValidator.java:43)at javax.validation.Validation$GenericBootstrapImpl.configure(Validation.java:269)

... 12 more

从网上搜了一下,发明还必要在hibernate.cfg.xml中加入一条设置设置设备摆设摆设:

property name="javax.persistence.validation.mode">noneproperty>

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

做完这些,然后运行Test.java,统统正常。 OK,问题办理。

您可能还会对下面的文章感兴趣: