Topic: hibernate 入门问题 大哥们帮忙啊

  Print this page

1.hibernate 入门问题 大哥们帮忙啊 Copy to clipboard
Posted by: 198255nihao
Posted on: 2007-05-11 10:18

我怎么总是出现
org.hibernate.HibernateException: Could not parse configuration: /hibernate.cfg.xml
.
.
.
Caused by: org.dom4j.DocumentException: Error on line 20 of document : Content is not allowed in trailing section. Nested Exception: Content is not allowed in trailing section

的异常啊

hibernate.cfg.xml 如下

<?xml version="1.0" encoding="GB2312"?>
<!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="show_sql">true</property>
    <property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
    <property name="connection.driver_class">com.microsoft.jdbc.sqlserver.SQLServerDriver</property>
    <property name="connection.url">jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=TEACHER</property>
    <property name="connection.username">sa</property>
    <property name="connection.password"/>
    <mapping resource="teach/Students.hbm.xml"/>
  </session-factory>
</hibernate-configuration>

Students.hbm.xml 如下

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

<hibernate-mapping>
<class name="teach.Students" table="STUDENTS">
<property name="students_id" column="STUDENTS_ID" type="string" />
<property name="students_name" column="STUDENTS_NAME" type="string" />
<property name="students_class" column="STUDENTS_CLASS" type="string" />
</class>
</hibernate-mapping>

我用得是 hibernate 3

Students.java 如下

package teach;

import java.io.Serializable;
/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright Coffee 2007</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class Students implements Serializable{

private String students_class;
private String students_id;
private String students_name;
public Students() {
}
public String getStudents_class() {
return students_class;
}

public void setStudents_class(String students_class) {
this.students_class = students_class;
}

public void setStudents_name(String students_name) {
this.students_name = students_name;
}

public void setStudents_id(String students_id) {
this.students_id = students_id;
}

public String getStudents_id() {
return students_id;
}

public String getStudents_name() {
return students_name;
}

}

BusinessService.java 如下

package teach;

/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright Coffee 2007</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/

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

public class BusinessService {
public static SessionFactory sessionFactory;
static {
try {
Configuration config = new Configuration().configure();
sessionFactory = config.buildSessionFactory();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String args[]) {
Session session = sessionFactory.openSession();
Transaction tx = null;
Students s = new Students();
s.setStudents_id("001");
s.setStudents_name("王一");
s.setStudents_class("一班");
try {
tx = session.beginTransaction();
session.save( s );
tx.commit();
} catch (Exception e) {
if (tx != null) {
// Something went wrong; discard all partial changes
tx.rollback();
}
throw e;
} finally {
// No matter what, close the session
session.close();
}
}
}

2.Re:hibernate 入门问题 大哥们帮忙啊 [Re: 198255nihao] Copy to clipboard
Posted by: JiafanZhou
Posted on: 2007-05-11 17:00

The problem for you here is quite clear: i.e.
Could not parse configuration: /hibernate.cfg.xml

Check the following things:
1. hibernate.cfg.xml must be in your project classpath.
2. have you included all the jars in the hibernate lib directory?
3. sometimes the order of the jars in the classpath matters.
4. turn on log4j for Hibernate to see what is really happening.

Regards,
Jiafan

3.Re:hibernate 入门问题 大哥们帮忙啊 [Re: 198255nihao] Copy to clipboard
Posted by: YuLimin
Posted on: 2007-05-15 20:07

xml配置文件用XMLSpy之类的工具打开编辑检查一下看是否有错!

另外你的encoding="GB2312",是否改为UTF-8

4.Re:hibernate 入门问题 大哥们帮忙啊 [Re: 198255nihao] Copy to clipboard
Posted by: 198255nihao
Posted on: 2007-05-16 19:52

谢谢大哥们.已经搞定

5.Re:hibernate 入门问题 大哥们帮忙啊 [Re: 198255nihao] Copy to clipboard
Posted by: chengbd
Posted on: 2007-05-22 13:12

“搞定”,你就说明一下如何“搞定”的,所以,先扣您3分。


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923