2017-06-16 12 views
0

ここでは、外部キーを持つテーブルに行を挿入しようとしています。しかし、私は次の例外が発生します: "org.hibernate.MappingException:不明なエンティティ:com.xxx.model.Employee"。どうすればこの問題を解決できますか? Department.javaHibernate DAOメソッドを使用して外部キーを使用してテーブルに挿入

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

} 

:EmployeeDaoImplで

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

は、新しい従業員を追加するには
@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; 
    } 
    } 

Employee.java:私のコードで、次の

@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:部門POJOで

<?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> 

答えて

1

で、以下に示すように、従業員のマッピングが存在するかどうか確認してください休止状態のエンティティがスキャンされています。 hibernate.cfg.xmlに設定するか、

packagesToScan 

というSessionFactory属性を設定することができます。

希望すると便利です。

+0

私はconfig xmlファイルの内容で私の質問を更新しましたか、それが正しいかどうかをチェックしてください? –

+0

sessionFactoryに問題がクラス名がありませんでした。どうもありがとう。 –

+0

@ Nayana_Dasだから、annotatedClassesに対してpackagesToScanを使用し、すべてのエンティティを一覧表示することが常に推奨されます。これにより、設定ファイルの負担が軽減され、新しいエンティティを定義するときに設定ファイルを再度確認する必要がなくなる可能性があります。私はそれが助けてうれしいです! –

0

セッターとゲッター

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

Departmentクラスの結合列も与えなければなりませんか? Bcoz私はすでに@ManyToOne @ JoinColumn(name = "department_id")私立学科のようなEmployeeクラスのjoinカラムを与えています。 –

+0

これは、双方向マッピングを実行する場合に必要です。エンティティは単方向マッピングで認識できます。 –

関連する問題