Guest User

Untitled

a guest
Jul 7th, 2018
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.28 KB | None | 0 0
  1. package cadastro.controler.utils;
  2.  
  3. import org.hibernate.Session;
  4. import org.hibernate.SessionFactory;
  5. import org.hibernate.SessionFactoryObserver;
  6. import org.hibernate.cfg.Configuration;
  7. import org.hibernate.service.ServiceRegistry;
  8. import org.hibernate.service.ServiceRegistryBuilder;
  9. import org.hibernate.tool.hbm2ddl.SchemaUpdate;
  10. import cadastro.model.bean.*;
  11.  
  12. /**
  13. *
  14. * @author Reginaldo Spricigo
  15. *
  16. * @since 1.0
  17. * @version 1.0, 14/12/2011
  18. */
  19. public class HibernateUtils {
  20.  
  21. private static final String DATABASE_HOST = "jdbc:mysql://mysql.rspricigo.jelastic.com/familiarebelatto";
  22.  
  23. private static final SessionFactory factory;
  24. private static final ServiceRegistry serviceRegistry;
  25.  
  26. static {
  27. Configuration config = getConfiguration();
  28.  
  29. serviceRegistry = new ServiceRegistryBuilder().applySettings(
  30. config.getProperties()).buildServiceRegistry();
  31.  
  32. config.setSessionFactoryObserver(new SessionFactoryObserver() {
  33.  
  34. @Override
  35. public void sessionFactoryCreated(SessionFactory factory) { }
  36.  
  37. @Override
  38. public void sessionFactoryClosed(SessionFactory factory) {
  39. ServiceRegistryBuilder.destroy(serviceRegistry);
  40. }
  41. });
  42.  
  43. factory = config.buildSessionFactory(serviceRegistry);
  44. }
  45.  
  46. private static Configuration getConfiguration() {
  47.  
  48. Configuration cfg = new Configuration();
  49.  
  50. cfg.addAnnotatedClass(Pessoa.class);
  51.  
  52.  
  53. cfg.setProperty("hibernate.connection.driver_class",
  54. "com.mysql.jdbc.Driver");
  55. cfg.setProperty("hibernate.connection.url", DATABASE_HOST);
  56. cfg.setProperty("hibernate.connection.username", "cadrebelatto");
  57. cfg.setProperty("hibernate.connection.password", "jabulani");
  58. cfg.setProperty("hibernate.show_sql", "true");
  59. cfg.setProperty("hibernate.dialect",
  60. "org.hibernate.dialect.MySQL5InnoDBDialect");
  61. cfg.setProperty("hibernate.connection.autocommit", "false");
  62. cfg.setProperty("hibernate.c3p0.min_size", "5");
  63. cfg.setProperty("hibernate.c3p0.max_size", "25");
  64. cfg.setProperty("hibernate.c3p0.timeout", "60");
  65. cfg.setProperty("hibernate.c3p0.max_statements", "50");
  66. cfg.setProperty("hibernate.c3p0.acquireIncrement", "1");
  67. cfg.setProperty("hibernate.c3p0.preferredTestQuery", "SELECT 1");
  68. cfg.setProperty("hibernate.hbm2ddl.auto", "update");
  69. cfg.setProperty("hibernate.cache.provider_class",
  70. "org.hibernate.cache.NoCacheProvider");
  71. cfg.setProperty("hibernate.transaction.factory_class",
  72. "org.hibernate.transaction.JDBCTransactionFactory");
  73. cfg.setProperty("hibernate.current_session_context_class", "thread");
  74. cfg.setProperty("hibernate.generate_statistics", "true");
  75.  
  76. return cfg;
  77. }
  78.  
  79. public static Session openSession() {
  80. return factory.openSession();
  81. }
  82.  
  83. public static void createOrUpdateSchema(boolean script, boolean doUpdate) {
  84.  
  85. SchemaUpdate schema = new SchemaUpdate(getConfiguration());
  86. schema.execute(script, doUpdate);
  87. }
  88. }
Add Comment
Please, Sign In to add comment