gpt4 book ai didi

java - 无法映射第二类 : ** org. hibernate.MappingException:表员工中的关联引用了未映射的类**

转载 作者:行者123 更新时间:2023-12-02 04:02:08 25 4
gpt4 key购买 nike

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

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

部门.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;
}

}

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

最佳答案

使用完全限定的类名

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

Many-To-One的详细信息

关于java - 无法映射第二类 : ** org. hibernate.MappingException:表员工中的关联引用了未映射的类**,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34783074/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com