初学hibernate编程,我的第一个例程,自己不会调试,请高手指点一下

tshbona 2007-12-03 09:56:29
/**
*
* 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
...全文
641 点赞 收藏 8
写回复
8 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
tshbona 2007-12-17
我解决了,其实是我的<?xml version="1.0" encoding='utf-8'?> 这句之前有空行和空格,呵呵
回复
forestking_xx 2007-12-03
这个错误表明你没有把log4j和dom4j等lib文件加入。log4j用于生成log,而dom4j用于解析xml文件。
回复
tshbona 2007-12-03
谢谢,修改之后还是提示下列错误,还有就是version='1.0'根version="1.0"是不是一样的,谢谢
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
org.hibernate.HibernateException: Could not parse configuration: /hibernate.cfg.xml
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1354)
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.dom4j.DocumentException: Error on line 1 of document : Content is not allowed in prolog. Nested exception: Content is not allowed in prolog.
at org.dom4j.io.SAXReader.read(SAXReader.java:482)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1344)
... 3 more
回复
Johnson_Hong 2007-12-03
你把hibernate.cfg.xml的文件头用错了,你用的是配置mapping的,是复制的吧,应该用
<!DOCTYPE hibernate-configuration
PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
回复
天外流星 2007-12-03

org.hibernate.MappingException: invalid configuration
说明mapping文件User.hbm.xml有问题.
Caused by: org.xml.sax.SAXParseException: Document root element "hibernate-configuration", must match DOCTYPE root "hibernate-mapping".
说明文件的格式有问题.
文件头有问题
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration
PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
回复
tshbona 2007-12-03
Welcome2Hibernate
src
JRE_LIB - D:\Sun\AppServer\jdk\jre\lib\rt.jar
Hibernate3.1
mysql-connector-java-3.1.14-bin.jar - D:\mysql-connector-java-3.1.14
Hibernate 3.1 Core Libraries
antlr-2.7.5H3.jar - D:\MyEclipse 5.0GA\eclipse\plugins\com.genuitec.org.hibernate.eclipse_5.0.0\myeclipse-data\3.0\lib
asm.jar - D:\MyEclipse 5.0GA\eclipse\plugins\com.genuitec.org.hibernate.eclipse_5.0.0\myeclipse-data\3.0\lib
asm-attrs.jar - D:\MyEclipse 5.0GA\eclipse\plugins\com.genuitec.org.hibernate.eclipse_5.0.0\myeclipse-data\3.0\lib
cglib-2.1.3.jar - D:\MyEclipse 5.0GA\eclipse\plugins\com.genuitec.org.hibernate.eclipse_5.0.0\myeclipse-data\3.1\lib
commons-collections-2.1.1.jar - D:\MyEclipse 5.0GA\eclipse\plugins\com.genuitec.org.hibernate.eclipse_5.0.0\myeclipse-data\3.0\lib
commons-logging-1.0.4.jar - D:\MyEclipse 5.0GA\eclipse\plugins\com.genuitec.org.hibernate.eclipse_5.0.0\myeclipse-data\3.0\lib
dom4j-1.6.1.jar - D:\MyEclipse 5.0GA\eclipse\plugins\com.genuitec.org.hibernate.eclipse_5.0.0\myeclipse-data\3.1\lib
ehcache-1.1.jar - D:\MyEclipse 5.0GA\eclipse\plugins\com.genuitec.org.hibernate.eclipse_5.0.0\myeclipse-data\3.0\lib
hibernate3.jar - D:\MyEclipse 5.0GA\eclipse\plugins\com.genuitec.org.hibernate.eclipse_5.0.0\myeclipse-data\3.1\lib
jaas.jar - D:\MyEclipse 5.0GA\eclipse\plugins\com.genuitec.org.hibernate.eclipse_5.0.0\myeclipse-data\3.0\lib
jaxen-1.1-beta-7.jar - D:\MyEclipse 5.0GA\eclipse\plugins\com.genuitec.org.hibernate.eclipse_5.0.0\myeclipse-data\3.1\lib
jdbc2_0-stdext.jar - D:\MyEclipse 5.0GA\eclipse\plugins\com.genuitec.org.hibernate.eclipse_5.0.0\myeclipse-data\3.0\lib
jta.jar - D:\MyEclipse 5.0GA\eclipse\plugins\com.genuitec.org.hibernate.eclipse_5.0.0\myeclipse-data\3.0\lib
log4j-1.2.11.jar - D:\MyEclipse 5.0GA\eclipse\plugins\com.genuitec.org.hibernate.eclipse_5.0.0\myeclipse-data\3.1\lib
xerces-2.6.2.jar - D:\MyEclipse 5.0GA\eclipse\plugins\com.genuitec.org.hibernate.eclipse_5.0.0\myeclipse-data\3.0\lib
xml-apis.jar - D:\MyEclipse 5.0GA\eclipse\plugins\com.genuitec.org.hibernate.eclipse_5.0.0\myeclipse-data\3.0\lib
回复
tshbona 2007-12-03
我想这不是关键,我也能找到Dom4j和Log4j的包
Welcome2Hibernate
src
JRE_LIB - D:\Sun\AppServer\jdk\jre\lib\rt.jar
Hibernate3.1
mysql-connector-java-3.1.14-bin.jar - D:\mysql-connector-java-3.1.14
Hibernate 3.1 Core Libraries
antlr-2.7.5H3.jar - D:\MyEclipse 5.0GA\eclipse\plugins\com.genuitec.org.hibernate.eclipse_5.0.0\myeclipse-data\3.0\lib
asm.jar - D:\MyEclipse 5.0GA\eclipse\plugins\com.genuitec.org.hibernate.eclipse_5.0.0\myeclipse-data\3.0\lib
asm-attrs.jar - D:\MyEclipse 5.0GA\eclipse\plugins\com.genuitec.org.hibernate.eclipse_5.0.0\myeclipse-data\3.0\lib
cglib-2.1.3.jar - D:\MyEclipse 5.0GA\eclipse\plugins\com.genuitec.org.hibernate.eclipse_5.0.0\myeclipse-data\3.1\lib
commons-collections-2.1.1.jar - D:\MyEclipse 5.0GA\eclipse\plugins\com.genuitec.org.hibernate.eclipse_5.0.0\myeclipse-data\3.0\lib
commons-logging-1.0.4.jar - D:\MyEclipse 5.0GA\eclipse\plugins\com.genuitec.org.hibernate.eclipse_5.0.0\myeclipse-data\3.0\lib
dom4j-1.6.1.jar - D:\MyEclipse 5.0GA\eclipse\plugins\com.genuitec.org.hibernate.eclipse_5.0.0\myeclipse-data\3.1\lib
ehcache-1.1.jar - D:\MyEclipse 5.0GA\eclipse\plugins\com.genuitec.org.hibernate.eclipse_5.0.0\myeclipse-data\3.0\lib
hibernate3.jar - D:\MyEclipse 5.0GA\eclipse\plugins\com.genuitec.org.hibernate.eclipse_5.0.0\myeclipse-data\3.1\lib
jaas.jar - D:\MyEclipse 5.0GA\eclipse\plugins\com.genuitec.org.hibernate.eclipse_5.0.0\myeclipse-data\3.0\lib
jaxen-1.1-beta-7.jar - D:\MyEclipse 5.0GA\eclipse\plugins\com.genuitec.org.hibernate.eclipse_5.0.0\myeclipse-data\3.1\lib
jdbc2_0-stdext.jar - D:\MyEclipse 5.0GA\eclipse\plugins\com.genuitec.org.hibernate.eclipse_5.0.0\myeclipse-data\3.0\lib
jta.jar - D:\MyEclipse 5.0GA\eclipse\plugins\com.genuitec.org.hibernate.eclipse_5.0.0\myeclipse-data\3.0\lib
log4j-1.2.11.jar - D:\MyEclipse 5.0GA\eclipse\plugins\com.genuitec.org.hibernate.eclipse_5.0.0\myeclipse-data\3.1\lib
xerces-2.6.2.jar - D:\MyEclipse 5.0GA\eclipse\plugins\com.genuitec.org.hibernate.eclipse_5.0.0\myeclipse-data\3.0\lib
xml-apis.jar - D:\MyEclipse 5.0GA\eclipse\plugins\com.genuitec.org.hibernate.eclipse_5.0.0\myeclipse-data\3.0\lib
回复
天外流星 2007-12-03
org.dom4j.DocumentException: Error on line 1 of document : Content is not allowed in prolog.
说明Dom4j读取hibernate.cfg.xml文件有错误,检查一下你的hibernate.cfg.xml文件的格式.
比如:
xml文件是UTF-8编码的,如果该文件通过Ultraedit编辑后,会在无BOM头的UTF-8文件中加入BOM,但是DOM4j不认这个BOM(dom4j1.3),解决的办法可以通过升级dom4j到1.6解决www.dom4j.org
什么是BOM?http://www.unicode.org/faq/utf_bom.html#22
回复
相关推荐
发帖
Java EE
创建于2007-09-28

6.6w+

社区成员

J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
申请成为版主
帖子事件
创建了帖子
2007-12-03 09:56
社区公告
暂无公告