为什么我得到无法解析配置:/hibernate.cfg.xml错误

时间:2016-10-13 10:04:33

标签: 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">


<hibernate-configuration>  

<session-factory>  
 <property name="hbm2ddl.auto">create</property>  
 <property name="dialect">org.hibernate.dialect.SQLServerDialect</property>  
 <property name="connection.url">jdbc:sqlserver://localhost;databaseName=MyDB</property>  
 <property name="connection.username">sa</property>  
 <property name="connection.password">password</property>  
 <property name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
 <property name="show_sql">true</property>    
 <mapping resource="user.hbm.xml"></mapping>  
 </session-factory>  

</hibernate-configuration>  

我没有看到文件有什么问题

user.hbm.xml

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

    <hibernate-mapping>
    <class name="com.raj.User" table="UserData">
    <id name="id" type="int" column="id">
    <generator class="assigned"></generator>
    </id>
    <property name="email" column="email"></property>
    <property name="login" column="login"></property>
    <property name="password" column="password"></property>
    </class>

    </hibernate-mapping>

代码出了什么问题,有人请检查并帮我解决这个问题,提前谢谢

错误

org.apache.jasper.JasperException: An exception occurred processing JSP page /register.jsp at line 5

2: <jsp:useBean id="obj" class="com.rajshree.User"></jsp:useBean>
3: <jsp:setProperty property="*" name="obj"/>
4: 
5: <% int i=UserDao.register(obj);
6: if(i>0)out.println("You are successfully registered");%>


Stacktrace:
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:521)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:430)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:723)


root cause 

org.hibernate.HibernateException: Could not parse configuration: /hibernate.cfg.xml
    org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1491)
    org.hibernate.cfg.Configuration.configure(Configuration.java:1425)
    org.hibernate.cfg.Configuration.configure(Configuration.java:1411)
    com.rajshree.UserDao.register(UserDao.java:11)
    org.apache.jsp.register_jsp._jspService(register_jsp.java:69)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:388)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:723)


root cause 

org.dom4j.DocumentException: Connection reset Nested exception: Connection reset
    org.dom4j.io.SAXReader.read(SAXReader.java:484)
    org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1481)
    org.hibernate.cfg.Configuration.configure(Configuration.java:1425)
    org.hibernate.cfg.Configuration.configure(Configuration.java:1411)
    com.rajshree.UserDao.register(UserDao.java:11)
    org.apache.jsp.register_jsp._jspService(register_jsp.java:69)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:388)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:723)

我在运行代码时得到的堆栈跟踪

1 个答案:

答案 0 :(得分:0)

1) 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">

<hibernate-configuration>  

<session-factory>  
 <property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property> 
 <property name="hibernate.hbm2ddl.auto">update</property>   
 <property name="hibernate.connection.url">jdbc:sqlserver://localhost;databaseName=MyDB</property>  
 <property name="hibernate.connection.username">sa</property>  
 <property name="hibernate.connection.password">password</property>  
 <property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
 <property name="show_sql">true</property>    
 <mapping resource="user.hbm.xml"></mapping>  
 </session-factory>  

</hibernate-configuration>  

注意:需要互联网连接

2)使用以下 HibernateUtil 获取SessionFactory对象:

import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

public class HibernateUtil {
private static SessionFactory sessionAnnotationFactory;

public static SessionFactory getSessionAnnotationFactory() {
    if (sessionAnnotationFactory == null) {
        sessionAnnotationFactory = buildAnnotationSessionFactory();
    }
    return sessionAnnotationFactory;
}

private static SessionFactory buildAnnotationSessionFactory() {
    SessionFactory sessionFactory = null;
    try {

        sessionFactory = new AnnotationConfiguration().configure("hibernate.cfg.xml").buildSessionFactory();
    } catch (HibernateException e) {
        e.printStackTrace();
    }
    return sessionFactory;
}

}

并获取SessionFactory&amp;会话对象使用以下代码:

SessionFactory sessionFactory = HibernateUtil.getSessionAnnotationFactory();
session = sessionFactory.openSession();
相关问题