【问题标题】:Error Creating SessionFactory Hibernate in myeclipse reverse engineered database在 myeclipse 逆向工程数据库中创建 SessionFactory Hibernate 时出错
【发布时间】:2012-12-17 18:40:14
【问题描述】:

我正在 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 中键入 from hitest 时,它会告诉我 org.hibernate.hql.ast.QuerySyntaxException:hitest 未映射 [来自 hitest] 而 来自 sor.hibernate.Hitest 效果很好

【问题讨论】:

  • 请注意,“from hitest”正在寻找“hitest”的映射,而不是“Hitest”。名称区分大小写。
  • 非常感谢!它适用于“来自 Hitest”

标签: hibernate jakarta-ee hibernate-mapping object-relational-model


【解决方案1】:

尝试删除中的空白

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

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

【讨论】:

  • 我试过了,但是我搜索了'"'没有其他类似的实例。我没有更改错误
  • 堆栈跟踪中的这一行很奇怪:jboss.org/dtd/hibernate/hibernate-mapping-4.0.dtd%20。这个 URL 是在哪里引用的?最后似乎有一个多余的空白(%20)。
  • @JB_Nizet :删除您指示的空格会省略该错误行。错误并没有太大的不同。注意:我更改了 db-name 并尝试使用新连接。事实上新的错误是这样的:pastebin.com/ghWF66nQ
  • 我没有足够的声望来支持你的答案。它有很大帮助。下一个错误是关于验证的。现在我找到了答案。
  • pastebin.com/FEtTUyNt 是我的问题的答案。由于声誉低,stackoverflow 不允许我在不到 8 小时内发布我自己的问题的答案。所以我把它贴到pastebin
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 2011-05-17
    • 2018-08-03
    • 2011-07-24
    • 2015-05-02
    • 1970-01-01
    • 2010-09-23
    • 2012-03-19
    • 2017-06-23
    • 2012-12-20
    相关资源
    最近更新 更多