我试图在具有外键的表中插入一行使用hibernate。但是我得到以下异常:" org.hibernate.MappingException:未知实体:com.xxx.model.Employee"。我该如何解决这个问题?以下是我的代码:
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>
答案 0 :(得分:1)
请检查您的休眠实体是否正在被扫描。您可以将它们配置为hibernate.cfg.xml或通过
packagesToScan
SessionFactory的属性。
希望这有帮助!
答案 1 :(得分:0)
在Department pojo中,Employee映射应该如下所示,使用setter和getter
@ManyToOne
@JoinColumn(name = "id")
private Employee emp;