title: org.hibernate.MappingException: Unknown entity: annotations.Users
body: <p>Consider the hierarchy : </p>
  
  <p><img src="https://i.stack.imgur.com/wwjQG.gif" alt="enter image description here"></p>
  
  <p>And the following classes and xml : </p>
  
  <p><strong>HibernateUtil.java</strong></p>
  
  <pre><code>package annotations;
  
  import org.hibernate.SessionFactory;
  import org.hibernate.cfg.Configuration;
  import org.hibernate.service.ServiceRegistry;
  import org.hibernate.service.ServiceRegistryBuilder;
  
  
  /**
   * 
   * @author X3
   *
   */
  public class HibernateUtil {
  
      private static final SessionFactory sessionFactory = buildSessionFactory();
      private static final String HIBERNATE_CFG = "hibernateAnnotations.cfg.xml";
  
      private static SessionFactory buildSessionFactory() 
      {
          Configuration cfg = new Configuration().addResource(HIBERNATE_CFG).configure();
          ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().
                  applySettings(cfg.getProperties()).buildServiceRegistry();
          SessionFactory sessionFactory = cfg.buildSessionFactory(serviceRegistry);
          return sessionFactory;
      }
  
      public static SessionFactory getSessionFactory() {
          return sessionFactory;
      }
  }
  </code></pre>
  
  <p><strong>Users.java</strong></p>
  
  <pre><code>package annotations;
  
  import javax.persistence.Column;
  import javax.persistence.Entity;
  import javax.persistence.GeneratedValue;
  import javax.persistence.Id;
  import javax.persistence.Table;
  import java.sql.Timestamp;
  
  @Entity
  @Table(name = "Users")
  public class Users {
  
      @Id
      @GeneratedValue
      @Column(name = "USER_ID")
      private long userID;
  
      @Column(name = "USERNAME", nullable = false, length = 100)
      private String username;
  
      @Column(name = "MessageTimeDate", nullable = false)
      private java.sql.Timestamp datetime;
  
      @Column(name = "UserMessage", nullable = false)
      private String message;
  
  
  
      public Users(String username , String message)
      {
          java.util.Date date = new java.util.Date();
          this.datetime = new Timestamp(date.getTime());
  
          this.username = username;
          this.message = message;
      }
  
      public long getUserID() {
          return userID;
      }
  
      public void setUserID(long userID) {
          this.userID = userID;
      }
  
      public String getUsername() {
          return username;
      }
  
      public void setUsername(String username) {
          this.username = username;
      }
  
      public java.sql.Timestamp getDatetime() {
          return datetime;
      }
  
      public void setDatetime(java.sql.Timestamp datetime) {
          this.datetime = datetime;
      }
  
      public String getMessage() {
          return message;
      }
  
      public void setMessage(String message) {
          this.message = message;
      }
  
  }
  </code></pre>
  
  <p><strong>Main.java</strong> </p>
  
  <pre><code>package annotations;
  
  import org.hibernate.Session;
  import org.hibernate.SessionFactory;
  
  public class Main {
  
      public static void main(String[] args) {
  
          try
          {
                 SessionFactory sf = HibernateUtil.getSessionFactory();
                 Session session = sf.openSession();
                 session.beginTransaction();
  
                 Users user1 = new Users("Jack" , "Hello");
                 session.save(user1);
                 session.getTransaction().commit();
                 session.close();
          }
  
          catch (Exception e)
          {
              System.out.println(e.toString());
              e.getStackTrace();
          }
  
  
      }
  }
  </code></pre>
  
  <p>And <strong>hibernateAnnotations.cfg.xml</strong></p>
  
  <pre><code>&lt;?xml version='1.0' encoding='utf-8'?&gt;
  &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;property name="connection.driver_class"&gt;com.mysql.jdbc.Driver&lt;/property&gt;
          &lt;property name="connection.url"&gt;jdbc:mysql://localhost:3306/CHAT_DB&lt;/property&gt;
          &lt;property name="connection.username"&gt;root&lt;/property&gt;
          &lt;property name="connection.password"&gt;root&lt;/property&gt;
          &lt;property name="connection.pool_size"&gt;1&lt;/property&gt;
          &lt;property name="dialect"&gt;org.hibernate.dialect.MySQLDialect&lt;/property&gt;
          &lt;property name="current_session_context_class"&gt;thread&lt;/property&gt;
          &lt;property name="cache.provider_class"&gt;org.hibernate.cache.NoCacheProvider&lt;/property&gt;
          &lt;property name="show_sql"&gt;true&lt;/property&gt;
          &lt;property name="hbm2ddl.auto"&gt;create&lt;/property&gt;
          &lt;mapping class="annotations.Users"&gt;&lt;/mapping&gt;       
      &lt;/session-factory&gt;
  &lt;/hibernate-configuration&gt;
  </code></pre>
  
  <p>When I run main(), the following error appears in output window : </p>
  
  <pre><code>org.hibernate.MappingException: Unknown entity: annotations.Users
  </code></pre>
  
  <p>But the entity <code>Users</code> is in <code>annotations</code> package , so what's wrong ? </p>