Example usage for org.hibernate.cfg Configuration Configuration

List of usage examples for org.hibernate.cfg Configuration Configuration

Introduction

In this page you can find the example usage for org.hibernate.cfg Configuration Configuration.

Prototype

public Configuration() 

Source Link

Usage

From source file:com.dotosoft.dotoquiz.tools.util.HibernateUtil.java

License:Apache License

public static SessionFactory buildSessionFactory(Settings setting) {
    try {/*from   w w  w.jav  a2 s. c  o  m*/
        Properties prop = new Properties();
        prop.setProperty("hibernate.connection.driver_class",
                setting.getConfiguration().getConnection().getDriverClass());
        prop.setProperty("hibernate.connection.url", setting.getConfiguration().getConnection().getUrl());
        prop.setProperty("hibernate.connection.username", setting.getConfiguration().getConnection().getUser());
        prop.setProperty("hibernate.connection.password",
                setting.getConfiguration().getConnection().getPassword());
        prop.setProperty("hibernate.connection.pool_size",
                String.valueOf(setting.getConfiguration().getConnection().getPoolSize()));
        prop.setProperty("hibernate.dialect", setting.getConfiguration().getDialect());
        prop.setProperty("hibernate.hbm2ddl.auto", setting.getConfiguration().getHbm2ddl());
        prop.setProperty("hibernate.show_sql", setting.getConfiguration().getShowSQL());

        Configuration annotationConfig = new Configuration().addProperties(prop);

        for (String packageMap : setting.getConfiguration().getMappingPackages()) {
            annotationConfig.addPackage(packageMap);
        }

        for (String classMap : setting.getConfiguration().getMappingClasses()) {
            annotationConfig.addAnnotatedClass(Class.forName(classMap));
        }

        StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder()
                .applySettings(annotationConfig.getProperties());
        sessionFactory = annotationConfig.buildSessionFactory(ssrb.build());

    } catch (Throwable ex) {
        // Make sure you log the exception, as it might be swallowed
        System.err.println("Initial SessionFactory creation failed." + ex);
        throw new ExceptionInInitializerError(ex);
    }

    return sessionFactory;
}

From source file:com.dreamwork.service.MailService.java

public void start() {
    Configuration configuration = new Configuration().configure();
    StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder()
            .applySettings(configuration.getProperties());
    SessionFactory factory = configuration.buildSessionFactory(builder.build());
    service = new SenderService(this);
}

From source file:com.drogaria.util.HibernateUtil.java

private static SessionFactory criarFabricaDeSessoes() {
    try {/*from  ww w . j  a  v a  2 s . com*/
        Configuration configuracao = new Configuration().configure(); // Leitura do hibernate.cfg.xml

        // 
        ServiceRegistry registro = new StandardServiceRegistryBuilder()
                .applySettings(configuracao.getProperties()).build();

        SessionFactory fabrica = configuracao.buildSessionFactory(registro);
        System.out.println("A fabrica conectou !!!");
        return fabrica;
    } catch (Throwable ex) {
        System.err.println("A fbrica de sesses no pode ser criada." + ex);
        throw new ExceptionInInitializerError(ex);
    }
}

From source file:com.dutchconnected.fruithuis.frames.Main.java

/**
  * @param args the command line arguments
  *///from  ww w.  j av  a2s .com
public static void main(String args[]) throws ClassNotFoundException {
    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(LoginForm.class.getName()).log(java.util.logging.Level.SEVERE, null,
                ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(LoginForm.class.getName()).log(java.util.logging.Level.SEVERE, null,
                ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(LoginForm.class.getName()).log(java.util.logging.Level.SEVERE, null,
                ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(LoginForm.class.getName()).log(java.util.logging.Level.SEVERE, null,
                ex);
    }
    //</editor-fold>

    Class.forName("oracle.jdbc.driver.OracleDriver");
    SessionFactory factory = new Configuration().

    //addPackage("com.xyz") //add package if used.
            addAnnotatedClass(Category.class).addAnnotatedClass(Order.class).addAnnotatedClass(Product.class)
            .addAnnotatedClass(ProductOrder.class).addAnnotatedClass(ProductUnitUser.class)
            .addAnnotatedClass(Unit.class).addAnnotatedClass(User.class).configure().buildSessionFactory();
    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new Categorie(factory).setVisible(true);
        }
    });
}

From source file:com.dutchconnected.fruithuis.Main.java

public static void main(String[] args) {
    try {//w  w  w.  j ava2  s .c  o m
        Class.forName("oracle.jdbc.driver.OracleDriver");
        factory = new Configuration().configure().
        //addPackage("com.xyz") //add package if used.
                addAnnotatedClass(Category.class).addAnnotatedClass(Order.class)
                .addAnnotatedClass(Product.class).addAnnotatedClass(ProductOrder.class)
                .addAnnotatedClass(ProductUnitUser.class).addAnnotatedClass(Unit.class)
                .addAnnotatedClass(User.class).buildSessionFactory();
    } catch (Throwable ex) {
        System.err.println("Failed to create sessionFactory object." + ex);
        throw new ExceptionInInitializerError(ex);
    }

}

