无法创建Session Factory.hibernate.HibernateException:无法解析配置:/hibernate.cfg.xml

时间:2017-06-14 05:04:46

标签: java hibernate hibernate-mapping

我对hibernate不太熟悉。我过去几天都面临着这个问题。我不知道该怎么做。

Stacktrace:

Session Factory could not be created.org.hibernate.HibernateException: Could not parse configuration: /hibernate.cfg.xml
Exception in thread "main" java.lang.ExceptionInInitializerError
    at com.websystique.hibernate.HibernateUtil.<clinit>(HibernateUtil.java:15)
    at com.websystique.hibernate.HibernateStandAloneDemo.saveStudent(HibernateStandAloneDemo.java:67)
    at com.websystique.hibernate.HibernateStandAloneDemo.main(HibernateStandAloneDemo.java:23)
Caused by: 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 org.hibernate.cfg.Configuration.configure(Configuration.java:1411)
    at com.websystique.hibernate.HibernateUtil.<clinit>(HibernateUtil.java:12)
    ... 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:1481)
    ... 5 more

的HibernateUtil

package com.websystique.hibernate;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {

    private static final SessionFactory sessionFactory;

    static{
        try{
            sessionFactory = new Configuration().configure().buildSessionFactory();
        }catch (Throwable ex) {
            System.err.println("Session Factory could not be created." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }

}

HibernateStandAloneDemo

package com.websystique.hibernate;

import java.util.List;

import org.hibernate.Session;

import com.websystique.hibernate.model.Student;

/**
 * Class used to perform CRUD operation on database with Hibernate API's
 *
 */
public class HibernateStandAloneDemo {

    @SuppressWarnings("unused")
    public static void main(String[] args) {

        HibernateStandAloneDemo application = new HibernateStandAloneDemo();

        /*
         * Save few objects with hibernate
         */
        int studentId1 = application.saveStudent("Sam", "Disilva", "Maths");
        int studentId2 = application.saveStudent("Joshua", "Brill", "Science");
        int studentId3 = application.saveStudent("Peter", "Pan", "Physics");
        int studentId4 = application.saveStudent("Bill", "Laurent", "Maths");

        /*
         * Retrieve all saved objects
         */
        List<Student> students = application.getAllStudents();
        System.out.println("List of all persisted students >>>");
        for (Student student : students) {
            System.out.println("Persisted Student :" + student);
        }

        /*
         * Update an object
         */
        application.updateStudent(studentId4, "ARTS");

        /*
         * Deletes an object
         */
        application.deleteStudent(studentId2);

        /*
         * Retrieve all saved objects
         */
        List<Student> remaingStudents = application.getAllStudents();
        System.out.println("List of all remained persisted students >>>");
        for (Student student : remaingStudents) {
            System.out.println("Persisted Student :" + student);
        }

    }

    /**
     * This method saves a Student object in database
     */
    public int saveStudent(String firstName, String lastName, String section) {
        Student student = new Student();
        student.setFirstName(firstName);
        student.setLastName(lastName);
        student.setSection(section);

        Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();

        int id = (Integer) session.save(student);
        session.getTransaction().commit();
        return id;
    }

    /**
     * This method returns list of all persisted Student objects/tuples from
     * database
     */
    public List<Student> getAllStudents() {
        Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();

        @SuppressWarnings("unchecked")
        List<Student> employees = (List<Student>) session.createQuery(
                "FROM Student s ORDER BY s.firstName ASC").list();

        session.getTransaction().commit();
        return employees;
    }

    /**
     * This method updates a specific Student object
     */
    public void updateStudent(int id, String section) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();

        Student student = (Student) session.get(Student.class, id);
        student.setSection(section);
        //session.update(student);//No need to update manually as it will be updated automatically on transaction close.
        session.getTransaction().commit();
    }

    /**
     * This method deletes a specific Student object
     */
    public void deleteStudent(int id) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();

        Student student = (Student) session.get(Student.class, id);
        session.delete(student);
        session.getTransaction().commit();
    }
}

hibernate.hbm.xml的映射

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.OracleDialect</property>
        <property name="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property>
        <property name="hibernate.connection.username">tparaksha</property>
        <property name="hibernate.connection.password">rakshatpa1</property>
        <property name="hibernate.connection.url">jdbc:oracle:thin:@192.168.1.61:1521:orcl</property>
        <property name="show_sql">true</property>
        <property name="format_sql">false</property>
        <mapping resource="com/websystique/hibernate/model/Student.hbm.xml"/>
    </session-factory>
</hibernate-configuration>

Student.hbm.xml的映射 的 Student.hbm.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
 "-//Hibernate/Hibernate Mapping DTD//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
    <class name="com.websystique.hibernate.model.Student" table="STUDENT" schema="saurabh">
        <id name="id" type="int" column="id">
            <generator class="native" />
        </id>
        <property name="firstName" column="FIRST_NAME" type="string" />
        <property name="lastName" column="LAST_NAME" type="string" />
        <property name="section" column="SECTION" type="string" />
    </class>
</hibernate-mapping>

有人可以帮帮我吗?

2 个答案:

答案 0 :(得分:0)

尝试将hibernate.cfg.xml中的DTD更改为如下所示。

<!DOCTYPE hibernate-configuration SYSTEM
    "classpath://org/hibernate/hibernate-configuration-3.0.dtd">

这意味着hibernate将从类路径加载DTD。

答案 1 :(得分:0)

我的hibernate.cfg.xml以:

开头
<?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">

这与您的Student.hbm.xml相同。