【问题标题】:Insert into table with foreign key using Hibernate DAO method使用 Hibernate DAO 方法插入带有外键的表
【发布时间】:2017-11-18 18:21:12
【问题描述】:

在这里,我正在尝试使用休眠向具有外键的表中插入一行。但我得到以下异常:“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;
}

}

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

【问题讨论】:

    标签: mysql spring hibernate foreign-keys


    【解决方案1】:

    请检查您的休眠实体是否正在被扫描。您可以将它们配置到 hibernate.cfg.xml 或通过

    packagesToScan
    

    SessionFactory 的属性。

    希望这会有所帮助!

    【讨论】:

    • 我已经用config xml文件内容更新了我的问题,请检查是否正确?
    • 问题是 sessionFactory 中缺少类名。非常感谢。
    • @Nayana_Das,总是鼓励在 annotatedClasses 上使用 packagesToScan 并列出所有实体。这将减少您的配置文件负担,并且您在定义新实体时可能不需要再次查看配置文件。我很高兴它有帮助!
    【解决方案2】:

    在 Department pojo 中,Employee 映射应该如下所示,带有 setter 和 getter

    @ManyToOne
    @JoinColumn(name = "id")
    private Employee  emp;
    

    【讨论】:

    • Department类也需要加入列吗?因为我已经在 Employee 类中给出了 join 列,例如 @ManyToOne @JoinColumn(name="department_id") private Department department;
    • 如果要进行双向映射,这是必需的。实体可以通过单向映射来识别。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-03
    • 2017-08-27
    • 1970-01-01
    • 1970-01-01
    • 2017-08-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多