最新的Hibernate和Derby:无法建立JDBC连接 [英] Latest Hibernate and Derby: Unable to make JDBC Connection
问题描述
我试图创建一个使用Hibernate连接到Derby数据库的准系统项目。我使用的是最新版本的Hibernate和Derby,但是我得到了一个通用的无法建立JDBC连接
的错误。
这是我的 pom.xml
文件:
< project xmlns =http://maven.apache.org/POM/4.0.0xmlns: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\">
< modelVersion> 4.0.0< / modelVersion>
< groupId> StaticVoidGames< / groupId>
< artifactId> DatabaseTest< / artifactId>
< version> 0.0.1-SNAPSHOT< / version>
< name> DatabaseTest< / name>
<属性>
< maven.compiler.source> 1.8< /maven.compiler.source>
< maven.compiler.target> 1.8< /maven.compiler.target>
< / properties>
<依赖关系>
< dependency>
< groupId> org.hibernate< / groupId>
< artifactId> hibernate-core< / artifactId>
< version> 5.0.6.Final< / version>
< /依赖关系>
< dependency>
< groupId> org.apache.derby< / groupId>
< artifactId> derby< / artifactId>
< version> 10.12.1.1< / version>
< /依赖关系>
< dependency>
< groupId> org.apache.derby< / groupId>
< artifactId> derbyclient< / artifactId>
< version> 10.12.1.1< / version>
< /依赖关系>
< dependency>
< groupId> javax.transaction< / groupId>
< artifactId> jta< / artifactId>
< version> 1.1< / version>
< /依赖关系>
< /依赖关系>
< / project>
这是我的 hibernate-derby-cfg.xml
file:
<?xml version ='1.0'encoding ='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
- // Hibernate / Hibernate配置DTD 3.0 // EN
http://www.hibernate.org/dtd/hibernate-configuration -3.0.dtd>
< hibernate-configuration>
< session-factory>
< property name =connection.driver_class> org.apache.derby.jdbc.ClientDriver< / property>
< property name =connection.url> jdbc:derby:TestDerbyDB; create = true< / property>
< property name =connection.username>< / property>
< property name =connection.password>< / property>
<! - - SQL方言 - >
< property name =dialect> org.hibernate.dialect.DerbyDialect< / property>
<! - 禁用二级缓存 - >
< property name =cache.provider_class> org.hibernate.cache.internal.NoCacheProvider< / property>
<! - 将所有执行的SQL回复到stdout - >
< property name =show_sql> true< / property>
< property name =hbm2ddl.auto>更新< / property>
< / session-factory>
< / hibernate-configuration>
以下是我的barbones代码:
import org.hibernate.Session;
import org.hibernate.SessionFactory;
导入org.hibernate.cfg.Configuration;
public class DatabaseTestMain {
public static void main(String ... args){
Configuration derbyConfiguration = new Configuration()。configure(hibernate-derby.cfg.xml );
SessionFactory derbySF = derbyConfiguration.buildSessionFactory();
Session derbySession = derbySF.openSession();
derbySession.close();
当我运行它时,我得到这个堆栈跟踪:
线程main中的异常org.hibernate.service.spi.ServiceException:无法创建请求的服务[org.hibernate。 engine.jdbc.env.spi.JdbcEnvironment]
at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:244)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService( AbstractServiceRegistryImpl.java:208)
位于org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:189))
(JdbcServicesImpl。 java:51)
at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:94)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java :217)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:189)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.handleTypes(MetadataBuildingProcess.java :352)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:111)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.build (MetadataBuildingImpl.java:83)
位于org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:418)
位于org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java :87)
在org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:692)
在org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:724)
at DatabaseTestMain.main(DatabaseTestMain.java:9)
导致:org.hibernate.HibernateException:无法建立JDBC连接[jdbc:derby:TestDerb yDB; create = true]
at org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator.createConnection(BasicConnectionCreator.java:60)
at org.hibernate.engine.jdbc.connections.internal。 PooledConnections.addConnections(PooledConnections.java:106)
在org.hibernate.engine.jdbc.connections.internal.PooledConnections。< init>(PooledConnections.java:40)
在org.hibernate.engine .jdbc.connections.internal.PooledConnections。< init>(PooledConnections.java:19)
at org.hibernate.engine.jdbc.connections.internal.PooledConnections $ Builder.build(PooledConnections.java:138)
at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.buildPool(DriverManagerConnectionProviderImpl.java:110)
at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.configure(DriverManagerConnectionProviderImpl.java :74)
在org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.c onfigureService(StandardServiceRegistryImpl.java:94)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:217)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl。 java:189)
位于org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.buildJdbcConnectionAccess(JdbcEnvironmentInitiator.java:145)
位于org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator。启动服务(JdbcEnvironmentInitiator.java:66)
位于org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:35)
位于org.hibernate.boot.registry.internal。 StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:88)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:234)
我试过改变我的
< property name =dialect> org.hibernate.dialect.DerbyTenSevenDialect< /性>
我试着改变我的 pom.xml
使用Derby 10.7:
< dependency>
< groupId> org.apache.derby< / groupId>
< artifactId> derby< / artifactId>
< version> 10.7.1.1< / version>
< /依赖关系>
< dependency>
< groupId> org.apache.derby< / groupId>
< artifactId> derbyclient< / artifactId>
< version> 10.7.1.1< / version>
< /依赖关系>
但无论我做什么,我仍然会遇到上述错误。我错过了什么?
我问过这个问题关于从Hibernate 4.0.1升级到Hibernate 4.3.5的同样的问题。关于我的类路径来自哪里,升级到新版本等问题有几个问题,所以我尝试着用最新版本的全新项目重新开始,但是我仍然遇到了错误。
编辑:我应该注意,所有这些设置在Hibernate 4.0.1中都能正常工作,并且Derby数据库不需要用户名或密码。 / p>
您正在使用 ClientDriver
连接到本地数据库。最近的德比文档(撰写本文时)表示,
加载Derby本地JDBC驱动程序的类是类
org.apache.derby.jdbc.EmbeddedDriver
。加载Derby的
网络客户端驱动程序的类是类org.apache.derby.jdbc.ClientDriver
。
请参阅 https:// db.apache.org/derby/docs/10.9/ref/rrefjdbc32052.html
所以为了让你的代码运行,我建议你 EmbeddedDriver
如果你不需要太多的改变。如果你仍然需要使用 ClientDriver
,那么你必须 运行网络服务实例 将连接URL更改为类似于 jdbc:derby:// localhost:1527 / MyDbTest ; create = true
并且你必须同时提供用户名和密码 ClientDriver
。
I'm trying to create a barebones project that uses Hibernate to connect to a Derby database. I'm using the latest versions of both Hibernate and Derby, but I get a generic Unable to make JDBC Connection
error.
Here is my pom.xml
file:
<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">
<modelVersion>4.0.0</modelVersion>
<groupId>StaticVoidGames</groupId>
<artifactId>DatabaseTest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>DatabaseTest</name>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.0.6.Final</version>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<version>10.12.1.1</version>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derbyclient</artifactId>
<version>10.12.1.1</version>
</dependency>
<dependency>
<groupId>javax.transaction</groupId>
<artifactId>jta</artifactId>
<version>1.1</version>
</dependency>
</dependencies>
</project>
Here is my hibernate-derby-cfg.xml
file:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">org.apache.derby.jdbc.ClientDriver</property>
<property name="connection.url">jdbc:derby:TestDerbyDB;create=true</property>
<property name="connection.username"></property>
<property name="connection.password"></property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.DerbyDialect</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</property>
</session-factory>
</hibernate-configuration>
And here is my barbones code:
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class DatabaseTestMain {
public static void main(String... args){
Configuration derbyConfiguration = new Configuration().configure("hibernate-derby.cfg.xml");
SessionFactory derbySF = derbyConfiguration.buildSessionFactory();
Session derbySession = derbySF.openSession();
derbySession.close();
}
}
When I run that, I get this stack trace:
Exception in thread "main" org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:244)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:208)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:189)
at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:51)
at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:94)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:217)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:189)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.handleTypes(MetadataBuildingProcess.java:352)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:111)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.build(MetadataBuildingProcess.java:83)
at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:418)
at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:87)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:692)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:724)
at DatabaseTestMain.main(DatabaseTestMain.java:9)
Caused by: org.hibernate.HibernateException: Unable to make JDBC Connection [jdbc:derby:TestDerbyDB;create=true]
at org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator.createConnection(BasicConnectionCreator.java:60)
at org.hibernate.engine.jdbc.connections.internal.PooledConnections.addConnections(PooledConnections.java:106)
at org.hibernate.engine.jdbc.connections.internal.PooledConnections.<init>(PooledConnections.java:40)
at org.hibernate.engine.jdbc.connections.internal.PooledConnections.<init>(PooledConnections.java:19)
at org.hibernate.engine.jdbc.connections.internal.PooledConnections$Builder.build(PooledConnections.java:138)
at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.buildPool(DriverManagerConnectionProviderImpl.java:110)
at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.configure(DriverManagerConnectionProviderImpl.java:74)
at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:94)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:217)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:189)
at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.buildJdbcConnectionAccess(JdbcEnvironmentInitiator.java:145)
at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:66)
at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:35)
at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:88)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:234)
I have tried changing my sql dialect to use Derby 10.7:
<property name="dialect">org.hibernate.dialect.DerbyTenSevenDialect</property>
And I have tried changing my pom.xml
to use Derby 10.7:
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<version>10.7.1.1</version>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derbyclient</artifactId>
<version>10.7.1.1</version>
</dependency>
But no matter what I do, I still get the above error. Am I missing something?
I asked this question about this same problem while upgrading from Hibernate 4.0.1 to Hibernate 4.3.5. There were a few questions about where my classpath was coming from, upgrading to a newer version, etc. So I tried starting over with a completely new project with the newest versions of everything, but I'm still getting the error.
Edit: I should note that all of these settings worked fine in Hibernate 4.0.1, and Derby databases don't require a username or password.
You are using ClientDriver
to connect to a local database. The recent derby documentation (at the time of writing) says that
The class that loads Derby's local JDBC driver is the class
org.apache.derby.jdbc.EmbeddedDriver
. The class that loads Derby's network client driver is the classorg.apache.derby.jdbc.ClientDriver
.
Please refer https://db.apache.org/derby/docs/10.9/ref/rrefjdbc32052.html
So in order for your code to run, I would suggest you to EmbeddedDriver
if you don't want much of the changes. Incase if you still have to use ClientDriver
, then you will have to run the network service instance change the connection URL to something like jdbc:derby://localhost:1527/MyDbTest;create=true
and also you must have to provide both username and password for ClientDriver
.
这篇关于最新的Hibernate和Derby:无法建立JDBC连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!