×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Java
Posted by: usera5b92
Added: Jul 20, 2022 6:19 PM
Views: 7
Tags: hibernate
  1. public static SessionFactory getSessionFactory() {
  2.  
  3.         // Create registry
  4.         StandardServiceRegistry registry = new StandardServiceRegistryBuilder().configure().build();
  5.         if (sessionFactory == null) {
  6.             try {
  7.                 // Create MetadataSources
  8.                 MetadataSources sources = new MetadataSources(registry);
  9.  
  10.                 // Create Metadata
  11.                 Metadata metadata = sources.getMetadataBuilder().build();
  12.  
  13.                 // Create SessionFactory
  14.                 sessionFactory = metadata.getSessionFactoryBuilder().build();
  15.  
  16.             } catch (Exception e) {
  17.                 e.printStackTrace();
  18.                 StandardServiceRegistryBuilder.destroy(registry);
  19.             }
  20.         }
  21.         return sessionFactory;
  22.     }