【问题标题】:Hibernate Configuration Loading Error休眠配置加载错误
【发布时间】:2014-09-24 16:44:19
【问题描述】:

我正在尝试创建一个简单的 hibernate 项目,但是在读取 hibernate.cfg.xml 文件时出现错误,不知道是什么问题,下面是代码和错误跟踪。请帮忙。

这是我的 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">

<hibernate-configuration>

   <session-factory>
        <!-- Database connection settings -->
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/EPS</property>
        <property name="hibernate.connection.username">haris</property>
        <property name="hibernate.connection.password">ihatecookies</property>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="hibernate.connection.pool_size">1</property>

        <!-- SQL dialect -->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>

        <!-- Echo all executed SQL to stdout -->
        <property name="hibernate.show_sql">true</property>

        <!-- Drop or re-create database -->
        <property name="hibernate.hbm2ddl.auto">update</property>

        <!-- Mapping files -->
        <mapping class="com.secure.eps.model.User"/>        

    </session-factory> 
</hibernate-configuration>

这是我的主要课程:

package com.secure.eps.controller;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;

import com.secure.eps.model.User;

public class HibernateUtil {

    public static void main(String args[]){

        User user = new User();
        user.setUsername("RoojiPooji");
        user.setPassword("ihatecookies");

        Configuration configuration = new Configuration().configure("hibernate.cfg.xml");
        StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
        SessionFactory sessionFactory = configuration.configure().buildSessionFactory(serviceRegistry);

        Session session = sessionFactory.openSession();
        session.beginTransaction();
        session.save(user);
        session.getTransaction().commit();
        session.close();

    }

}

这是错误堆栈:

Sep 24, 2014 9:37:41 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.5.Final}
Sep 24, 2014 9:37:41 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.3.6.Final}
Sep 24, 2014 9:37:41 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000205: Loaded properties from resource hibernate.properties: {hibernate.connection.driver_class=org.h2.Driver, hibernate.service.allow_crawling=false, hibernate.dialect=org.hibernate.dialect.H2Dialect, hibernate.max_fetch_depth=5, hibernate.format_sql=true, hibernate.generate_statistics=true, hibernate.connection.username=sa, hibernate.connection.url=jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1;MVCC=TRUE, hibernate.bytecode.use_reflection_optimizer=false, hibernate.jdbc.batch_versioned_data=true, hibernate.connection.pool_size=5}
Sep 24, 2014 9:37:41 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Sep 24, 2014 9:37:41 PM org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: hibernate.cfg.xml
Sep 24, 2014 9:37:41 PM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: hibernate.cfg.xml
Exception in thread "main" org.hibernate.HibernateException: Could not parse configuration: hibernate.cfg.xml
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2163)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:2075)
    at com.secure.eps.controller.HibernateUtil.main(HibernateUtil.java:19)
Caused by: org.dom4j.DocumentException: null Nested exception: null
    at org.dom4j.io.SAXReader.read(SAXReader.java:484)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2155)
    ... 2 more

【问题讨论】:

    标签: java xml hibernate


    【解决方案1】:

    我认为这是您的 xml 配置中的这一行的问题。

        <!-- Mapping files -->
        <mapping class="com.secure.eps.model.User"/>
    

    请参阅休眠文档:http://www.tutorialspoint.com/hibernate/hibernate_mapping_files.htm

    【讨论】:

    • 不,不是这样,你也可以在休眠配置文件中映射一个实体。
    • 尝试删除您传递给 Configuration.configure() 方法的字符串。默认情况下,它应该寻找应用程序资源“hibernate.cfg.xml”。告诉我进展如何!
    • 奇怪,我复制了它,对我来说效果很好。也许这是一个依赖问题。你的类路径中有哪些 JARS+版本?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-11
    • 2015-07-19
    • 2011-04-30
    • 2010-11-08
    • 2015-03-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多