初次接触Hibernate,老是出现 Unable to read XML的错误

byte_z 2013-04-27 08:52:20
hibernate.cfg.xml如下:
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>

<session-factory>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/demo
</property>
<property name="connection.username">root</property>
<property name="connection.password">123456</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="myeclipse.connection.profile">
testDriver
</property>
<mapping resource="model/Xsb.hbm.xml" />

</session-factory>

</hibernate-configuration>


Xsb.hbm.xml文件如下:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd ">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="model.Xsb" table="XSB" >
<id name="xh" type="java.lang.String">
<column name="XH" length="10" />
<generator class="assigned" />
</id>
<property name="xsm" type="java.lang.String">
<column name="XSM" length="20" not-null="true" />
</property>
</class>
</hibernate-mapping>

这些配置文件了,POJO类了什么的都是MyEclipse 10.6自动生成的…………



错误如下:
2013-4-27 20:41:13 org.hibernate.annotations.common.Version <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.1.Final}
2013-4-27 20:41:13 org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.1.4.Final}
2013-4-27 20:41:13 org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
2013-4-27 20:41:13 org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
2013-4-27 20:41:13 org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
2013-4-27 20:41:13 org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
2013-4-27 20:41:13 org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: model/Xsb.hbm.xml
%%%% Error Creating SessionFactory %%%%
org.hibernate.InvalidMappingException: Unable to read XML
at org.hibernate.internal.util.xml.MappingReader.readMappingDocument(MappingReader.java:109)
at org.hibernate.cfg.Configuration.add(Configuration.java:478)
at org.hibernate.cfg.Configuration.add(Configuration.java:474)
at org.hibernate.cfg.Configuration.add(Configuration.java:647)
at org.hibernate.cfg.Configuration.addResource(Configuration.java:730)
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:2109)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:2081)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2061)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2014)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1929)
at session.HibernateSessionFactory.<clinit>(HibernateSessionFactory.java:31)
at test.Tester.main(Tester.java:11)
Caused by: org.dom4j.DocumentException: http://www.jboss.org/dtd/hibernate/hibernate-mapping-3.0.dtd%20 Nested exception: http://www.jboss.org/dtd/hibernate/hibernate-mapping-3.0.dtd%20
at org.dom4j.io.SAXReader.read(SAXReader.java:484)
at org.hibernate.internal.util.xml.MappingReader.readMappingDocument(MappingReader.java:78)
... 11 more
2013-4-27 20:41:14 org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
2013-4-27 20:41:14 org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
2013-4-27 20:41:14 org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: model/Xsb.hbm.xml
%%%% Error Creating SessionFactory %%%%
org.hibernate.InvalidMappingException: Unable to read XML
at org.hibernate.internal.util.xml.MappingReader.readMappingDocument(MappingReader.java:109)
at org.hibernate.cfg.Configuration.add(Configuration.java:478)
at org.hibernate.cfg.Configuration.add(Configuration.java:474)
at org.hibernate.cfg.Configuration.add(Configuration.java:647)
at org.hibernate.cfg.Configuration.addResource(Configuration.java:730)
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:2109)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:2081)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2061)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2014)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1929)
at session.HibernateSessionFactory.rebuildSessionFactory(HibernateSessionFactory.java:70)
at session.HibernateSessionFactory.getSession(HibernateSessionFactory.java:54)
at test.Tester.main(Tester.java:11)
Caused by: org.dom4j.DocumentException: http://www.jboss.org/dtd/hibernate/hibernate-mapping-3.0.dtd%20 Nested exception: http://www.jboss.org/dtd/hibernate/hibernate-mapping-3.0.dtd%20
at org.dom4j.io.SAXReader.read(SAXReader.java:484)
at org.hibernate.internal.util.xml.MappingReader.readMappingDocument(MappingReader.java:78)
... 12 more
Exception in thread "main" java.lang.NullPointerException
at test.Tester.main(Tester.java:13)


HibernateSessionFactory如下:
package session;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;

