title: save an object into database using hibernate
body: <p>I have a Model Object which named <code>Student_Info</code> in the package <code>com.hibernate</code>.</p>
  
  <p>In the same package I have a Main class where I created an instance of <code>Student_Info</code> :</p>
  
  <pre><code>public class Main {
  
      public static void main(String[] args) {
          Student_Info student = new Student_Info();
          student.setRollNo(1);
          student.setName("Ichigo");
  
          SessionFactory sessionFactory = 
                  new AnnotationConfiguration().configure().buildSessionFactory();
          Session session = sessionFactory.openSession();
          session.beginTransaction();
  
          session.save(student);
  
          session.getTransaction().commit();
          session.close();
          sessionFactory.close();
  
      }
  
  }
  </code></pre>
  
  <p>In the <code>hibernate.cfg.xml</code> file i have this :</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;!-- Database connection settings --&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/hibernatedb&lt;/property&gt;
          &lt;property name="connection.username"&gt;root&lt;/property&gt;
          &lt;property name="connection.password"&gt;toor&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.MySQLDialect&lt;/property&gt;
  
          &lt;!-- Enable Hibernate's automatic session context management --&gt;
          &lt;property name="current_session_context_class"&gt;thread&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;mapping resource="com.hibernate.Student_Info" /&gt;
      &lt;/session-factory&gt;
  &lt;/hibernate-configuration&gt;
  </code></pre>
  
  <p>But when I run the Main class I get this error :</p>
  
  <pre><code>Exception in thread "main" org.hibernate.MappingNotFoundException: resource: com.hibernate.Student_Info not found
      at org.hibernate.cfg.Configuration.addResource(Configuration.java:767)
      at org.hibernate.cfg.AnnotationConfiguration.addResource(AnnotationConfiguration.java:123)
      at org.hibernate.cfg.AnnotationConfiguration.addResource(AnnotationConfiguration.java:46)
      at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:2255)
      at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:2227)
      at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2207)
      at org.hibernate.cfg.AnnotationConfiguration.doConfigure(AnnotationConfiguration.java:213)
      at org.hibernate.cfg.AnnotationConfiguration.doConfigure(AnnotationConfiguration.java:46)
      at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2160)
      at org.hibernate.cfg.AnnotationConfiguration.doConfigure(AnnotationConfiguration.java:201)
      at org.hibernate.cfg.AnnotationConfiguration.doConfigure(AnnotationConfiguration.java:46)
      at org.hibernate.cfg.Configuration.configure(Configuration.java:2075)
      at org.hibernate.cfg.AnnotationConfiguration.configure(AnnotationConfiguration.java:183)
      at org.hibernate.cfg.AnnotationConfiguration.configure(AnnotationConfiguration.java:46)
      at org.hibernate.cfg.Configuration.configure(Configuration.java:2054)
      at org.hibernate.cfg.AnnotationConfiguration.configure(AnnotationConfiguration.java:177)
      at com.hibernate.Main.main(Main.java:16)
  </code></pre>
  
  <p>So what's wrong with what I wrote, I think all is set correctly.</p>
  
  <h1>Edit :</h1>
  
  <p>This is my project folder structure :</p>
  
  <p><img src="https://i.stack.imgur.com/JB6Zu.jpg" alt="enter image description here"></p>
  
  <h1>Edit 2:</h1>
  
  <p>This is the <code>Student_Info</code> class :</p>
  
  <pre><code>@Entity
  @Table(name="STUDENT_INFORMATION")
  public class Student_Info {
  
      @Id
      private int rollNo;
      private String name;
  
      public int getRollNo() {
          return rollNo;
      }
  
      public void setRollNo(int rollNo) {
          this.rollNo = rollNo;
      }
  
      public String getName() {
          return name;
      }
  
      public void setName(String name) {
          this.name = name;
      }
  
  
  
  }
  </code></pre>
  
  <h1>Edit 3 :</h1>
  
  <p>I created a file named <code>Student_Info.hbm.xml</code> in the package <code>com.hibernate</code> :</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"&gt;
  
  &lt;hibernate-mapping&gt;
      &lt;class name="com.hibernate.Student_Info" table="STUDENT_INFORMATION" catalog="hibernatedb"&gt;
          &lt;id name="rollNo" type="int"&gt;
              &lt;column name="rollNo" /&gt;
              &lt;generator class="assigned" /&gt;
          &lt;/id&gt;
          &lt;property name="name" type="string"&gt;
              &lt;column name="name" /&gt;
          &lt;/property&gt;
      &lt;/class&gt;
  &lt;/hibernate-mapping&gt;
  </code></pre>
  
  <p>And in the <code>hibernate.cfg.xml</code> I changed this line :</p>
  
  <pre><code>&lt;mapping resource="com.hibernate.Student_Info" /&gt;
  </code></pre>
  
  <p>With : </p>
  
  <pre><code>&lt;mapping resource="com.hibernate.Student_Info.hbm.xml" /&gt;
  </code></pre>
  
  <p>But when I run the Main class I get this error : </p>
  
  <pre><code>Exception in thread "main" org.hibernate.MappingNotFoundException: resource: com.hibernate.Student_Info.hbm.xml not found
      at org.hibernate.cfg.Configuration.addResource(Configuration.java:767)
      at org.hibernate.cfg.AnnotationConfiguration.addResource(AnnotationConfiguration.java:123)
      at org.hibernate.cfg.AnnotationConfiguration.addResource(AnnotationConfiguration.java:46)
      at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:2255)
      at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:2227)
      at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2207)
      at org.hibernate.cfg.AnnotationConfiguration.doConfigure(AnnotationConfiguration.java:213)
      at org.hibernate.cfg.AnnotationConfiguration.doConfigure(AnnotationConfiguration.java:46)
      at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2160)
      at org.hibernate.cfg.AnnotationConfiguration.doConfigure(AnnotationConfiguration.java:201)
      at org.hibernate.cfg.AnnotationConfiguration.doConfigure(AnnotationConfiguration.java:46)
      at org.hibernate.cfg.Configuration.configure(Configuration.java:2075)
      at org.hibernate.cfg.AnnotationConfiguration.configure(AnnotationConfiguration.java:183)
      at org.hibernate.cfg.AnnotationConfiguration.configure(AnnotationConfiguration.java:46)
      at org.hibernate.cfg.Configuration.configure(Configuration.java:2054)
      at org.hibernate.cfg.AnnotationConfiguration.configure(AnnotationConfiguration.java:177)
      at com.hibernate.Main.main(Main.java:16)
  </code></pre>