我该如何修复我的/hibernate.cfg.xml文件? [英] How can I fix my /hibernate.cfg.xml file?

查看:192
本文介绍了我该如何修复我的/hibernate.cfg.xml文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图按照本教程进行操作:
https:// www。 youtube.com/playlist?list=PL4AFF701184976B25



我已经看了视频,期待看到hibernate成功添加到我的mysql数据库,但我是反而得到这个错误。

  SLF4J:未能加载类org.slf4j.impl.StaticLoggerBinder 。 
SLF4J:默认为无操作(NOP)记录器实现
SLF4J:有关详细信息,请参阅http://www.slf4j.org/codes.html#StaticLoggerBinder。
线程main中的异常org.hibernate.HibernateException:无法解析配置:/hibernate.cfg.xml
在org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2246)
在org.hibernate.cfg.Configuration.configure(Configuration.java:2158)
在org.hibernate.cfg.Configuration.configure(Configuration.java:2137)
在org.koushik.hibernate .HibernateTest.main(HibernateTest.java:15)
引起:org.dom4j.DocumentException:文档第19行的错误:对于与元素类型name关联的属性{1}, 。嵌套异常:对于与元素类型name关联的属性{1},预计打开引号。
at org.dom4j.io.SAXReader.read(SAXReader.java:482)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2238)
... 3更多






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

< hibernate-configuration>
< session-factory>
<! - 数据库连接设置 - >
< 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>建立< / property>
< mapping class =org.koushik.javabrains.dto.UserDetails/>
< / session-factory>
< / hibernate-configuration>

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



另外,如果有人可以指出,我会非常感激我朝着一些为Hibernate 4.3.7工作的Hibernate教程。到目前为止,我发现的所有内容都是混合了大量过期的材料,需要安装过去的hibernate版本才能学习。

编辑------ -------------



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



编辑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;


$ / code>

HibernateTest.java

  package org.koushik.hibernate; 

import org.hibernate.Session;
import org.hibernate.SessionFactory;
导入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();
}

}


解决方案

试试这个:

 <?xml version =1.0encoding =utf-8?> 
<!DOCTYPE hibernate-configuration PUBLIC
- // Hibernate / Hibernate配置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>根< / 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>建立< / 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>


I'm attempting to follow this tutorial: https://www.youtube.com/playlist?list=PL4AFF701184976B25

I've gotten to video five expecting to see hibernate successfully adding to my mysql database, but I'm instead getting this error. Below that is my hibernate.cfg.xml file.

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>

What could the problem be and how can I go about fixing it? I can't tell if it can't find my file, or if something is syntactically wrong within it.

Also, I'd be very grateful if someone could point me towards some working Hibernate tutorials for Hibernate 4.3.7. All I've found so far is a mixture of broken/heavily out dated materials that require installing past versions of hibernate to learn from.

Edit-------------------

I attempted to validate in Eclipse and this error appears at line 8 saying "The document type declaration for root element type "hibernate-configuration" must end with '>'." Also, this line is the only line where the "connection..." doesn't appear in blue, and the name part of property name doesn't appear in purple. Feels like a syntax error but I can't seem to find it.

Edit 2------------------ Here are my two files

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();
    }

}

解决方案

Try this:

<?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>

这篇关于我该如何修复我的/hibernate.cfg.xml文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