Skip to main content

Hibernate 5.4.4 - Instantiate an instance of SessionFactory


Introduction

We will learn how to create an instance of SessionFactory using Hibernate 5.4.4

Code

private static final SessionFactory sessionFactory;
public static final String cfgFileLocation = "/hibernate.cfg.xml";

 try {     
    StandardServiceRegistry standardRegistry = new StandardServiceRegistryBuilder()

                                           .configure( cfgFileLocation ).build();
     
    Metadata metadata = new MetadataSources( standardRegistry )
            .getMetadataBuilder()
            .applyImplicitNamingStrategy( ImplicitNamingStrategyJpaCompliantImpl.INSTANCE )
            .build();

    sessionFactory = metadata.getSessionFactoryBuilder()
          .build();
    } catch (Throwable ex) {         
        System.out.println( "exception" );
        throw new ExceptionInInitializerError(ex);
    }


Following exceptions are resolved through the above code

  • java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: Person is not mapped
  • java.lang.UnsupportedOperationException: The application must supply JDBC connections


Comments