Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package cadastro.controler.utils;
- import org.hibernate.Session;
- import org.hibernate.SessionFactory;
- import org.hibernate.SessionFactoryObserver;
- import org.hibernate.cfg.Configuration;
- import org.hibernate.service.ServiceRegistry;
- import org.hibernate.service.ServiceRegistryBuilder;
- import org.hibernate.tool.hbm2ddl.SchemaUpdate;
- import cadastro.model.bean.*;
- /**
- *
- * @author Reginaldo Spricigo
- *
- * @since 1.0
- * @version 1.0, 14/12/2011
- */
- public class HibernateUtils {
- private static final String DATABASE_HOST = "jdbc:mysql://mysql.rspricigo.jelastic.com/familiarebelatto";
- private static final SessionFactory factory;
- private static final ServiceRegistry serviceRegistry;
- static {
- Configuration config = getConfiguration();
- serviceRegistry = new ServiceRegistryBuilder().applySettings(
- config.getProperties()).buildServiceRegistry();
- config.setSessionFactoryObserver(new SessionFactoryObserver() {
- @Override
- public void sessionFactoryCreated(SessionFactory factory) { }
- @Override
- public void sessionFactoryClosed(SessionFactory factory) {
- ServiceRegistryBuilder.destroy(serviceRegistry);
- }
- });
- factory = config.buildSessionFactory(serviceRegistry);
- }
- private static Configuration getConfiguration() {
- Configuration cfg = new Configuration();
- cfg.addAnnotatedClass(Pessoa.class);
- cfg.setProperty("hibernate.connection.driver_class",
- "com.mysql.jdbc.Driver");
- cfg.setProperty("hibernate.connection.url", DATABASE_HOST);
- cfg.setProperty("hibernate.connection.username", "cadrebelatto");
- cfg.setProperty("hibernate.connection.password", "jabulani");
- cfg.setProperty("hibernate.show_sql", "true");
- cfg.setProperty("hibernate.dialect",
- "org.hibernate.dialect.MySQL5InnoDBDialect");
- cfg.setProperty("hibernate.connection.autocommit", "false");
- cfg.setProperty("hibernate.c3p0.min_size", "5");
- cfg.setProperty("hibernate.c3p0.max_size", "25");
- cfg.setProperty("hibernate.c3p0.timeout", "60");
- cfg.setProperty("hibernate.c3p0.max_statements", "50");
- cfg.setProperty("hibernate.c3p0.acquireIncrement", "1");
- cfg.setProperty("hibernate.c3p0.preferredTestQuery", "SELECT 1");
- cfg.setProperty("hibernate.hbm2ddl.auto", "update");
- cfg.setProperty("hibernate.cache.provider_class",
- "org.hibernate.cache.NoCacheProvider");
- cfg.setProperty("hibernate.transaction.factory_class",
- "org.hibernate.transaction.JDBCTransactionFactory");
- cfg.setProperty("hibernate.current_session_context_class", "thread");
- cfg.setProperty("hibernate.generate_statistics", "true");
- return cfg;
- }
- public static Session openSession() {
- return factory.openSession();
- }
- public static void createOrUpdateSchema(boolean script, boolean doUpdate) {
- SchemaUpdate schema = new SchemaUpdate(getConfiguration());
- schema.execute(script, doUpdate);
- }
- }
Add Comment
Please, Sign In to add comment