【问题标题】:unable to map second class : ** org.hibernate.MappingException: An association from the table employee refers to an unmapped class**无法映射第二类:** org.hibernate.MappingException:来自表员工的关联指的是未映射的类**
【发布时间】:2016-01-14 06:33:41
【问题描述】:

我正在使用无法映射其他类的代码,因为我有两个类,并且这个类的代码工作正常,我做错了吗,请检查代码:

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

}

错误日志说员工被映射到非映射类部门 为什么?以及当一个人与两个以上的班级一起工作时如何解决...... 任何帮助将不胜感激 问候和感谢。

【问题讨论】:

  • 发布您的 Employee.java 代码。您的 Employee.java 中是否有类型为 Department 的可变部门字段?
  • 尝试将全限定类名添加到映射中
  • 我添加了employee.java和department.java
  • 只是猜测。如果你完全限定了类名,那么你在引用或关系中不做同样的事情吗?
  • 知道了,谢谢您的帮助

标签: java xml hibernate


【解决方案1】:

使用完全限定的类名

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

Many-To-One的详细信息

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-15
    • 1970-01-01
    • 1970-01-01
    • 2019-09-30
    • 1970-01-01
    • 2010-12-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多