我正在處理代碼,因爲我無法映射其他類,因爲我已經兩個階級,這個代碼用一個類工作得很好,我做錯了,請檢查代碼:無法映射第二類:** org.hibernate.MappingException:來自表employee的關聯指的是未映射的類**
department.hbm.xml
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中是否有類型部門的變量部門字段? – Siva
嘗試將完全合格的類名添加到映射 – Jens
我添加了employee.java和department.java – emotionlessbananas