From source file:com.dz.common.dataimport2.DataImport.java

public static void init() {
        Configuration configurate = new Configuration()
                .configure(DataImport.class.getResource("apply_manage/hibernate.cfg.xml"));
        sessionFactory = configurate.buildSessionFactory();

        ctx = new ClassPathXmlApplicationContext("com/dz/common/dataimport2/applicationContext.xml");
        driverDao = ctx.getBean(DriverDao.class);
        vehicleDao = ctx.getBean(VehicleDao.class);
        contractService = ctx.getBean(ContractService.class);
        vehicleApprovalService = ctx.getBean(VehicleApprovalService.class);
    }/*  w w w  .j  a va 2  s  . c  o  m*/

From source file:com.eby.orm.utils.HibernateUtils.java

private static SessionFactory buildSessionFactory() {
    try {/* w ww . j av  a  2 s .c  o  m*/
        // Create the SessionFactory from standard (hibernate.cfg.xml) 
        // config file.
        String configFile = "conf/hibernate.cfg.xml";
        String path = System.getProperty("user.dir");
        path = path + "/" + configFile;
        SessionFactory sessFactory = new Configuration().configure(new File(path)).buildSessionFactory();
        return sessFactory;
    } catch (Throwable ex) {
        // Log the exception. 
        System.err.println("Exception stack Trace ************** begin");
        System.err.println("Hibernate : Initial SessionFactory creation failed." + ex);
        ex.printStackTrace();
        System.err.println("Exception Stack Trace ********* END");
        throw new ExceptionInInitializerError(ex);
    }
}

From source file:com.entity.getSession.java

public static Session getSess() {
    SessionFactory fac = new Configuration().configure().buildSessionFactory();
    Session session = fac.openSession();
    //session.beginTransaction();
    System.out.println("success");
    return session;

}

From source file:com.ephesoft.dcma.core.hibernate.DynamicHibernateDao.java

License:Open Source License

/**
 * Constructor./*from   w w w .j a  va  2s . c  o m*/
 * 
 * @param userName String
 * @param password String
 * @param driverName String
 * @param jdbcUrl String
 * @param dialectName String
 */
public DynamicHibernateDao(String userName, String password, String driverName, String jdbcUrl,
        String dialectName) {
    Properties properties = createHibernateProperties(userName, password, driverName, jdbcUrl, dialectName);

    Configuration configuration = new Configuration().addProperties(properties);
    try {
        if (sessionFactory != null) {
            sessionFactory.close();
        }
    } catch (Exception e) {
        LOG.error("Could not close session factory", e);
    }
    sessionFactory = configuration.buildSessionFactory();
}

From source file:com.ephesoft.dcma.core.hibernate.DynamicHibernateDao.java

License:Open Source License

/**
 * Constructor.//w  ww .j ava 2s  . com
 * 
 * @param cfgLocation String
 */
public DynamicHibernateDao(String cfgLocation) {
    Configuration configuration = new Configuration().configure(cfgLocation);
    String filePath = META_INF + File.separator + FOLDER_NAME + File.separator + FILE_NAME + ".properties";
    Properties properties;
    InputStream propertyInStream = null;
    try {
        propertyInStream = new ClassPathResource(filePath).getInputStream();
        properties = new Properties();
        properties.load(propertyInStream);
    } catch (Exception e) {
        LOG.error(e.getMessage(), e);
        throw new HibernateException(e.getMessage(), e);
    } finally {
        if (propertyInStream != null) {
            try {
                propertyInStream.close();
            } catch (IOException ioe) {
                LOG.info("Could not close property input stream in Dynamic Hibernate Dao.");
            }
        }
    }
    for (Object propertyKey : properties.keySet()) {
        String propertyKeyString = (String) propertyKey;
        if (!propertyKeyString.equalsIgnoreCase(PASSWORD)) {
            configuration.setProperty(propertyKeyString, properties.getProperty(propertyKeyString));
        } else {
            PasswordDecryptor passwordDecryptor = new PasswordDecryptor(
                    properties.getProperty(propertyKeyString));
            try {
                configuration.setProperty(propertyKeyString, passwordDecryptor.getDecryptedString());
            } catch (CryptographyException e) {
                LOG.error(e.getMessage(), e);
                throw new HibernateException(e.getMessage(), e);
            }
        }
    }
    try {
        if (sessionFactory != null) {
            sessionFactory.close();
        }
    } catch (Exception e) {
        LOG.error("Could not close session factory", e);
    }
    sessionFactory = configuration.buildSessionFactory();
}