无法映射第二个类:** org.hibernate.MappingException:表employee中的关联引用了未映射的类**

时间:2016-01-14 06:33:42

标签: java xml hibernate

我正在处理我无法映射其他类的代码,因为我有两个类,这个代码与一个类工作正常,我做错了请检查代码:

department.hbm.xml

 <hibernate-mapping>
        <class name="com.java.commons.Department" table="department">
            <id name="id" column="id">
                <generator class="assigned" />
            </id>

            <property name="deptName" column="deptName" />
        </class>
    </hibernate-mapping>

employee.hbm.xml

<hibernate-mapping>
    <class name="com.java.commons.Employee" table="employee">
        <id name="id" column="id" type="long">
            <generator class="identity" />
        </id>
        <property name="firstName" type="String" column="firstName" />
        <property name="salary" column="salary" />
        <many-to-one name="department" class="Department" column="department" />
    </class>
</hibernate-mapping>

connection.cfg.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:3306/justhibernate</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">root</property>
        <property name="hibernate.show_sql">true</property>
        <property name="hibernate.hbm2ddl.auto">create</property>
        <property name="hibernate.order_inserts">true</property>
        <property name="hibernate.order_updates">true</property>
        <property name="hibernate.jdbc.batch_size">2</property>

        <mapping resource="employee.hbm.xml" />
        <mapping resource="department.hbm.xml" />


    </session-factory>

</hibernate-configuration>

connection.java

public static SessionFactory buldSessionFactory() {
        SessionFactory sessionFactory = null;
        try {
            sessionFactory = new Configuration().configure("connection.cfg.xml").addResource("department.hbm.xml")
                    .addResource("employee.hbm.xml").buildSessionFactory();
            System.out.println("session Factory created");
        } catch (Throwable ex) {
            System.err.println("Session Factory creation Failed  " + ex);
            throw new ExceptionInInitializerError(ex);
        }
        return sessionFactory;

    }

错误日志

Session Factory creation Failed  org.hibernate.MappingException: An association from the table employee refers to an unmapped class: Department
Exception in thread "main" java.lang.ExceptionInInitializerError
    at com.java.commons.HibernateUtil.buldSessionFactory(HibernateUtil.java:16)
    at com.java.save_data.SavingData.savingData(SavingData.java:9)
    at com.java.save_data.SavingData.main(SavingData.java:15)
Caused by: org.hibernate.MappingException: An association from the table employee refers to an unmapped class: Department
    at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.secondPassCompileForeignKeys(InFlightMetadataCollectorImpl.java:1838)
    at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.secondPassCompileForeignKeys(InFlightMetadataCollectorImpl.java:1809)
    at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1627)
    at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:278)
    at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.build(MetadataBuildingProcess.java:83)
    at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:418)
    at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:87)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:692)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:724)
    at com.java.commons.HibernateUtil.buldSessionFactory(HibernateUtil.java:12)
    ... 2 more

Employee.java

public class Employee {

    private long id;
    private String firstName;
    private String salary;
    private Department department;

    public Employee() {
    }



    public Employee(String firstName, String salary, Department department) {
        this.firstName = firstName;
        this.salary = salary;
        this.department = department;
    }

    public long getId() {
        return id;
    }

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

    public String getFirstName() {
        return firstName;
    }

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

    public String getSalary() {
        return salary;
    }

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

    public Department getDepartment() {
        return department;
    }

    public void setDepartment(Department department) {
        this.department = department;
    }
}

department.java

public class Department {
    private long id;
    private String deptName;

    public Department() {
    }

    public long getId() {
        return id;
    }

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

    public String getDeptName() {
        return deptName;
    }

    public void setDeptName(String deptName) {
        this.deptName = deptName;
    }

    public Department(String deptName) {
        this.deptName = deptName;
    }

}

错误日志表示员工被映射到非映射类部门为什么?以及当一个人使用两个以上的课程时如何解决... 任何帮助将不胜感激 问候和谢谢。

1 个答案:

答案 0 :(得分:2)

使用完全限定的班级名称

<many-to-one name="department" class="com.java.commons.Department" column="department"  />

Many-To-One

的详细信息