Hibernate 4.1.2 HHH000342:无法获得与查询元数据的连接:没有为 jdbc:hsqldb:mem:test 找到合适的驱动程序

Posted

技术标签:

【中文标题】Hibernate 4.1.2 HHH000342:无法获得与查询元数据的连接:没有为 jdbc:hsqldb:mem:test 找到合适的驱动程序【英文标题】:Hibernate 4.1.2 HHH000342: Could not obtain connection to query metadata : No suitable driver found for jdbc:hsqldb:mem:test 【发布时间】:2012-04-05 17:21:27 【问题描述】:

我做错了什么?

我的persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
    <persistence-unit name="Test" transaction-type="RESOURCE_LOCAL">
           <provider>org.hibernate.ejb.HibernatePersistence</provider>
           <class>User</class>
            <properties>
              <property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver"/>
              <property name="hibernate.connection.url" value="jdbc:hsqldb:mem:test"/>
              <property name="hibernate.connection.password" value="sa"/>
              <property name="hibernate.connection.username" value=""/>
              <property name="hibernate.hbm2ddl.auto" value="create"/>
              <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
              <property name="hibernate.show_sql" value="true"/>
              <property name="hibernate.format_sql" value="true"/>
            </properties>
          </persistence-unit>

我的代码

...
Persistence.createEntityManagerFactory("Test")
...

我收到此错误

javax.persistence.PersistenceException: [PersistenceUnit: Test] Unable to build EntityManagerFactory
    at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:915)
    at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:890)
    at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:57)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:63)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:47)
    at com.aol.biz.vendor.persistence.TestVpsUser.setUp(TestVpsUser.java:32)
    at junit.framework.TestCase.runBare(TestCase.java:125)
    at junit.framework.TestResult$1.protect(TestResult.java:106)
    at junit.framework.TestResult.runProtected(TestResult.java:124)
    at junit.framework.TestResult.run(TestResult.java:109)
    at junit.framework.TestCase.run(TestCase.java:118)
    at junit.framework.TestSuite.runTest(TestSuite.java:208)
    at junit.framework.TestSuite.run(TestSuite.java:203)
    at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: 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:170)
    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:71)
    at org.hibernate.cfg.Configuration.buildSettingsInternal(Configuration.java:2274)
    at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2270)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1739)
    at org.hibernate.ejb.EntityManagerFactoryImpl.<init>(EntityManagerFactoryImpl.java:93)
    at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:905)
    ... 18 more

但是!!

如果我在创建工厂之前手动加载了 jdbc 驱动程序类 - 一切正常。 O_o

try 
            Class.forName("org.hsqldb.jdbcDriver");
         catch (ClassNotFoundException e) 
            // TODO Auto-generated catch block
            e.printStackTrace();
        

如果没有这一步,我怎么能做到这一切?请帮忙!

【问题讨论】:

这个错误可能有很多原因。你可以看看这个***.com/questions/9126531/…也许会有所帮助。 【参考方案1】:

我对 4.1.2.Final 也有类似的问题。但如果我回滚到 4.1.1.Final,我的程序运行良好。

【讨论】:

所以你是说改变我正在使用的 spring-boot 的版本?我查看了我的 POM,但没有看到 4.1.2。我有以下内容:&lt;project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"&gt; &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt; @Drew1208 您可以为您的案例创建另一个问题并提供完整的 pom 文件吗?

以上是关于Hibernate 4.1.2 HHH000342:无法获得与查询元数据的连接:没有为 jdbc:hsqldb:mem:test 找到合适的驱动程序的主要内容,如果未能解决你的问题,请参考以下文章

hibernate主键生成策略使用UUID报出如下警告:org.hibernate.id.UUIDHexGenerator - HHH000409: Using org.hibernate.id.(代

ERROR org.hibernate.internal.SessionImpl - HHH000346: Error during managed flush [object references

Hibernate里面表间有连接,查询一个表出org.hibernate.HibernateException: HHH000142: Javassist Enhancement failed: 表联

使用JBoss AS 7连接到DB2 db的JPA-Hibernate:HHH000183:没有为查询类找到持久化类

Hibernate 异常提示_1

WARN: HHH000277: Could not bind factory to JNDI