title: org.hibernate.MappingException: Unknown entity:
body: <p>I really want to understand what is going on with my code.</p>
  
  <p>I have a standalone application which uses spring and Hibernate as JPA and I am trying to run the test using a single main Class</p>
  
  <p>My main class</p>
  
  <pre><code>package edu.acct.tsegay.common;
  
  import edu.acct.tsegay.model.User;
  import edu.acct.tsegay.business.IUserBusinessObject;
  
  import org.springframework.context.ApplicationContext;
  import org.springframework.context.support.ClassPathXmlApplicationContext;
  
  public class App {
      public static void main(String[] args) {
          try {
              ApplicationContext context = new ClassPathXmlApplicationContext(
                      "Spring3AndHibernate-servlet.xml");
              IUserBusinessObject userBusinessObject = (IUserBusinessObject) context
                      .getBean("userBusiness");
  
              User user = (User) context.getBean("user1");
              user.setPassword("pass");
              user.setUsername("tsegay");
              System.out.println(user.getPassword());
  
              userBusinessObject.delete(user);
  
              User user2 = new User();
              user2.setUsername("habest");
              user2.setPassword("pass1");
              System.out.println(user2.getPassword());
              /*
               * userBusinessObject.save(user2);
               * 
               * User user3 = userBusinessObject.searchUserbyId("tsegay");
               * System.out.println("Search Result: " + user3.getUsername());
               */
              System.out.println("Success");
          } catch (Exception e) {
              e.printStackTrace();
          }
  
      }
  
  }
  </code></pre>
  
  <p>my application context is:</p>
  
  <pre><code>&lt;beans xmlns="http://www.springframework.org/schema/beans"
  
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  
      xsi:schemaLocation="http://www.springframework.org/schema/beans
  
  http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"&gt;
  
      &lt;!-- data source --&gt;
      &lt;bean id="dataSource"
          class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt;
          &lt;property name="driverClassName" value="com.mysql.jdbc.Driver" /&gt;
          &lt;property name="url" value="jdbc:mysql://localhost:3306/test" /&gt;
          &lt;property name="username" value="test" /&gt;
          &lt;property name="password" value="password" /&gt;
      &lt;/bean&gt;
  
      &lt;!-- session factory --&gt;
      &lt;bean id="sessionFactory"
          class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"&gt;
  
          &lt;property name="dataSource"&gt;
              &lt;ref bean="dataSource" /&gt;
  
          &lt;/property&gt;
  
          &lt;property name="hibernateProperties"&gt;
  
              &lt;props&gt;
                  &lt;prop key="hibernate.dialect"&gt;org.hibernate.dialect.MySQLDialect&lt;/prop&gt;
                  &lt;prop key="hibernate.show_sql"&gt;true&lt;/prop&gt;
              &lt;/props&gt;
          &lt;/property&gt;
  
  
  
      &lt;/bean&gt;
  
      &lt;!-- exposed person business object --&gt;
      &lt;bean id="userBusiness" class="edu.acct.tsegay.business.UserBusinessObject"&gt;
          &lt;property name="userDao" ref="userDao" /&gt;
  
      &lt;/bean&gt;
      &lt;bean id="user1" class="edu.acct.tsegay.model.User"&gt;
          &lt;property name="username" value="tse" /&gt;
          &lt;property name="password" value="pass" /&gt;
      &lt;/bean&gt;
  
  
      &lt;!-- Data Access Object --&gt;
      &lt;bean id="userDao" class="edu.acct.tsegay.dao.UserDao"&gt;
          &lt;property name="sessionFactory" ref="sessionFactory" /&gt;
      &lt;/bean&gt;
  
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>My User Model is:</p>
  
  <pre><code>package edu.acct.tsegay.model;
  
  import java.io.Serializable;
  
  import javax.persistence.Entity;
  import javax.persistence.Id;
  import javax.persistence.Version;
  
  import org.hibernate.annotations.NaturalId;
  
  @Entity
  public class User implements Serializable {
      /**
       * 
       */
      private static final long serialVersionUID = 1L;
      private String username;
      private String password;
      private Integer VERSION;
  
      @Version
      public Integer getVERSION() {
          return VERSION;
      }
  
      public void setVERSION(Integer vERSION) {
          VERSION = vERSION;
      }
      @NaturalId
      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;
      }
  
  }
  </code></pre>
  
  <p>My DAO is:</p>
  
  <pre><code>package edu.acct.tsegay.dao;
  
  import edu.acct.tsegay.model.User;
  
  import org.hibernate.SessionFactory;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.orm.hibernate3.HibernateTemplate;
  
  import org.springframework.stereotype.Repository;
  
  @Repository
  public class UserDao implements IUserDao {
      private SessionFactory sessionFactory;
  
      private HibernateTemplate hibernateTemplate;
  
      public SessionFactory getSessionFactory() {
          return sessionFactory;
      }
      @Autowired
      public void setSessionFactory(SessionFactory sessionFactory) {
          this.sessionFactory = sessionFactory;
          this.hibernateTemplate = new HibernateTemplate(sessionFactory);
      }
  
      public void save(User user) {
          // TODO Auto-generated method stub
          // getHibernateTemplate().save(user);
          this.hibernateTemplate.save(user);
      }
  
      public void delete(User user) {
          // TODO Auto-generated method stub
          this.hibernateTemplate.delete(user);
      }
  
      public User searchUserbyId(String username) {
          // TODO Auto-generated method stub
          return this.hibernateTemplate.get(User.class, username);
      }
  
  }
  </code></pre>
  
  <p>And this my stacktrace error when  i run the program:</p>
  
  <pre><code>pass
  org.springframework.orm.hibernate3.HibernateSystemException: Unknown entity: edu.acct.tsegay.model.User; nested exception is org.hibernate.MappingException: Unknown entity: edu.acct.tsegay.model.User
      at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:679)
      at org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:412)
      at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:411)
      at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
      at org.springframework.orm.hibernate3.HibernateTemplate.delete(HibernateTemplate.java:837)
      at org.springframework.orm.hibernate3.HibernateTemplate.delete(HibernateTemplate.java:833)
      at edu.acct.tsegay.dao.UserDao.delete(UserDao.java:34)
      at edu.acct.tsegay.business.UserBusinessObject.delete(UserBusinessObject.java:30)
      at edu.acct.tsegay.common.App.main(App.java:23)
  Caused by: org.hibernate.MappingException: Unknown entity: edu.acct.tsegay.model.User
      at org.hibernate.impl.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:580)
      at org.hibernate.impl.SessionImpl.getEntityPersister(SessionImpl.java:1365)
      at org.hibernate.event.def.DefaultDeleteEventListener.onDelete(DefaultDeleteEventListener.java:100)
      at org.hibernate.event.def.DefaultDeleteEventListener.onDelete(DefaultDeleteEventListener.java:74)
      at org.hibernate.impl.SessionImpl.fireDelete(SessionImpl.java:793)
      at org.hibernate.impl.SessionImpl.delete(SessionImpl.java:771)
      at org.springframework.orm.hibernate3.HibernateTemplate$25.doInHibernate(HibernateTemplate.java:843)
      at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:406)
      ... 6 more
  </code></pre>