【发布时间】:2021-11-02 05:38:00
【问题描述】:
我正在学习 hibernate 并遵循我正在关注的书中完全相同的代码。我使用eclipse IDE 和hibernate3 库。我的数据库是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