无法解析配置:hibernate.cfg.xml

时间:2013-02-19 06:17:20

标签: hibernate

其实我正在尝试进行hibernate日志记录。这是我的hibernate.cfg.xml文件

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>

    <session-factory>
        <property name="hbm2ddl.auto">update</property>
        <property name="show_sql">true</property>
        <property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property>
        <property name="connection.url">jdbc:oracle//localhost/5432/newdatabase</property>
        <property name="connection.username">postgres</property>
        <property name="connection.password">P@ssword</property>
        <property name="connection.driver_class">org.postgresql.Driver</property>

    <mapping resource="employee.hbm.xml"/>

    </session-factory>

</hibernate-configuration>

这是我的主要课程

package com.javatpoint;
import org.hibernate.*;
import org.hibernate.cfg.*;

public class StoreData {
public static void main(String[] args) {

    Configuration cfg=new Configuration();
    cfg.configure("hibernate.cfg.xml");

    SessionFactory factory=cfg.buildSessionFactory();
    Session session=factory.openSession();
    Transaction tx=session.beginTransaction();

    session.save(new Employee("Arun",3800));
    session.save(new Employee("Varun",4800));

    tx.commit();
    session.close();
    System.out.println("record successfully persisted");

}
}

当我运行此操作时,我收到以下错误。

Exception in thread "main" org.hibernate.HibernateException: Could not parse configuration: hibernate.cfg.xml
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1491)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1425)
    at com.javatpoint.StoreData.main(StoreData.java:9)
Caused by: org.dom4j.DocumentException: hibernate.sourceforge.net Nested exception: hibernate.sourceforge.net
    at org.dom4j.io.SAXReader.read(SAXReader.java:484)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1481)
    ... 2 more

经过大量谷歌搜索后,我发现它与DOCTYPE有关。但即使这样,我也无法找到完美的解决方案。有人能帮助我吗?感谢。

2 个答案:

答案 0 :(得分:1)

你没想到你的

 <property name="connection.url">jdbc:oracle//localhost/5432/newdatabase</property>

必须如下:

 <property name="connection.url">jdbc:oracle//localhost:5432/newdatabase</property>

答案 1 :(得分:0)

你试过

吗?
 <property name="hibernate.hbm2ddl.auto">update</property>
        <property name="hibernate.show_sql">true</property>
        <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
        <property name="hibernate.connection.url">jdbc:oracle//localhost/5432/newdatabase</property>
        <property name="hibernate.connection.username">postgres</property>
        <property name="hibernate.connection.password">P@ssword</property>
        <property name="hibernate.connection.driver_class">org.postgresql.Driver</property>