0

我正在myeclipse中学习休眠。我尝试对 mysql 表进行逆向工程并遵循官方的 myeclipse 教程。生成了所有 xml 文件,但编写了一个简单的测试程序,它给了我以下错误:

.

     17-dic-2012 19.18.57 org.hibernate.annotations.common.Version <clinit>
     INFO: HCANN000001: Hibernate Commons Annotations {4.0.1.Final}
     17-dic-2012 19.18.57 org.hibernate.Version logVersion
     INFO: HHH000412: Hibernate Core {4.1.4.Final}
     17-dic-2012 19.18.57 org.hibernate.cfg.Environment <clinit>
     INFO: HHH000206: hibernate.properties not found
     17-dic-2012 19.18.57 org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
17-dic-2012 19.18.57 org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
17-dic-2012 19.18.57 org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
17-dic-2012 19.18.57 org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: sor/hibernate/Hitest.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 org.hibernate.cfg.Configuration.configure(Configuration.java:1908)
    at sor.hibernate.HibernateSessionFactory.<clinit>(HibernateSessionFactory.java:32)
    at sor.hibernate.BaseHibernateDAO.getSession(BaseHibernateDAO.java:13)
    at sor.hibernate.HitestTest.main(HitestTest.java:15)
Caused by: org.dom4j.DocumentException: http://www.jboss.org/dtd/hibernate/hibernate-mapping-4.0.dtd%20 Nested exception: http://www.jboss.org/dtd/hibernate/hibernate-mapping-4.0.dtd%20
    at org.dom4j.io.SAXReader.read(SAXReader.java:484)
    at org.hibernate.internal.util.xml.MappingReader.readMappingDocument(MappingReader.java:78)
    ... 13 more
17-dic-2012 19.18.59 org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
17-dic-2012 19.18.59 org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
17-dic-2012 19.18.59 org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: sor/hibernate/Hitest.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 org.hibernate.cfg.Configuration.configure(Configuration.java:1908)
    at sor.hibernate.HibernateSessionFactory.rebuildSessionFactory(HibernateSessionFactory.java:71)
    at sor.hibernate.HibernateSessionFactory.getSession(HibernateSessionFactory.java:55)
    at sor.hibernate.BaseHibernateDAO.getSession(BaseHibernateDAO.java:13)
    at sor.hibernate.HitestTest.main(HitestTest.java:15)
Caused by: org.dom4j.DocumentException: http://www.jboss.org/dtd/hibernate/hibernate-mapping-4.0.dtd%20 Nested exception: http://www.jboss.org/dtd/hibernate/hibernate-mapping-4.0.dtd%20
    at org.dom4j.io.SAXReader.read(SAXReader.java:484)
    at org.hibernate.internal.util.xml.MappingReader.readMappingDocument(MappingReader.java:78)
    ... 14 more
Exception in thread "main" java.lang.NullPointerException
    at sor.hibernate.HitestTest.main(HitestTest.java:15)

测试程序是

package sor.hibernate;


import java.util.Date;

import org.hibernate.Session;
import org.hibernate.Transaction;


public class HitestTest {

    public static void main(String[] args) {

        HitestDAO dao = new HitestDAO();
        Transaction tx = dao.getSession().beginTransaction();

        Hitest hitest[] = new Hitest[10];
        for (int i = 0; i < hitest.length; i++) {
            hitest[i]  = new Hitest();
            hitest[i].setGreeting("saluto numero " + i*23);
            hitest[i].setRef(i+10);
            hitest[i].setDate(new Date(System.currentTimeMillis()));
            dao.save(hitest[i]);
        }
        tx.commit();
        dao.getSession().close();
    }
}

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="hbm2ddl.auto">update</property>
        <property name="dialect">
            org.hibernate.dialect.MySQLDialect
        </property>
        <property name="connection.url">
            jdbc:mysql://localhost:3306/efeulib
        </property>
        <property name="connection.username">root</property>
        <property name="connection.password">qwerty</property>
        <property name="connection.driver_class">
            com.mysql.jdbc.Driver
        </property>
        <property name="myeclipse.connection.profile">
            mysqlJDriver
        </property>
        <mapping resource="sor/hibernate/Hitest.hbm.xml" />

    </session-factory>

</hibernate-configuration>

而 Hitest.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 package="sor.hibernate">
    <class name="sor.hibernate.Hitest" table="hitest" catalog="test">
        <id name="id" type="java.lang.Integer">
            <column name="id" />
            <generator class="assigned" />
        </id>
        <property name="greeting" type="java.lang.String">
            <column name="greeting" length="200" />
        </property>
        <property name="date" type="java.util.Date">
            <column name="date" length="10" />
        </property>
        <property name="ref" type="java.lang.Integer">
            <column name="ref" />
        </property>
    </class>
</hibernate-mapping>

我已阅读相关问题,并删除了所有尾随行和空格。但它们都没有奏效。我添加到这些文件中读取问题的唯一内容是 Hitest.hbm.xml 中的 package="sor.hibernate" 属性路径和...都是正确的,它们都是自动生成的。

更多信息。当我在 HQL 中从 hitest键入时,它告诉我 org.hibernate.hql.ast.QuerySyntaxException: hitest is not mapped [from hitest] while from sor.hibernate.Hitest运行良好

4

2 回答 2

0

尝试删除空格

"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd "
                                                       ^-- here

在你可能犯过同样错误的所有其他地方。

于 2012-12-17T18:49:03.243 回答
0

解决了@JB_Nizet 指出的问题,我遇到了一个关于验证的新错误。(注意:空间的问题是一个myeclipse错误。我尝试了一个项目,它是myeclipse添加了那个邪恶的空间。)我在hibernate.cfg.xml中添加了以下行,问题部分解决了。新错误是(%%%% Error Creating SessionFactory %%%% org.hibernate.HibernateException: Error applications BeanValidation 关系约束)

<property name="javax.persistence.validation.mode">none</property>

新错误是休眠 sql 查询错误。应该已经分配了 ID,所以我将 Hitest.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="sor.hibernate.Hitest" table="hitest" catalog="test">
        <id name="id" type="java.lang.Integer">
            <column name="id" />
            <generator class="increment" />
        </id>
        <property name="greeting" type="java.lang.String">
            <column name="greeting" length="200" />
        </property>
        <property name="date" type="java.util.Date">
            <column name="date" length="10" />
        </property>
        <property name="ref" type="java.lang.Integer">
            <column name="ref" />
        </property>
    </class>
</hibernate-mapping>
于 2012-12-18T08:01:25.267 回答