2015-11-16 90 views
0

Hibernate無法連接到我的本地數據庫。 我在使用Hibernate 5的不同項目中使用了相同的配置文件,並且它工作正常。但我必須在這個版本中繼續使用Hibernate 3.0.5。Hibernate 3.0.5無法連接數據庫

這個項目是在Eclipse 4.4.1上用Maven 2.2.1和Hibernate 3.0.5設置的。 讀取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

您的hibernate.cfg.xml包含錯誤 –

+0

我一直在尋找他們像瘋了一樣,你能說出錯誤嗎? –

+0

它的一些XML相關的錯誤,檢查缺少<或未封閉的標籤或打開或關閉標籤中的一些奇怪的字符。正如它所說的SAXParsing錯誤意味着看起來像XML解析錯誤。請在這裏發佈hibernate.cfg.xml –

回答

0

像3.0.5這樣的hibernate的早期版本不支持導入.dtd文件。

這個問題很容易通過使用sourceforge鏈接來解決。

對於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文件,你可以在那裏複製它們。