【问题标题】:Duplicate import: Employee -> Employee org.hibernate.InvalidMappingException: Could not parse mapping document from resource Employee.hbm.xml重复导入:Employee -> Employee org.hibernate.InvalidMappingException:无法从资源 Employee.hbm.xml 解析映射文档
【发布时间】:2013-12-06 03:19:14
【问题描述】:

我是新的冬眠者 当我尝试执行以下代码时,我遇到了异常:

org.hibernate.InvalidMappingException: 无法从资源 Employee.hbm.xml 解析映射文档

这是我的代码:

配置:

    <hibernate-configuration> 
    <session-factory> 
    <property name="hibernate.dialect"> 
    org.hibernate.dialect.MySQLDialect 
    </property> 
    <property name="hibernate.connection.driver_class"> 
    com.mysql.jdbc.Driver 
    </property> 

    <property name="hibernate.connection.url"> 
    jdbc:mysql://localhost/hibernatetest 
    </property> 
    <property name="hibernate.connection.username"> 
    root 
    </property> 
    <property name="hibernate.connection.password"> 
    root 
    </property> 

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

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

来源:

    package com.practice.HBtest; 

public class Employee {

    private int id;
    private String firstName;
    private String lastName;
    private Integer 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;
    }
}

映射:

<hibernate-mapping> 
<class name="Employee" table="EMPLOYEE"> 
<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> 

来源摘录:

import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

public class EmployeeRecordManager {

    private static SessionFactory sessionFactory;
    private static ServiceRegistry serviceRegistry;
    private Session session;

    public static void main(String[] args) {

        EmployeeRecordManager erm = new EmployeeRecordManager();

        try {
            Configuration cfg = new Configuration().addResource("Employee.hbm.xml");
            SessionFactory sessionFactory = cfg.configure().buildSessionFactory(serviceRegistry);

        } catch (Exception e) {
            System.out.println(e);
        }
        Integer empID1 = erm.addEmployee("Amruta", "Ali", 1000);
        Integer empID2 = erm.addEmployee("Ruchi", "Das", 5000);
        Integer empID3 = erm.addEmployee("Mitul", "Paul", 10000);

        System.out.println(erm);

    }

    public Integer addEmployee(String fname, String lname, int salary) {
        Session session = sessionFactory.openSession();
        Transaction tx = null;
        Integer empID = null;
        try {
            tx = session.beginTransaction();
            Employee employee = new Employee(fname, lname, salary);
            empID = (Integer) session.save(employee);
            tx.commit();
        } catch (HibernateException e) {
            if (tx != null)
                tx.rollback();
            e.printStackTrace();
        } finally {
            session.close();
        }
        return empID;
    }

}







   Nov 7, 2012 5:36:15 PM org.hibernate.annotations.common.Version <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.1.Final}
Nov 7, 2012 5:36:15 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.1.8.Final}
Nov 7, 2012 5:36:15 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Nov 7, 2012 5:36:15 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Nov 7, 2012 5:36:15 PM org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: Employee.hbm.xml
Nov 7, 2012 5:36:15 PM org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
Nov 7, 2012 5:36:15 PM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
Nov 7, 2012 5:36:15 PM org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: Employee.hbm.xml
Nov 7, 2012 5:36:15 PM org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: null
Nov 7, 2012 5:36:15 PM org.hibernate.cfg.Configuration$MappingsImpl addImport
INFO: HHH000071: Duplicate import: Employee -> Employee
org.hibernate.InvalidMappingException: Could not parse mapping document from resource Employee.hbm.xml
    at org.hibernate.cfg.Configuration$MetadataSourceQueue.processHbmXml(Configuration.java:3417)
    at org.hibernate.cfg.Configuration$MetadataSourceQueue.processHbmXmlQueue(Configuration.java:3406)
    at org.hibernate.cfg.Configuration$MetadataSourceQueue.processMetadata(Configuration.java:3394)
    at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1341)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1737)
    at com.practice.HBtest.EmployeeRecordManager.main(EmployeeRecordManager.java:24)
Caused by: org.hibernate.DuplicateMappingException: Duplicate class/entity mapping Employee
    at org.hibernate.cfg.Configuration$MappingsImpl.addClass(Configuration.java:2582)
    at org.hibernate.cfg.HbmBinder.bindRoot(HbmBinder.java:174)
    at org.hibernate.cfg.Configuration$MetadataSourceQueue.processHbmXml(Configuration.java:3414)
    ... 5 more
Exception in thread "main" java.lang.NullPointerException
    at com.practice.HBtest.EmployeeRecordManager.addEmployee(EmployeeRecordManager.java:39)
    at com.practice.HBtest.EmployeeRecordManager.main(EmployeeRecordManager.java:30)
org.hibernate.InvalidMappingException: Could not parse mapping document from resource Employee.hbm.xml
After Adding the code mentioned by you was getting below Exception:


  Nov 7, 2012 6:48:24 PM org.hibernate.annotations.common.Version <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.1.Final}