/**
* Configures and provides access to Hibernate sessions, tied to the
* current thread of execution. Follows the Thread Local Session
* pattern, see {@link http://hibernate.org/42.html }.
*/
@SuppressWarnings("deprecation")
public class HibernateSessionFactory {

/**
* Location of hibernate.cfg.xml file.
* Location should be on the classpath as Hibernate uses
* #resourceAsStream style lookup for its configuration file.
* The default classpath location of the hibernate config file is
* in the default package. Use #setConfigFile() to update
* the location of the configuration file for the current session.
*/
private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
private static Configuration configuration = new Configuration();
private static org.hibernate.SessionFactory sessionFactory;
private static String configFile = CONFIG_FILE_LOCATION;

static {
try {
configuration.configure(configFile);
sessionFactory = configuration.buildSessionFactory();
} catch (Exception e) {
System.err
.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
private HibernateSessionFactory() {
}

/**
* Returns the ThreadLocal Session instance. Lazy initialize
* the <code>SessionFactory</code> if needed.
*
* @return Session
* @throws HibernateException
*/
public static Session getSession() throws HibernateException {
Session session = (Session) threadLocal.get();

if (session == null || !session.isOpen()) {
if (sessionFactory == null) {
rebuildSessionFactory();
}
session = (sessionFactory != null) ? sessionFactory.openSession()
: null;
threadLocal.set(session);
}

return session;
}

/**
* Rebuild hibernate session factory
*
*/
public static void rebuildSessionFactory() {
try {
configuration.configure(configFile); sessionFactory = configuration.buildSessionFactory();
} catch (Exception e) {
System.err
.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}

/**
* Close the single hibernate session instance.
*
* @throws HibernateException
*/
public static void closeSession() throws HibernateException {
Session session = (Session) threadLocal.get();
threadLocal.set(null);

if (session != null) {
session.close();
}
}

/**
* return session factory
*
*/
public static org.hibernate.SessionFactory getSessionFactory() {
return sessionFactory;
}

/**
* return session factory
*
* session factory will be rebuilded in the next call
*/
public static void setConfigFile(String configFile) {
HibernateSessionFactory.configFile = configFile;
sessionFactory = null;
}

/**
* return hibernate configuration
*
*/
public static Configuration getConfiguration() {
return configuration;
}

}
...全文
1636 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
查看余e 2014-05-15
  • 打赏
  • 举报
回复
三楼 大神 。。
  • 打赏
  • 举报
回复
我也遇到这个问题了,真的是一个空格!
OD9999 2014-03-30
  • 打赏
  • 举报
回复
3楼正解!找个半天居然是因为一个空格!!!????
sxax 2014-01-10
  • 打赏
  • 举报
回复
引用 3 楼 fangmingshijie 的回复:
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd ">//看到dtd后面的空格没有?解析不了
之前我一直都没关心过这个后面带空格,学习下~ 建议LZ可以尝试修改下
loveunittesting 2014-01-10
  • 打赏
  • 举报
回复
hibernate-mapping-3.0.dtd 后面的空格要去掉,你看日志,不是有个20%吗,就是空格
鼠标键盘 2014-01-09
  • 打赏
  • 举报
回复
3楼正解啊。。不知道为什么hibernate4+的用Myeclipse生成会多有空格
fw347969680 2014-01-09
  • 打赏
  • 举报
回复
引用 7 楼 u010816188 的回复:
3楼正解啊。。不知道为什么hibernate4+的用Myeclipse生成会多有空格
正解,XML的格式问题。
vigiles 2013-11-22
  • 打赏
  • 举报
回复
请问楼主这个问题解决了吗
xianwangkai 2013-05-03
  • 打赏
  • 举报
回复
你先弄个空的hibernate配置文件,最好从hibernate的官网下载的demo中去,然后试一试,如果好着,然后在修改里面内容为你的xml内容。 怀疑还是你的xml中有些发容易发现的错误!
cjbedu2012 2013-05-03
  • 打赏
  • 举报
回复
<property name="myeclipse.connection.profile"> testDriver</property> 一般不加为好
  • 打赏
  • 举报
回复
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd ">//看到dtd后面的空格没有?解析不了
xian_hf 2013-04-27
  • 打赏
  • 举报
回复
是不是缺少读取xml的jar包? dtd文件是否在本地?
byte_z 2013-04-27
  • 打赏
  • 举报
回复

目录结构
PS:文件包也都是MyEclipse自己添加的。

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