Issue
This is a spring mvc application using IntelliJ.
I'm getting this error:
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
Here is my setup, hibernate.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>
My servlet-context.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>
My controller:
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";
}
}
UserServiceImpl:
@Service
public class UserServiceImpl implements UserService {
@Autowired
UserDao userDao;
public User getUser(int id) {
return userDao.get(id);
}
}
UserDaoImpl:
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);
}
}
User model:
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;
}
}
Now it is obvious it is not picking up my User model for some reason.
What I'm confused is, I did set it so spring would automatically scan the jar for this (at least I think) in the servlet-context.xml in the component-scan tag.
I also have the annotatedClasses property in the sessionFactory bean tag.
I commented out the packagesToScan in the sessionFactory bean tag as I was reading that this error could be because of circular references which I think means it is being wired up to many times?
Solution
Use javax.persistence.Entity
rather than org.hibernate.annotations.Entity
.
Answered By - Bozho
Answer Checked By - Marie Seifert (JavaFixing Admin)