Hibernate无法连接到我的本地数据库。 我在使用Hibernate 5的不同项目中使用了相同的配置文件,并且它工作正常。但是我必须继续在这个中使用Hibernate 3.0.5。
在Eclipse 4.4.1上使用Maven 2.2.1和Hibernate 3.0.5设置了projekt。 它在读取hibernate.cfg.xml并尝试连接到数据库时失败。到目前为止,代码只尝试构建会话工厂,这就是为什么我还没有任何映射文件。
的pom.xml:
<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>com.materna.azubi.pi</groupId>
<artifactId>Parkplatzverwaltung</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Parkplatzverwaltung</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<PostgreSQLVersion>9.3-1103-jdbc41</PostgreSQLVersion>
<HibernateVersion>3.0.5</HibernateVersion>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>${PostgreSQLVersion}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate</artifactId>
<version>${HibernateVersion}</version>
</dependency>
</dependencies>
</project>
主要课程:
import org.hibernate.SessionFactory;
public class Testclass {
public static void main(String args[]) {
SessionFactory factory = Connect.getSessionFactory();
}
}
hibernate.cfg.xml中:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">
org.hibernate.dialect.PostgreSQLDialect
</property>
<property name="hibernate.connection.driver_class">
org.postgresql.Driver
</property>
<property name="hibernate.connection.url">
jdbc:postgresql://localhost/postgres
</property>
<property name="hibernate.connection.username">
azubi
</property>
<property name="hibernate.connection.password">
azubi
</property>
<!-- List of XML mapping files -->
<mapping resource="ProviderImpl.hbm.xml"/>
</session-factory>
</hibernate-configuration>
堆栈追踪:
Nov 16, 2015 1:52:03 PM org.hibernate.cfg.Environment <clinit>
INFORMATION: Hibernate 3.0.5
Nov 16, 2015 1:52:03 PM org.hibernate.cfg.Environment <clinit>
INFORMATION: hibernate.properties not found
Nov 16, 2015 1:52:03 PM org.hibernate.cfg.Environment <clinit>
INFORMATION: using CGLIB reflection optimizer
Nov 16, 2015 1:52:03 PM org.hibernate.cfg.Environment <clinit>
INFORMATION: using JDK 1.4 java.sql.Timestamp handling
Nov 16, 2015 1:52:03 PM org.hibernate.cfg.Configuration configure
INFORMATION: configuring from resource: /hibernate.cfg.xml
Nov 16, 2015 1:52:03 PM org.hibernate.cfg.Configuration getConfigurationInputStream
INFORMATION: Configuration resource: /hibernate.cfg.xml
Nov 16, 2015 1:52:04 PM org.hibernate.cfg.Configuration doConfigure
SCHWERWIEGEND: problem parsing configuration/hibernate.cfg.xml
org.dom4j.DocumentException: Connection refused: connect Nested exception: Connection refused: connect
at org.dom4j.io.SAXReader.read(SAXReader.java:484)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1168)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1112)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1098)
at mypackage.Connect.buildSessionFactory(Connect.java:10)
at mypackage.Connect.<clinit>(Connect.java:7)
at mypackage.impl.Testclass.main(Testclass.java:16)
Nested exception:
java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:345)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at java.net.Socket.connect(Socket.java:538)
at sun.net.NetworkClient.doConnect(NetworkClient.java:180)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:432)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:527)
at sun.net.www.http.HttpClient.<init>(HttpClient.java:211)
at sun.net.www.http.HttpClient.New(HttpClient.java:308)
at sun.net.www.http.HttpClient.New(HttpClient.java:326)
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:1168)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1104)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:998)
at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:932)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1512)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1440)
at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.setupCurrentEntity(XMLEntityManager.java:646)
at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.startEntity(XMLEntityManager.java:1300)
at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.startDTDEntity(XMLEntityManager.java:1267)
at com.sun.org.apache.xerces.internal.impl.XMLDTDScannerImpl.setInputSource(XMLDTDScannerImpl.java:263)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$DTDDriver.dispatch(XMLDocumentScannerImpl.java:1164)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$DTDDriver.next(XMLDocumentScannerImpl.java:1050)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:964)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:606)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:117)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:510)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:848)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:777)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1213)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:649)
at org.dom4j.io.SAXReader.read(SAXReader.java:465)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1168)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1112)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1098)
at mypackage.Connect.buildSessionFactory(Connect.java:10)
at mypackage.Connect.<clinit>(Connect.java:7)
at mypackage.impl.Testclass.main(Testclass.java:16)
SessionFactory creation failed.org.hibernate.HibernateException: problem parsing configuration/hibernate.cfg.xml
Exception in thread "main" java.lang.ExceptionInInitializerError
at mypackage.Connect.buildSessionFactory(Connect.java:13)
at mypackage.Connect.<clinit>(Connect.java:7)
at mypackage.impl.Testclass.main(Testclass.java:16)
Caused by: org.hibernate.HibernateException: problem parsing configuration/hibernate.cfg.xml
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1173)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1112)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1098)
at mypackage.Connect.buildSessionFactory(Connect.java:10)
... 2 more
Caused by: org.dom4j.DocumentException: Connection refused: connect Nested exception: Connection refused: connect
at org.dom4j.io.SAXReader.read(SAXReader.java:484)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1168)
... 5 more
到目前为止,我已经尝试在cfg.xml中找到任何错误,为什么它可能无法与maven一起使用,以及Hibernate 5和Hibernate 3之间是否存在一些我忽略的差异。 我曾经有过
<property name="hibernate.dialect">
而不是
<property name="dialect">
但这并没有改变任何事情。
异常的根在Testclass第16行:
SessionFactory factory = Connect.getSessionFactory();
被调用的类看起来像这样:
public class Connect {
private static final SessionFactory sessionFactory = buildSessionFactory();
private static SessionFactory buildSessionFactory() {
try{
return new Configuration().configure().buildSessionFactory();
}catch (Throwable ex) {
System.err.println("SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
public static void shutdown() {
getSessionFactory().close();
}
}
我现在知道,我的程序可以访问互联网,而且dtd可以通过互联网访问。 我希望你能帮忙!
答案 0 :(得分:0)
像3.0.5这样的早期版本的hibernate不支持很好地导入.dtd文件。
使用sourceforge liks可以很容易地解决这个问题。
对于hibernate.cfg.xml,请使用:
<!DOCTYPE hibernate-configuration SYSTEM
"hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
对于任何class.hbm.xml文件,请使用:
<!DOCTYPE hibernate-mapping SYSTEM
"hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
如果仍然出现错误,hibernate会告诉你它在哪里查找.dtd文件,你可以将它们复制到那里。