我怎么总是出现
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 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 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();
}
}
}