title: org.springframework.orm.hibernate3.HibernateSystemException: Unknown entity
body: <p>This is a spring mvc application using IntelliJ.</p>
  
  <p>I'm getting this error:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>Here is my setup, hibernate.cfg.xml:</p>
  
  <pre><code>&lt;!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"&gt;
  
  &lt;hibernate-configuration&gt;
      &lt;session-factory&gt;
  
          &lt;mapping package="com.testproj1.core.model"/&gt;
  
          &lt;mapping class="com.testproj1.core.model.User"/&gt;
  
  
      &lt;/session-factory&gt;
  
  &lt;/hibernate-configuration&gt;
  </code></pre>
  
  <p>My servlet-context.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;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"&gt;
  
      &lt;!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure --&gt;
  
      &lt;!-- Scans within the base package of the application for @Components to configure as beans --&gt;
      &lt;!-- @Controller, @Service, @Configuration, etc. --&gt;
      &lt;context:component-scan base-package="com.testproj1" /&gt;
  
      &lt;!-- Enables the Spring MVC @Controller programming model --&gt;
      &lt;mvc:annotation-driven /&gt;
  
  
  
       &lt;bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"&gt;
          &lt;property name="driverClassName" value="com.mysql.jdbc.Driver"/&gt;
          &lt;property name="url" value="jdbc:mysql://localhost/testproj1"/&gt;
          &lt;property name="username" value="root"/&gt;
          &lt;property name="password" value="123"/&gt;
          &lt;property name="maxActive" value="100"/&gt;
          &lt;property name="maxIdle" value="30"/&gt;
          &lt;property name="maxWait" value="1000"/&gt;
          &lt;property name="defaultAutoCommit" value="true"/&gt;
          &lt;property name="removeAbandoned" value="true"/&gt;
          &lt;property name="removeAbandonedTimeout" value="60"/&gt;
      &lt;/bean&gt;
  
  
      &lt;!-- Hibernate SessionFactory --&gt;
      &lt;bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"&gt;
          &lt;property name="dataSource" ref="dataSource"/&gt;
          &lt;!--&lt;property name="packagesToScan" value="com.testproj1.core.model"/&gt;--&gt;
          &lt;property name="configLocation" value="/WEB-INF/config/hibernate.cfg.xml"/&gt;
          &lt;property name="hibernateProperties"&gt;
              &lt;value&gt;
                  hibernate.dialect=org.hibernate.dialect.MySQLDialect
                  hibernate.query.substitutions=true 'Y', false 'N'
                  hibernate.show_sql=true
              &lt;/value&gt;
          &lt;/property&gt;
          &lt;property name="annotatedClasses"&gt;
              &lt;list&gt;
                  &lt;value&gt;com.testproj1.core.model.User&lt;/value&gt;
              &lt;/list&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  
      &lt;!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) --&gt;
  
  
      &lt;bean id="userDao" class="com.testproj1.core.doa.UserDaoImpl"&gt;
          &lt;property name="sessionFactory" ref="sessionFactory"/&gt;
      &lt;/bean&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>My controller:</p>
  
  <pre><code>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";
      }
  }
  </code></pre>
  
  <p>UserServiceImpl:</p>
  
  <pre><code>@Service
  public class UserServiceImpl implements UserService {
  
      @Autowired
      UserDao userDao;
  
      public User getUser(int id) {
          return userDao.get(id);
      }
  
  }
  </code></pre>
  
  <p>UserDaoImpl:</p>
  
  <pre><code>public class UserDaoImpl extends GenericDaoImpl&lt;User, Integer&gt; 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);
      }
  
  }
  </code></pre>
  
  <p>User model:</p>
  
  <pre><code>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;
      }
  
  }
  </code></pre>
  
  <p>Now it is obvious it is not picking up my User model for some reason.</p>
  
  <p>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.</p>
  
  <p>I also have the annotatedClasses property in the sessionFactory bean tag.</p>
  
  <p>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?</p>