title: org.hibernate.MappingException: Unknown entity: library.model.Person
body: <p>I've been struggling with this all day but haven't yet found a solution.</p>
  
  <p>I have a simple entity mapping to a database table:</p>
  
  <pre><code>@Entity
  @Table(name = "PERSON")
  public class Person implements Serializable {
  
  // Attributes.    
  @Id
  @Column(name = "ID", unique = true, nullable = false)    
  @GeneratedValue(strategy=GenerationType.AUTO)
  private Integer id;
  
  @Column(name = "NAME", nullable = false, length=50)      
  private String name;
  
  @Column(name = "ADDRESS", nullable = false, length=100)
  private String address;
  
  @Column(name = "TELEPHONE", nullable = false, length=10)
  private String telephone;
  
  @Column(name = "EMAIL", nullable = false, length=50)
  private String email;
  
  @OneToMany(cascade=CascadeType.ALL, fetch = FetchType.LAZY)
  @JoinColumn(name="PERSON_ID")   
  private List&lt;Book&gt; books;
  
  // Constructors.
  public Person() {
      this.id = 0;
      this.name = ("");
      this.address = ("");
      this.telephone = ("");
      this.email = ("");
      this.books = null;
  }
  </code></pre>
  
  <p>This entity is governed by a Spring controller with the following method:</p>
  
  <pre><code>@RequestMapping(value = "/register", method = RequestMethod.POST)
  public String post(@ModelAttribute("person") Person person, 
                     BindingResult bindingResult, 
                     SessionStatus sessionStatus,
                     Model model /* , 
                     HttpSession session */) throws DAOException {                  
      logger.info(PersonController.class.getName() + ".post() method called.");
  
      personValidator.validate(person, bindingResult);
      if (bindingResult.hasErrors()) {
          return "register";
      }
      else {
  
          // Write Person to database.
          personService.insert(person);
  
          // Set view.
          sessionStatus.setComplete();             
          return "options";            
      }
  } 
  </code></pre>
  
  <p>The service class is:</p>
  
  <pre><code>@Service("personService")
  @Transactional
  public class PersonService {
  
  @Autowired 
  PersonDAO personDAO;
  
  public void insert(Person person) throws DAOException {
      personDAO.insert(person);
  }
  </code></pre>
  
  <p>And the DAO is as follows:  </p>
  
  <pre><code>@Repository("PersonDAO")
  public class PersonDAOImpl implements PersonDAO {
  
  @Autowired
  private SessionFactory sessionFactory;
  static final Logger logger = Logger.getLogger(PersonDAOImpl.class.getName()); 
  
  @Override
  public void insert(Person person) throws DAOException {
      logger.info(PersonDAOImpl.class.getName() + ".insert() method called.");
  
      Session session = sessionFactory.openSession();
      Transaction transaction = session.getTransaction();
      try {
          transaction.begin();
          session.save(person);
          transaction.commit();
      }
      catch(RuntimeException e) {
          transaction.rollback();
          throw new DAOException(e);
      }
      finally {
          session.close();
      }
  }
  </code></pre>
  
  <p>And of course the part of my Spring servlet binding everything together:</p>
  
  <pre><code>&lt;bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"&gt;
      &lt;property name="dataSource" ref="dataSource"&gt;&lt;/property&gt;        
      &lt;property name="hibernateProperties"&gt;
          &lt;props&gt;
             &lt;prop key="hibernate.dialect"&gt;org.hibernate.dialect.DerbyDialect&lt;/prop&gt;
             &lt;prop key="hibernate.show_sql"&gt;true&lt;/prop&gt;
             &lt;prop key="hibernate.current_session_context_class "&gt;thread&lt;/prop&gt;
  
             &lt;!-- What to do with the database schema. --&gt;
             &lt;prop key="hbm2ddl.auto"&gt;validate&lt;/prop&gt;    
             &lt;!-- validate:    validate the schema, makes no changes to the database.
                  update:      update the schema.
                  create:      creates the schema, destroying previous data.
                  create-drop: drop the schema at the end of the session. --&gt;                 
          &lt;/props&gt;            
      &lt;/property&gt;                    
  &lt;/bean&gt;
  </code></pre>
  
  <p>So why am I getting the message:</p>
  
  <pre><code>org.hibernate.MappingException: Unknown entity: library.model.Person
  </code></pre>
  
  <p>If I try things like:</p>
  
  <pre><code>&lt;property name="packagesToScan" value="library.model" /&gt; 
  
  &lt;property name="annotatedClasses"&gt;
          &lt;list&gt;
              &lt;value&gt;library.model.Person&lt;/value&gt;
          &lt;/list&gt;
      &lt;/property&gt;   
  </code></pre>
  
  <p>At the moment I am getting:</p>
  
  <blockquote>
    <p>Jun 15, 2014 10:50:58 PM org.apache.catalina.core.ApplicationContext
    log INFO: Initializing Spring root WebApplicationContext Jun 15, 2014
    10:51:02 PM org.apache.catalina.core.StandardContext listenerStart
    SEVERE: Exception sending context initialized event to listener
    instance of class
    org.springframework.web.context.ContextLoaderListener
    org.springframework.beans.factory.BeanCreationException: Error
    creating bean with name 'personController': Injection of autowired
    dependencies failed; nested exception is
    org.springframework.beans.factory.BeanCreationException: Could not
    autowire field: library.services.PersonService
    library.controller.PersonController.personService; nested exception is
    org.springframework.beans.factory.BeanCreationException: Error
    creating bean with name 'personService': Injection of autowired
    dependencies failed; nested exception is
    org.springframework.beans.factory.BeanCreationException: Could not
    autowire field: library.dao.PersonDAOImpl
    library.services.PersonService.personDAO; nested exception is
    org.springframework.beans.factory.BeanCreationException: Error
    creating bean with name 'PersonDAOImpl': Injection of autowired
    dependencies failed; nested exception is
    org.springframework.beans.factory.BeanCreationException: Could not
    autowire field: org.hibernate.SessionFactory
    library.dao.PersonDAOImpl.sessionFactory; nested exception is
    org.springframework.beans.factory.BeanCreationException: Error
    creating bean with name 'sessionFactory' defined in ServletContext
    resource [/WEB-INF/library-servlet.xml]: Invocation of init method
    failed; nested exception is java.lang.NoClassDefFoundError:
    javax/persistence/NamedStoredProcedureQuery   at
    org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:292)
      at
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1185)
      at
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
      at
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
      at
    org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)
      at
    org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
      at
    org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
      at
    org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
      at
    org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:700)
      at
    org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)
      at
    org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
      at
    org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403)
      at
    org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306)
      at
    org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)
      at
    org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4681)
      at
    org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5184)
      at
    org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5179)
      at java.util.concurrent.FutureTask.run(FutureTask.java:266)     at
    java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
      at
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
      at java.lang.Thread.run(Thread.java:744)</p>
  </blockquote>