初学hibernate编程,我的第一个例程,自己不会调试,请高手指点一下
/**
*
* test.java文件内容*/
package ch03.hibernate;
/**
* @author ROOT
*
*/
import org.hibernate.*;
import org.hibernate.cfg.*;
//测试类
public class Test {
public static void main(String[] args){
try {
//常见Session工厂
SessionFactory sf = new Configuration().configure().buildSessionFactory();
//获取Session实例
Session session = sf.openSession();
//开始事务
Transaction tx = session.beginTransaction();
//创建一个User对象
User user = new User();
//为对象赋值
user.setUsername("Hibernate");
user.setPassword("123");
//调用save()方法保存user实例到数据库
session.save(user);
//提交事务
tx.commit();
//关闭Session
session.close();
}catch (HibernateException e){
e.printStackTrace();
}
}
}
//User.java
package ch03.hibernate;
public class User {
//定义用户ID
private int id;
//定义用户名
private String username;
//定义用户密码
private String password;
//定义用户邮件地址
private String email;
/**
* @return email
*/
public String getEmail() {
return email;
}
/**
* @param email 要设置的 email
*/
public void setEmail(String email) {
this.email = email;
}
/**
* @return id
*/
public int getId() {
return id;
}
/**
* @param id 要设置的 id
*/
public void setId(int id) {
this.id = id;
}
/**
* @return password
*/
public String getPassword() {
return password;
}
/**
* @param password 要设置的 password
*/
public void setPassword(String password) {
this.password = password;
}
/**
* @return username
*/
public String getUsername() {
return username;
}
/**
* @param username 要设置的 username
*/
public void setUsername(String username) {
this.username = username;
}
}
//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="ch03.hibernate.User" table="myusertable">
<id name="id">
<generator class=" identity " />
</id>
<property name="username"/>
<property name="password"/>
<property name="email" />
</class>
</hibernate-mapping>
//hibernate.cfg.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-configuration>
<session-factory>
<!--Database connectiong settings(数据库连接设置)-->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/myproject</property>
<property name="connection.username">root</property>
<property name="connection.password">111111</property>
<!--JDBC connection pool(连接池)(use the bulit-in)-->
<property name="connection.pool_size">1</property>
<!--SQL dialect(SQL方言)-->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!--Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!--Disable the second-level cache-->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!--Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!--Drop and re-create the database schema on startup-->
<property name="hbm2ddl.auto">create</property>
<!--映射资源-->
<mapping resource="ch03/hibernate/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
//调试的时候报错,我是初学者,实在不知道如何修改错误,请指点,谢谢!
org.hibernate.MappingException: invalid configuration
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1347)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1288)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1274)
at ch03.hibernate.Test.main(Test.java:22)
Caused by: org.xml.sax.SAXParseException: Document root element "hibernate-configuration", must match DOCTYPE root "hibernate-mapping".
at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
at org.apache.xerces.util.ErrorHandlerWrapper.error(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.dtd.XMLDTDValidator.rootElementSpecified(Unknown Source)
at org.apache.xerces.impl.dtd.XMLDTDValidator.handleStartElement(Unknown Source)
at org.apache.xerces.impl.dtd.XMLDTDValidator.startElement(Unknown Source)
at org.apache.xerces.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown Source)
at org.apache.xerces.impl.XMLNSDocumentScannerImpl$NSContentDispatcher.scanRootElementHook(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
at org.dom4j.io.SAXReader.read(SAXReader.java:465)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1344)
... 3 more