title: Error: Exception in thread "main" org.hibernate.MappingException: Unknown entity: com.hibernate.demo.model.Contact
body: <p>Before you mark it duplicate, Please read below :</p>
  
  <p>I have already tried all the given answers in similar Questions-</p>
  
  <ol>
  <li>My Import for @Entity is correct:import javax.persistence.Entity;</li>
  <li>Mapping for entity in hibernate.cfg.xml is present </li>
  <li>Class is mapped from pkg level:"com.hibernate.demo.model.Contact"</li>
  <li>AnnotationConfiguration is also not resolving the issue.</li>
  <li>All the other second to third best answers were tried too.</li>
  </ol>
  
  <p><strong>Background</strong> : I have created a Spring-boot project and I am trying to learn hibernate, I am using H2 db and I am facing >Unknown entity: com.hibernate.demo.model.Contact</p>
  
  <blockquote>
    <p>I have verified that <code>mapping</code> of the class is present in
    hibernate.cfg.xml</p>
  </blockquote>
  
  <pre><code>&lt;mapping class="com.hibernate.demo.model.Contact"/&gt;
  </code></pre>
  
  <blockquote>
    <p>I have also verified that mapping has full pkg level mapping path </p>
  </blockquote>
  
  <p>This is the my hibernate.cfg.xml</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://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"&gt;
  
  &lt;hibernate-configuration&gt;
  
      &lt;session-factory&gt;
  
          &lt;!-- Database connection settings --&gt;
          &lt;property name="connection.driver_class"&gt;org.h2.Driver&lt;/property&gt;
          &lt;property name="connection.url"&gt;jdbc:h2:./data/contactmgr&lt;/property&gt;
  
  
          &lt;property name="connection.username"&gt;sa&lt;/property&gt;
  
  
          &lt;property name="hibernate.default_schema"&gt;PUBLIC&lt;/property&gt;
  
          &lt;!-- JDBC connection pool (use the built-in) --&gt;
          &lt;property name="connection.pool_size"&gt;1&lt;/property&gt;
  
          &lt;!-- SQL dialect --&gt;
          &lt;property name="dialect"&gt;org.hibernate.dialect.H2Dialect&lt;/property&gt;
  
          &lt;!-- Disable the second-level cache  --&gt;
          &lt;property name="cache.provider_class"&gt;org.hibernate.cache.internal.NoCacheProvider&lt;/property&gt;
  
          &lt;!-- Echo all executed SQL to stdout --&gt;
          &lt;property name="show_sql"&gt;true&lt;/property&gt;
  
          &lt;!-- Drop and re-create the database schema on startup --&gt;
          &lt;property name="hbm2ddl.auto"&gt;create&lt;/property&gt;
  
          &lt;property name="show_sql"&gt;true&lt;/property&gt;
  
          &lt;mapping class="com.hibernate.demo.model.Contact"/&gt;
  
      &lt;/session-factory&gt;
  
  &lt;/hibernate-configuration&gt;
  </code></pre>
  
  <p>my Application.java Main class :-</p>
  
  <pre><code>package com.hibernate.demo;
  
  
  
  import java.io.IOException; import java.io.RandomAccessFile; import java.nio.channels.FileLock;
  
  import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.Configuration; import org.hibernate.service.ServiceRegistry;
  
  import com.hibernate.demo.model.Contact;
  
  public class Application {
  
      //Session factory
      private static final SessionFactory sessionFactory = buildSesssionFactory();
  
        public static void main(String[] args) throws Exception {
  
          System.out.println("Session factory");
          Contact contact = new Contact.ContactBuilder("Bob", "Marley").withEmail("bob.nik@gmail.com").withPhone(5859789733L).build();
  
          //Open a Session
          System.out.println("Open a Session");
          Session session = sessionFactory.openSession();
  
          //Begin a Transaction
          System.out.println("Begin a Transaction");
           session.beginTransaction();
  
  
          //Use the session to save the contact
          System.out.println("Use the session to save the contact");
          try{
          session.save(contact);
          } catch(Exception e) {
              // Close the session
              shutdown();
              throw e;
  
          }
          //Commit the transaction
          System.out.println("Commit the transaction");
          session.getTransaction().commit();
  
  
          // Close the session
          System.out.println("Close the session");
          session.close();
  
      }
  
     private static SessionFactory buildSessionFactory()
     {
  
        try
        {
           if (sessionFactory == null)
           {
              Configuration configuration = new Configuration().configure(Application.class.getResource("/hibernate.cfg.xml"));
              StandardServiceRegistryBuilder serviceRegistryBuilder = new StandardServiceRegistryBuilder();
              serviceRegistryBuilder.applySettings(configuration.getProperties());
              ServiceRegistry serviceRegistry = serviceRegistryBuilder.build();
              sessionFactory = configuration.buildSessionFactory(serviceRegistry);
           }
           return sessionFactory;
        } catch (Throwable ex)
        {
           System.err.println("Initial SessionFactory creation failed." + ex);
           throw new ExceptionInInitializerError(ex);
        }
     }
  
     public static SessionFactory getSessionFactory()
     {
        return sessionFactory;
     }
  
     public static void shutdown()
     {
        getSessionFactory().close();
     }
  
  }
  </code></pre>
  
  <p>I am doing all the right things, I have already checked that the </p>
  
  <blockquote>
    <p>@Entity import has right package</p>
  </blockquote>
  
  <pre><code>package com.hibernate.demo.model;
  
  import javax.persistence.Column;
  import javax.persistence.Entity;
  import javax.persistence.GeneratedValue;
  import javax.persistence.GenerationType;
  import javax.persistence.Id;
  
  @Entity
  public class Contact {
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private int id;
  
      @Column
      private String firstName;
  
      @Column
      private String lastName;
  
      @Column
      private String email;
  
      @Column
      private Long phone;
  
      public Contact() {
      }
  
      public Contact(ContactBuilder contactBuilder) {
          this.firstName = contactBuilder.firstName;
          this.lastName = contactBuilder.lastName;
          this.email = contactBuilder.email;
          this.phone=contactBuilder.phone;
      }
  
      public int getId() {
          return id;
      }
  
      public void setId(int id) {
          this.id = id;
      }
  
      public String getFirstName() {
          return firstName;
      }
  
      public void setFirstName(String firstName) {
          this.firstName = firstName;
      }
  
      public String getLastName() {
          return lastName;
      }
  
      public void setLastName(String lastName) {
          this.lastName = lastName;
      }
  
      public String getEmail() {
          return email;
      }
  
      public void setEmail(String email) {
          this.email = email;
      }
  
      public Long getPhone() {
          return phone;
      }
  
      public void setPhone(Long phone) {
          this.phone = phone;
      }
  
      @Override
      public String toString() {
          return "Contact [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", email=" + email
                  + ", phone=" + phone + "]";
      }
  
      public static class ContactBuilder {
  
          private String firstName;
  
          private String lastName;
  
          private String email;
  
          private Long phone;
  
          public ContactBuilder(String firstName, String lastName) {
              this.firstName = firstName;
              this.lastName = lastName;
          }
  
          public ContactBuilder withEmail(String email) {
              this.email = email;
              return this;
  
          }
  
          public ContactBuilder withPhone(Long phone) {
              this.phone = phone;
              return this;
  
          }
  
          public Contact build() {
  
              return new Contact(this);
          }
  
      }
  
  }
  </code></pre>