在這裏我試圖插入一行到具有外鍵的表中使用休眠。但我得到以下異常:「org.hibernate.MappingException:未知實體:com.xxx.model.Employee」。我該如何解決這個問題?以下是我的代碼:使用Hibernate DAO方法插入到帶有外鍵的表
Employee.java:
@Entity
@Table(name = "employee")
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public class Employee {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
@Column(name = "id")
private long id;
@Column(name = "emp_name")
private String emp_name;
@ManyToOne
@JoinColumn(name="department_id")
private Department department;
@Column(name = "emp_id")
private String emp_id;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getEmp_name() {
return floor_name;
}
public void setEmp_name(String emp_name) {
this.emp_name = emp_name;
}
public Department getDepartment() {
return department;
}
public void setDepartment(Department department) {
this.department = department;
}
public String getEmp_id() {
return emp_id;
}
public void setEmp_id(String emp_id) {
this.emp_id = emp_id;
}
}
Department.java:
@Entity
@Table(name = "department")
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public class Department {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
@Column(name = "id")
private long id;
@Column(name = "dept_name")
private String dept_name;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getDept_name() {
return dept_name;
}
public void setDept_name(String dept_name) {
this.dept_name = dept_name;
}
}
要添加新員工:
Department department = null;
department = departmentService.getDepartmentById(id);
Employee employee = new Employee();
employee.setEmp_name(emp_name);
employee.setEmp_id(emp_id);
employee.setDepartment(department);
employeeService.addEmployee(employee);
在EmployeeDaoImpl:
@Override
public boolean addEmployee(Employee employee) throws Exception {
session = sessionFactory.openSession();
tx = session.beginTransaction();
session.save(employee);
tx.commit();
session.close();
return false;
}
在DepartmentDaoImpl:
@Override
public Building getDepartmentById(long id) throws Exception {
session = sessionFactory.openSession();
Department department = (Department) session.load(Department.class, new Long(id));
tx = session.getTransaction();
session.beginTransaction();
tx.commit();
return department;
}
config.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<context:component-scan base-package="com.xxx.controller" />
<mvc:annotation-driven />
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/test" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>com.xxx.model.Employee</value>
<value>com.xxx.model.Department</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
</props>
</property>
</bean>
<bean id="txManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="persistenceExceptionTranslationPostProcessor"
class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
<bean id="employeeDao" class="edu.am.amrita.trackapi.dao.EmployeeDaoImpl"></bean>
<bean id="employeeService" class="edu.am.amrita.trackapi.services.EmployeeServiceImpl"></bean>
<bean id="departmentDao" class="edu.am.amrita.trackapi.dao.DepartmentDaoImpl"></bean>
<bean id="departmentService" class="edu.am.amrita.trackapi.services.DepartmentServiceImpl"></bean>
</beans>
我已經用config xml文件內容更新了我的問題,請檢查它是否正確? –
問題是sessionFactory中缺少類名。非常感謝。 –
@Nayana_Das,總是鼓勵在annotatedClasses上使用packagesToScan並列出所有實體。這將減少您的配置文件負擔,您可能不需要在定義新實體時再次查看配置文件。我很高興它有幫助! –