Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.30 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package be.condorcet.projetpharmacie;
  7.  
  8. import java.util.Properties;
  9. import org.hibernate.SessionFactory;
  10.  
  11. /**
  12.  * Hibernate Utility class with a convenient method to get Session Factory
  13.  * object.
  14.  *
  15.  * @author Michel
  16.  */
  17. public class NewHibernateUtil {
  18.  
  19.     private static final SessionFactory sessionFactory;
  20.     static {
  21.         try {
  22.             String url = System.getenv().get("JDBC_DATABASE_URL");
  23.             String username = System.getenv("JDBC_DATABASE_USERNAME");
  24.             String password = System.getenv("JDBC_DATABASE_PASSWORD");
  25.             Properties prop = new Properties();
  26.             //provide the required properties
  27.             prop.setProperty("hibernate.connection.url", url);
  28.             prop.setProperty("hibernate.connection.username", username);
  29.             prop.setProperty("hibernate.connection.password", password);
  30.             prop.setProperty("dialect", "org.hibernate.dialect.PostgreSQLDialect");
  31.             prop.setProperty("hibernate.query.factory_class", "org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory");
  32.             prop.setProperty("hibernate.connection.driver_class", "org.postgresql.Driver");
  33.             //create a configuration
  34.             org.hibernate.cfg.Configuration config = new org.hibernate.cfg.Configuration();
  35.             //provide all properties to this configuration
  36.             config.setProperties(prop);
  37.             //add classes which are mapped to database tables.
  38.             config.addResource("be/condorcet/projetpharmacie/ApiInfo.hbm.xml");
  39.             config.addResource("be/condorcet/projetpharmacie/ApiMedecin.hbm.xml");
  40.             config.addResource("be/condorcet/projetpharmacie/ApiMedicament.hbm.xml");
  41.             config.addResource("be/condorcet/projetpharmacie/ApiPatient.hbm.xml");
  42.             config.addResource("be/condorcet/projetpharmacie/ApiPrescription.hbm.xml");
  43.             sessionFactory = config.buildSessionFactory();
  44.         } catch (Throwable ex) {
  45.             throw new ExceptionInInitializerError(ex);
  46.         }
  47.     }
  48.     public static SessionFactory getSessionFactory() {
  49.         return sessionFactory;
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement