title: Hibernate no arg constructor
body: <p>I was going through the hibernate documentation and the document says that hibernate requires a no-arg constructor for all our persistent classes:</p>
  
  <blockquote>
    <p>The no-argument constructor is a requirement for all persistent
    classes; Hibernate has to create objects for you, using Java
    Reflection. The constructor can be private, however package or public
    visibility is required for runtime proxy generation and efficient data
    retrieval without bytecode instrumentation.</p>
  </blockquote>
  
  <p>But when I created a sample program for testing by creating a POJO class without any no-arg constructor and by placing a constructor that takes parameters, I expected Hibernate will throw an exception but I was surprised to see that I was not getting exceptions.</p>
  
  <p>Here is my POJO:</p>
  
  <pre><code>public class Event {
      private Long id;
  
      private String title;
  
      public Event(String title) {}
  
      public Long getId() {
          return id;
      }
  
      private void setId(Long id) {
          this.id = id;
      }
  
      public String getTitle() {
          return title;
      }
  
      public void setTitle(String title) {
          this.title = title;
      }
  }
  </code></pre>
  
  <p>Here is my program:</p>
  
  <pre><code>import org.hibernate.Session;
  import java.util.*;
  import org.hibernate.tutorial.domain.Event;
  import org.hibernate.tutorial.util.HibernateUtil;
  
  public class EventManager {
      public static void main(String[] args) {
          EventManager mgr = new EventManager();
          mgr.createAndStoreEvent("My Event", new Date());
          HibernateUtil.getSessionFactory().close();
      }
  
      private void createAndStoreEvent(String title, Date theDate) {
          Session session = HibernateUtil.getSessionFactory().getCurrentSession();
          session.beginTransaction();
          Event theEvent = new Event("");
          theEvent.setTitle(title);
          theEvent.setDate(theDate);
          session.save(theEvent);
          session.getTransaction().commit();
      }
  }
  </code></pre>
  
  <p>I have also configured hibernate configuration and mapping files. Now I can see that a new record is stored in my DB when I execute this program.</p>
  
  <p>I am new to Hibernate, please help me in understanding the statement given in hibernate documentation as the document says we need a no-arg constructor. I am using Hibernate 3.x version.</p>