org.hibernate.AnnotationException:@OneToOne或@ManyToOne ........引用未知实体:

时间:2017-02-24 18:33:43

标签: mysql spring hibernate spring-mvc

我是Spring-Hibernate技术的新手,同时将Spring MVC与Hibernate集成在一起,用于两个类的关系。以下是代码:

用户类:

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 on ........   引用未知实体:......

到目前为止,stackoverflow上没有其他解决方案对我有效。

知道我做错了吗?

1 个答案:

答案 0 :(得分: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>