Search code examples
hibernatesessionfactory

SessionFactory Object Creation using persistance class default constructor


In xml approach of hibernate when we create hibernate SessionFactory object using

private static final SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();

then it will call default constructor of persistance class 3 times it means for creating Session factory object it needs default constructor of persistance class but if I am removing default constructor from my class and only argument constructor is there in this case JVM not provide default constructor then how session factory object is created ?


Solution

  • Hibernate tries to create a bean and it does it via reflection. It does the object creation by calling the no-arg constructor, and then using the setter methods to set the properties. You can't use a bean that doesn't have a no-arg constructor.

    If you don't have a default constructor you should get an exception

    org.hibernate.InstantiationException: No default constructor for entity: <ClassName>
    

    More reading