【问题标题】:Hibernate mapping error for no apparent reason没有明显原因的休眠映射错误
【发布时间】:2021-11-02 05:38:00
【问题描述】:

我正在学习 hibernate 并遵循我正在关注的书中完全相同的代码。我使用eclipse IDEhibernate3 库。我的数据库是Oracle 11g,只有一张表。

当我运行项目时,出现以下错误:

Sep 03, 2021 11:26:41 AM org.hibernate.cfg.Environment <clinit>
INFO: Hibernate 3.2.6
Sep 03, 2021 11:26:41 AM org.hibernate.cfg.Environment <clinit>
INFO: hibernate.properties not found
Sep 03, 2021 11:26:41 AM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: Bytecode provider name : cglib
Sep 03, 2021 11:26:41 AM org.hibernate.cfg.Environment <clinit>
INFO: using JDK 1.4 java.sql.Timestamp handling
Sep 03, 2021 11:26:41 AM org.hibernate.cfg.Configuration configure
INFO: configuring from resource: /hibernate.cfg.xml
Sep 03, 2021 11:26:41 AM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: Configuration resource: /hibernate.cfg.xml
Sep 03, 2021 11:26:41 AM org.hibernate.cfg.Configuration addResource
INFO: Reading mappings from resource : hibtest/Employee.hbm.xml
Failed to create sessionFactory object.org.hibernate.InvalidMappingException: Could not parse mapping document from resource hibtest/Employee.hbm.xml
Exception in thread "main" java.lang.ExceptionInInitializerError
    at hibtest.ManageEmployee.main(ManageEmployee.java:17)
Caused by: org.hibernate.InvalidMappingException: Could not parse mapping document from resource hibtest/Employee.hbm.xml
    at org.hibernate.cfg.Configuration.addResource(Configuration.java:575)
    at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1593)
    at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1561)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1540)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1514)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1434)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1420)
    at hibtest.ManageEmployee.main(ManageEmployee.java:14)
Caused by: org.hibernate.InvalidMappingException: Could not parse mapping document from input stream
    at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:514)
    at org.hibernate.cfg.Configuration.addResource(Configuration.java:572)
    ... 7 more
Caused by: org.dom4j.DocumentException: www.hibernate.org Nested exception: www.hibernate.org
    at org.dom4j.io.SAXReader.read(SAXReader.java:484)
    at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:505)
    ... 8 more

我的模型类:

// Employee class
package hibtest;

public class Employee {
private int id;
private String firstName;
private String lastName;
private int salary;

public Employee() {}

public Employee(String fname, String lname, int salary) {
this.firstName = fname;
this.lastName = lname;
this.salary = salary;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getFirstName() {  
return firstName;
}

public void setFirstName(String first_name) {
this.firstName = first_name;
}

public String getLastName() {
return lastName;
}

public void setLastName(String last_name) {
this.lastName = last_name;
}

public int getSalary() {
return salary;
}

public void setSalary( int salary ) {
this.salary = salary;
}

}

// mapping file for Employee class

休眠映射xml文件:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="Employee" table="EMPLOYEE">
<meta attribute="class-description">
This class contains the employee detail.
</meta>
<id name="id" type="int" column="id">
<generator class="native"/>
</id>
<property name="firstName" column="first_name" type="string"/>
<property name="lastName" column="last_name" type="string"/>
<property name="salary" column="salary" type="int"/>
</class>
</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">

<hibernate-configuration>
    <session-factory>
        <property name="connection.driver_class">
            oracle.jdbc.driver.OracleDriver
        </property>
        <property name="connection.url">
            jdbc:oracle:thin:@10.92.68.111:1521:MYSCHEMA
        </property>
        <property name="connection.username">
            TESTHIB
        </property>
        <property name="connection.password">
            testhib
        </property>

        <property name="dialect">org.hibernate.dialect.Oracle9iDialect</property>
        <property name="show_sql">true</property>
        <property name="max_fetch_depth">2</property>

        <property name="dialect" >
            org.hibernate.dialect.Oracle9iDialect
        </property>

        <property name="jdbc.batch_size">0</property>
        <property name="cache.query_cache_factory">
            hibernatingrhinos.hibernate.profiler.cache.ProfilerQueryCacheFactory
        </property>
        <property name="generate_statistics">true</property>

        <!--  the only class  -->

        <mapping resource="hibtest/Employee.hbm.xml" />

    </session-factory>
</hibernate-configuration>

我在这里做错了什么?

表列完全相同,即 id、first_name、last_name 和薪水。

【问题讨论】:

  • 你的hibernate配置好像有问题,可以分享一下你的hibernate配置文件吗?!
  • 我正在学习hibernate那就不要使用XML配置了。我使用 Hibernate 已经 12 年了,即使在那时,XML 配置也是一个遗留选项。除了 Java 注释,我从来没有使用过任何东西。
  • @mr1554 请添加休眠配置

标签: java hibernate orm hibernate-mapping


【解决方案1】:

您的映射似乎正常,但在解决 DTD 时似乎存在问题。

Hibernate 使用 DTD 来验证 XML 映射。 DTD 被检索如下:“http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd” URL。 在某些情况下,当 hibernate 无法检索 URL 时,它会产生错误。检查您的系统是否能够连接到该 URL。如果应用程序无法访问互联网,它应该通过类路径解析 DTD。但是,如果访问被阻止,例如通过强制门户,它可能会给你这个错误。

您可以在此处找到有关休眠映射的更多信息:https://docs.jboss.org/hibernate/core/3.2/reference/en/html/mapping.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-02-27
    • 2017-10-20
    • 1970-01-01
    • 1970-01-01
    • 2018-01-15
    • 2022-11-02
    • 1970-01-01
    相关资源
    最近更新 更多