Nov 7, 2012 6:48:24 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.1.8.Final}
Nov 7, 2012 6:48:24 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Nov 7, 2012 6:48:24 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Nov 7, 2012 6:48:24 PM org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: hibernate.cfg.xml
Nov 7, 2012 6:48:24 PM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: hibernate.cfg.xml
Nov 7, 2012 6:48:24 PM org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: Employee.hbm.xml
Nov 7, 2012 6:48:24 PM org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: null
Nov 7, 2012 6:48:24 PM org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
Nov 7, 2012 6:48:24 PM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
Nov 7, 2012 6:48:24 PM org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: Employee.hbm.xml
Nov 7, 2012 6:48:24 PM org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: null
Nov 7, 2012 6:48:24 PM org.hibernate.cfg.Configuration$MappingsImpl addImport
INFO: HHH000071: Duplicate import: Employee -> Employee
org.hibernate.InvalidMappingException: Could not parse mapping document from resource Employee.hbm.xml
    at org.hibernate.cfg.Configuration$MetadataSourceQueue.processHbmXml(Configuration.java:3417)
    at org.hibernate.cfg.Configuration$MetadataSourceQueue.processHbmXmlQueue(Configuration.java:3406)
    at org.hibernate.cfg.Configuration$MetadataSourceQueue.processMetadata(Configuration.java:3394)
    at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1341)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1737)
    at com.practice.HBtest.EmployeeRecordManager.main(EmployeeRecordManager.java:24)
Caused by: org.hibernate.DuplicateMappingException: Duplicate class/entity mapping Employee
    at org.hibernate.cfg.Configuration$MappingsImpl.addClass(Configuration.java:2582)
    at org.hibernate.cfg.HbmBinder.bindRoot(HbmBinder.java:174)
    at org.hibernate.cfg.Configuration$MetadataSourceQueue.processHbmXml(Configuration.java:3414)
    ... 5 more
Exception in thread "main" java.lang.NullPointerException
    at com.practice.HBtest.EmployeeRecordManager.addEmployee(EmployeeRecordManager.java:39)
    at com.practice.HBtest.EmployeeRecordManager.main(EmployeeRecordManager.java:30)
org.hibernate.InvalidMappingException: Could not parse mapping document from resource Employee.hbm.xml

【问题讨论】:

  • 发布您的Employee.hbm.xml 代码
  • 您的Employee.hbm.xml 或其路径存在问题。
  • Duplicated 参考链接修改配置
  • Employee.hbm.xml是一个配置文件

标签: java


【解决方案1】:

您的 hbm.xml 属性名称之间存在大小写差异:

<property name="firstname" column="first_name" type="String"/> 
<property name="lastname" column="last_name" type="String"/> 
<property name="salary" column="salary" type="int"/> 

以及您班级中的实际成员字段:

private String first**N**ame; 
private String last**N**ame; 
private Integer salary; 

更重要的是,你班级中的 getter 和 setter 也有驼峰式,而 hibernate 会寻找 getFirstname(), setFirstname(), ...

【讨论】:

  • 我已经完成了上面提到的更改,但它给了我同样的例外。
  • 还继续它所说的异常:线程主线程中的异常:“java.lang.NullPointerException”
【解决方案2】:

使用JPA annotations 而不是乱七八糟的XML

【讨论】:

    【解决方案3】:

    您能粘贴整个堆栈跟踪吗?在没有完整的堆栈跟踪的情况下调试它有点困难,因为在加载休眠配置的时候有很多事情可能会出错。

    这可能与您定义配置和建立连接的方式有关。我通常做的是:

    • 从主休眠配置文件加载配置 configuration = new Configuration().configure("hibernate.cfg.xml");

    • 根据您加载的配置构建会话工厂 sessionFactory = configuration.buildSessionFactory();

    没有理由显式添加 hbm.xml 资源(因为您已经在主配置文件中定义了该资源,也没有理由在构建会话工厂时使用服务注册表参数...

    此外,您的 id 映射可能需要将 DB 列放在单独的标签中,例如:

    <id name="id" type="int">
                <column name="id" scale="0" />
                <generator class="native" />
    </id>
    

    【讨论】:

    • 当我尝试了从主休眠配置文件加载配置的建议时,配置 = new Configuration().configure("hibernate.cfg.xml");它抛出一个异常
    • 嘿,我能够解决我的问题。缺少一个 jar,添加了该 jar,并为 mysql 添加了正确的端口,从而解决了这个问题。 :)
    【解决方案4】:

    我能够解决这个问题。 1.新增mysql连接器jar 2.为此添加了正确的端口 3.使用如下配置:

    Configuration configuration = new Configuration();
                configuration.configure();
                serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();        
                sessionFactory = configuration.buildSessionFactory(serviceRegistry);
    

    谢谢 每个曾经做出过贡献的人

    【讨论】:

      【解决方案5】:

      我经历过这个问题;找到以下解决方案

      • 清理您的服务器构建目录,即清理 Eclipse 项目并制作 肯定一样(重复)

      .hbm 文件不应存在。

      • 如果您的 POJO 在项目之外;然后将其保存到包中并在 .hbm 文件中定义包

      • 您必须在 POJO w.r.t hbm 文件中拥有所有可用的字段及其 getter 和 setter。

      • 确保字段区分大小写,如上面“Filip”所述。

      • 检查 jar 文件是否可用

      【讨论】:

        猜你喜欢
        • 2013-05-19
        • 2013-05-13
        • 1970-01-01
        • 2016-01-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-09-12
        相关资源
        最近更新 更多