gpt4 book ai didi

mysql - org.hibernate.AnnotationException : @OneToOne or @ManyToOne on . .......引用未知实体:

转载 作者:行者123 更新时间:2023-11-29 19:21:03 27 4
gpt4 key购买 nike

我是 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 or @ManyToOne on ........ references unknown entity:......

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

知道我做错了什么吗?

最佳答案

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>

关于mysql - org.hibernate.AnnotationException : @OneToOne or @ManyToOne on . .......引用未知实体:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42445923/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com