java - Hibernate : Failed to create sessionFactory object. org.hibernate.InvalidMappingException

标签 java hibernate postgresql hibernate-mapping

如果这与任何现有问题重复,我很抱歉。以下是我的员工 bean。

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;
       }
    }

我想将其映射到我的 postgres 表员工

CREATE TABLE employee
(
  id integer NOT NULL DEFAULT nextval('emp_id_seq'::regclass),
  first_name character varying(20),
  last_name character varying(20),
  salary integer,
  CONSTRAINT employee_pkey PRIMARY KEY (id)
)

这是我的主要类代码片段

  try{
     factory = new Configuration().configure().buildSessionFactory();
  }catch (Throwable ex) { 
     System.err.println("Failed to create sessionFactory object." + ex);
     throw new ExceptionInInitializerError(ex); 
  }

运行时出现以下错误

Failed to create sessionFactory object.org.hibernate.InvalidMappingException: Could not parse mapping document from resource Employee.hbm.xml
Exception in thread "main" java.lang.ExceptionInInitializerError
    at ManageEmployee.main(ManageEmployee.java:26)
Caused by: org.hibernate.InvalidMappingException: Could not parse mapping document from resource 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 ManageEmployee.main(ManageEmployee.java:23)

这是我的映射文件

<?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 package = "com.demo.hibernate.beans">
   <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>

我知道这与 Employee.hbm.xml 有关的可能原因是什么。请帮忙

提前致谢!!!

这里是 CFG 文件,如果有帮助的话

<?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"> 
<hibernate-configuration>
   <session-factory>
   <property name="hibernate.dialect">
      org.hibernate.dialect.PostgreSQLDialect
   </property>
   <property name="hibernate.connection.driver_class">
      org.postgresql.Driver
   </property>

   <!-- Assume test is the database name -->
   <property name="hibernate.connection.url">
      jdbc:postgresql://localhost:5432/postgres
   </property>
   <property name="hibernate.connection.username">
      postgres
   </property>
   <property name="hibernate.connection.password">
      postgres
   </property>

   <!-- List of XML mapping files -->
   <mapping resource="Employee.hbm.xml"/>

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

最佳答案

错误意味着 Hibernate 无法加载映射文件(检查代码,是 FileNotFoundException 的包装),因此您的错误位于 hibernate.cfg.xml 文件中,而不是映射中!
检查是否<mapping file="path/to/Employee.hbm.xml" />是正确的

关于java - Hibernate : Failed to create sessionFactory object. org.hibernate.InvalidMappingException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18143828/

相关文章:

javascript - HtmlUnit 未完全加载 YouTube 上的页面

java - href 和 iframe webcenter 上的动态值

java - 如何使用 Hibernate 有选择地更新 Spring bean 的属性?

Java解析DateTime并忽略时区

database-design - 每次查询时是否新创建 PostgreSQL VIEWS?

java - 生成类型标识给出错误,即 id(primary key) 没有默认值

java - 我可以在我的 Android 应用程序中定义动态 intent-filter 吗?

mysql - 具有 JPA 实体和 JAXB 的 rest(JAX-RS) Web 服务

Postgresql:如何从时间戳、时区字段正确创建带时区的时间戳

sql - 如何获取下表中每个最小值和最大值对应的行号