这是一个使用IntelliJ的Spring MVC应用程序。
我得到这个错误:
org.springframework.orm.hibernate3.HibernateSystemException: Unknown entity: com.testproj1.core.model.User; nested exception is org.hibernate.MappingException: Unknown entity: com.testproj1.core.model.User
org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:679)
org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:412)
org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:411)
org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
org.springframework.orm.hibernate3.HibernateTemplate.get(HibernateTemplate.java:512)
org.springframework.orm.hibernate3.HibernateTemplate.get(HibernateTemplate.java:506)
com.testproj1.core.doa.UserDaoImpl.get(UserDaoImpl.java:17)
com.testproj1.core.service.UserServiceImpl.getUser(UserServiceImpl.java:22)
com.testproj1.web.controllers.HomeController.index(HomeController.java:19)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)
org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:436)
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:424)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:669)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:574)
javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
root cause
org.hibernate.MappingException: Unknown entity: com.testproj1.core.model.User
org.hibernate.impl.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:701)
org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:92)
org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:1080)
org.hibernate.impl.SessionImpl.get(SessionImpl.java:997)
org.hibernate.impl.SessionImpl.get(SessionImpl.java:990)
org.springframework.orm.hibernate3.HibernateTemplate$1.doInHibernate(HibernateTemplate.java:519)
org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:406)
org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
org.springframework.orm.hibernate3.HibernateTemplate.get(HibernateTemplate.java:512)
org.springframework.orm.hibernate3.HibernateTemplate.get(HibernateTemplate.java:506)
com.testproj1.core.doa.UserDaoImpl.get(UserDaoImpl.java:17)
com.testproj1.core.service.UserServiceImpl.getUser(UserServiceImpl.java:22)
com.testproj1.web.controllers.HomeController.index(HomeController.java:19)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)
org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:436)
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:424)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:669)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:574)
javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
testproj1
下面是我的设置,hib.cfg.xml:
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<mapping package="com.testproj1.core.model"/>
<mapping class="com.testproj1.core.model.User"/>
</session-factory>
</hibernate-configuration>
我的servlet上下文.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Scans within the base package of the application for @Components to configure as beans -->
<!-- @Controller, @Service, @Configuration, etc. -->
<context:component-scan base-package="com.testproj1" />
<!-- Enables the Spring MVC @Controller programming model -->
<mvc:annotation-driven />
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost/testproj1"/>
<property name="username" value="root"/>
<property name="password" value="123"/>
<property name="maxActive" value="100"/>
<property name="maxIdle" value="30"/>
<property name="maxWait" value="1000"/>
<property name="defaultAutoCommit" value="true"/>
<property name="removeAbandoned" value="true"/>
<property name="removeAbandonedTimeout" value="60"/>
</bean>
<!-- Hibernate SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!--<property name="packagesToScan" value="com.testproj1.core.model"/>-->
<property name="configLocation" value="/WEB-INF/config/hibernate.cfg.xml"/>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.query.substitutions=true 'Y', false 'N'
hibernate.show_sql=true
</value>
</property>
<property name="annotatedClasses">
<list>
<value>com.testproj1.core.model.User</value>
</list>
</property>
</bean>
<!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) -->
<bean id="userDao" class="com.testproj1.core.doa.UserDaoImpl">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
</beans>
我的控制器:
public class HomeController {
@Autowired
private UserService userService;
@RequestMapping("/")
public String index() {
System.out.println("hello, world!");
User user = userService.getUser(1);
System.out.println("Username is: " + user.getUsername());
return "WEB-INF/views/home.jsp";
}
}
用户服务实现:
@Service
public class UserServiceImpl implements UserService {
@Autowired
UserDao userDao;
public User getUser(int id) {
return userDao.get(id);
}
}
用户道实现:
public class UserDaoImpl extends GenericDaoImpl<User, Integer> implements UserDao {
public User get(int id) {
User user = super.getHibernateTemplate().get(User.class, id);
return user;
}
public void addUser(User user)
{
super.getHibernateTemplate().save(user);
}
}
用户模型:
package com.testproj1.core.model;
import org.hibernate.annotations.Entity;
import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="users")
public class User {
protected Integer id;
protected String user_name;
@Id
public Integer getId() {
return id;
}
@Column
public String getUsername() {
return user_name;
}
public void setId(Integer id) {
this.id = id;
}
public void setUsername(String user_name) {
this.user_name = user_name;
}
}
现在很明显,由于某种原因,它没有选择我的用户模型。
我感到困惑的是,我确实设置了它,以便spring在component-scan标记中的servlet-context.xml中自动扫描jar文件(至少我认为是这样)。
我还在sessionFactory bean标记中拥有annotatedClasses属性。
我注解掉了sessionFactory bean标记中的packagesToScan,因为我读到这个错误可能是因为循环引用,我认为这意味着它被连接了很多次?
3条答案
按热度按时间2nc8po8w1#
使用
javax.persistence.Entity
而不是org.hibernate.annotations.Entity
。ivqmmu1c2#
还可以使用“javax.persistence.Id”@Id作为类标识符,即主键。
t5zmwmid3#
Hibernate模板无法找到带注解的实体类。如果实体类未在配置设置中声明,则会发生这种情况。因此,请将其包含为: