org.hibernate.MappingException: invalid configuration

来源:互联网 发布:淘宝怎么兑换虾米vip 编辑:程序博客网 时间:2024/05/08 04:37
写hibernate的hello world
内容非常简单,一个实体bean--Student.java
一个测试文件--StudentTest.java
两个配置文件:Student.hbm.xml,hibernate.cfg.xml


可是一直报错:
Exception in thread "main" org.hibernate.MappingException: invalid configuration
Caused by: org.xml.sax.SAXParseException; lineNumber: 3; columnNumber: 25; 文档无效: 找不到语法。


我实在是很困惑啊,都简单到不能简单的东西,还报错,也分明知道是配置文件的错,后来又怀疑是jar包的问题,有怀疑是测试写错了。


今天晚上myeclipse突发善心,居然给我很详细的提示,明确指出是hibernate.cfg.xml的第二行出错。


我于是去hibernate的参考文档中把他们的配置文件整个考过来,然后修改,再测试居然华丽丽的通过了。。


果然是hibernate.cfg.xml的文件头出错了。


原来的文件头是:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration
        xmlns="http://www.hibernate.org/xsd/hibernate-configuration"
        xsi:schemaLocation="http://www.hibernate.org/xsd/hibernate-configuration hibernate-configuration-4.0.xsd"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">


修改之后的是:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">


结论:
再也不能去其他地方乱考配置文件了,之前的配置文件就是我从一个项目里复制过来的。还是直接去文档里找比较好。
0 1
原创粉丝点击