【问题标题】:How can I fix my /hibernate.cfg.xml file?如何修复我的 /hibernate.cfg.xml 文件?
【发布时间】:2015-01-07 08:54:54
【问题描述】:

我正在尝试学习本教程: https://www.youtube.com/playlist?list=PL4AFF701184976B25

我已经观看了视频五,希望看到 hibernate 成功添加到我的 mysql 数据库中,但我却收到了这个错误。下面是我的 hibernate.cfg.xml 文件。

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Exception in thread "main" org.hibernate.HibernateException: Could not parse configuration: /hibernate.cfg.xml
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2246)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:2158)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:2137)
    at org.koushik.hibernate.HibernateTest.main(HibernateTest.java:15)
Caused by: org.dom4j.DocumentException: Error on line 19 of document  : Open quote is expected for attribute "{1}" associated with an  element type  "name". Nested exception: Open quote is expected for attribute "{1}" associated with an  element type  "name".
    at org.dom4j.io.SAXReader.read(SAXReader.java:482)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2238)
    ... 3 more

<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
   <session-factory>
   <!-- Database connection settings -->
   <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
   <property name="connection.url">jdbc:mysql://localhost/hibernatedb</property>
   <property name="connection.username">root</property>
   <property name="connection.password">mysqlroot</property>

   <property name="connection.pool_size">1</property>

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

   <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

   <property name=show_sql>true</property>
   <property name="hbm2ddl.auto">create</property>
   <mapping class="org.koushik.javabrains.dto.UserDetails" />
</session-factory>
</hibernate-configuration>

可能是什么问题,我该如何解决?我不知道它是否找不到我的文件,或者其中是否存在语法错误。

另外,如果有人能指出一些适用于 Hibernate 4.3.7 的 Hibernate 教程,我将不胜感激。到目前为止,我发现的只是一些损坏/严重过时的材料,需要安装过去版本的 hibernate 才能从中学习。

编辑-------

我试图在 Eclipse 中进行验证,这个错误出现在第 8 行,说 “根元素类型“休眠配置”的文档类型声明必须以“>”结尾。”此外,这一行是唯一没有以蓝色显示“连接...”的行,并且属性名称的名称部分没有以紫色显示。感觉像是语法错误,但我似乎找不到它。

编辑 2----------------- 这是我的两个文件

UserDetails.java

package org.koushik.javabrains.dto;

import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class UserDetails {
    @Id
    private int userId;
    private String userName;

    public int getUserId() {
        return userId;
    }
    public void setUserId(int userId) {
        this.userId = userId;
    }
    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
}

HibernateTest.java

package org.koushik.hibernate;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.koushik.javabrains.dto.UserDetails;

public class HibernateTest {

    public static void main(String[] args) {
        UserDetails user = new UserDetails();
        user.setUserId(1);
        user.setUserName("First User");

        SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
        Session session = sessionFactory.openSession();
        session.beginTransaction();
        session.save(user);
        session.getTransaction().commit();
    }

}

【问题讨论】:

    标签: java mysql hibernate orm hibernate.cfg.xml


    【解决方案1】:

    试试这个:

    <?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">
    
    <hibernate-configuration>
       <session-factory>
           <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
           <property name="hibernate.connection.url">jdbc:mysql://localhost/hibernatedb</property>
           <property name="hibernate.connection.username">root</property>
           <property name="hibernate.connection.password">mysqlroot</property>
           <property name="hibernate.connection.pool_size">1</property>
           <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
           <property name="hibernate.hbm2ddl.auto">create</property>
           <property name="hibernate.cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
           <property name="show_sql">true</property>
           <mapping class="org.koushik.javabrains.dto.UserDetails" />
        </session-factory>
    </hibernate-configuration>
    

    【讨论】:

    • 得到相同/相似的错误。文档异常:文档第 9 行出错:根元素类型“hibernate-configuration”的文档类型声明必须以“>”结尾。
    • 请确保您的编辑器不会在每行的末尾放置任何不可见的奇怪字符。
    • 如果你使用maven,你能发布你的POM文件吗?
    • 我没有使用 Maven atm。在我的帖子底部添加了更多详细信息,不确定是否有帮助。
    • 你试过把休眠。来自你的connection.driver_class?
    猜你喜欢
    • 1970-01-01
    • 2012-06-06
    • 2021-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-17
    相关资源
    最近更新 更多