2017-02-24 9 views
0

Spring MVCとHibernateを2つのクラスの関係で統合する際に、Spring-Hibernate技術に直面することに新しくなりました。org.hibernate.AnnotationException:@OneToOneまたは@ManyToOne on ........参照不明のエンティティ:

Userクラス:

package com.MVCHibernate.Model; 

import java.util.List; 
import java.util.Set; 

import javax.persistence.Entity; 
import javax.persistence.GeneratedValue; 
import javax.persistence.Id; 
import javax.persistence.OneToMany; 
import javax.persistence.Table; 

@Entity 
@Table(name = "EMP_USERS") 
public class UserModel { 

    @Id 
    @GeneratedValue 
    private int id; 
    private String username; 
    private String password; 
    private String role; 

    @OneToMany(mappedBy="emp_users") 
    private List<PermanentEmployeeModel> permanentEmployeeModel; 

    public int getId() { 
     return id; 
    } 

    public void setId(int id) { 
     this.id = id; 
    } 

    public String getUsername() { 
     return username; 
    } 
    public void setUsername(String username) { 
     this.username = username; 
    } 
    public String getPassword() { 
     return password; 
    } 
    public void setPassword(String password) { 
     this.password = password; 
    } 
    public String getRole() { 
     return role; 
    } 
    public void setRole(String role) { 
     this.role = role; 
    } 

    public List<PermanentEmployeeModel> getPermanentEmployeeModel() { 
     return permanentEmployeeModel; 
    } 

    public void setPermanentEmployeeModel(List<PermanentEmployeeModel> permanentEmployeeModel) { 
     this.permanentEmployeeModel = permanentEmployeeModel; 
    } 
} 


The PermanentEmployeeModel class: 

package com.MVCHibernate.Model; 

import javax.persistence.*; 

@Entity 
@Table(name = "EMP_PERMANENT") 
public class PermanentEmployeeModel{ 

    @Id 
    @GeneratedValue 
    private int eid; 
    private int fname; 
    private int lname; 
    private int salary; 
    private String grade; 
    private int date; 

    // ManyToOne relationship 

    @ManyToOne 
    @JoinColumn(name = "id") 
    private UserModel emp_user; 


    public int getEid() { 
     return eid; 
    } 
    public void setEid(int eid) { 
     this.eid = eid; 
    } 
    public int getFname() { 
     return fname; 
    } 
    public void setFname(int fname) { 
     this.fname = fname; 
    } 
    public int getLname() { 
     return lname; 
    } 
    public void setLname(int lname) { 
     this.lname = lname; 
    } 
    public int getSalary() { 
     return salary; 
    } 
    public void setSalary(int salary) { 
     this.salary = salary; 
    } 
    public String getGrade() { 
     return grade; 
    } 
    public void setGrade(String grade) { 
     this.grade = grade; 
    } 
    public int getDate() { 
     return date; 
    } 
    public void setDate(int date) { 
     this.date = date; 
    } 
    public UserModel getEmp_user() { 
     return emp_user; 
    } 
    public void setEmp_user(UserModel emp_user) { 
     this.emp_user = emp_user; 
    } 
} 

従業員-servlet.xml:以下のコードされているコードを実行している間

<context:component-scan base-package="com.MVCHibernate" /> 
<mvc:annotation-driven /> 
<bean 
    class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
    <property name="prefix" value="/WEB-INF/Files/" /> 
    <property name="suffix" value=".jsp" /> 
</bean> 

<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/EmployeeMVCHibernate" /> 
    <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.MVCHibernate.Model.UserModel</value> 
     </list> 
    </property> 
    <property name="hibernateProperties"> 
     <props> 
      <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> 
      <prop key="hibernate.show_sql">true</prop> 
      <prop key="hibernate.hbm2ddl.auto">create</prop> 
     </props> 
    </property> 
</bean> 

<!-- PERMANENT EMPLOYEE --> 
<bean id="sessionFactory1" 
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 
    <property name="dataSource" ref="dataSource" /> 
    <property name="annotatedClasses"> 
     <list> 
      <value>com.MVCHibernate.Model.PermanentEmployeeModel</value> 
     </list> 
    </property> 
    <property name="hibernateProperties"> 
     <props> 
      <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> 
      <prop key="hibernate.show_sql">true</prop> 
      <prop key="hibernate.hbm2ddl.auto">create</prop> 
     </props> 
    </property> 
</bean> 

<bean id="txManager" 
    class="org.springframework.orm.hibernate4.HibernateTransactionManager"> 
    <property name="sessionFactory" ref="sessionFactory" /> 
</bean> 

<!-- USER BEANS --> 
<!-- <bean id="userDAOImpl" class="com.MVCHibernate.DAO.UserDAOImpl" /> --> 
<bean id="userDAOImpl" class="com.MVCHibernate.DAO.UserDAOImpl"> 
<property name="sessionFactory" ref = "sessionFactory"></property> 
</bean> 
<bean id="userServiceImpl" class="com.MVCHibernate.Service.UserServiceImpl" /> 


<!-- EMPLOYEE BEANS --> 
<!-- <bean id="pempDAOImpl" class="com.MVCHibernate.DAO.PermanentEmployeeDAOImpl" /> --> 
<bean id="pempDAOImpl" class="com.MVCHibernate.DAO.PermanentEmployeeDAOImpl"> 
<property name="sessionFactory" ref = "sessionFactory1"></property> 
</bean> 
<bean id="pempServiceImpl" class="com.MVCHibernate.Service.PermanentEmployeeServiceImpl" /> 

それは次のようなエラーが表示さ:

org.hibernate.AnnotationException:........ 参照の@OneToOneまたは@ManyToOne不明なエンティティ:......

stackoverflow上の他のソリューションはので、私のために働いていません遠い

私は間違っていますか?

+0

以下はファイルです: –

+0

私はmappedByがテーブル名の代わりにクラス名を必要とすると信じています。あなたはinitの大文字小文字でクラス名に変更することができます。 –

+0

私はemployee-servlet.xmlに変更を加える必要がありますか? –

答えて

0
This is working now. 
Need to include the mapping resource into the employee-servlet.xml file 
**<import resource="classpath*:hibernate.cfg.xml"/>** 

hibernate.cfg.xml: 

<?xml version="1.0" encoding="utf-8"?> 
<!DOCTYPE hibernate-configuration SYSTEM "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> 
<hibernate-configuration> 
    <session-factory> 
     <mapping class="com.MVCHibernate.Model.UserModel"></mapping> 
     <mapping class="com.MVCHibernate.Model.PermanentEmployeeModel"></mapping> 
    </session-factory> 
</hibernate-configuration> 
関連する問題