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:cn.json.quicknCore.common.AbstractApplication.java

public void hibernateInit() {
    Properties properties = new Properties();
    Configuration cfg = new Configuration();
    // add model class to configuration.
    ArrayList<Class<?>> models = QuickUtils.getPackageClasses(Setting.packageRoot + ".model", null,
            Entity.class);
    models.addAll(QuickUtils.getPackageClassInJar(Setting.webRoot + "WEB-INF/lib/quick.jar",
            Setting.packageRoot + ".model", null, Entity.class));
    for (Plugin plugin : Setting.plugins) {
        if (plugin.getModels() != null)
            models.addAll(plugin.getModels());
    }/*from  w w w  . ja v a2 s .co m*/
    for (Class<?> model : models) {
        cfg.addAnnotatedClass(model);
    }

    models = getModels();
    if (models != null) {
        for (Class<?> model : models) {
            cfg.addAnnotatedClass(model);
        }
    }

    // dialectjdbcjdbc?mysqlinnodb for
    // mysql5dialect???
    if (Setting.dialect != null && Setting.dialect.length() > 0)
        properties.put("hibernate.dialect", Setting.dialect);
    else
        properties.put("hibernate.dialect", QuickUtils.getDialectByDriver(Setting.jdbcDriver));
    properties.put("hibernate.connection.driver_class", Setting.jdbcDriver);
    properties.put("hibernate.connection.url", Setting.jdbcUrl);
    properties.put("hibernate.connection.username", Setting.jdbcUser);
    properties.put("hibernate.connection.password", Setting.jdbcPassword);
    properties.put("hibernate.connection.provider_class", "org.hibernate.connection.C3P0ConnectionProvider");
    properties.put("hibernate.c3p0.min_size", Setting.initActive + "");
    properties.put("hibernate.c3p0.max_size", Setting.maxActive + "");
    properties.put("hibernate.c3p0.timeout", Setting.maxIdle + "");
    properties.put("hibernate.c3p0.idle_test_period", "600");//10
    properties.put("hibernate.c3p0.preferredTestQuery", "SELECT 1");
    if (Setting.runMode == Setting.DEV_MODE) {
        properties.put("hibernate.show_sql", "true");
    }
    properties.put("hibernate.order_updates", "true");
    properties.put("hibernate.cache.use_second_level_cache", "true");
    properties.put("hibernate.cache.provider_class", "org.hibernate.cache.EhCacheProvider");

    Properties extraProp = new Properties();
    try {
        InputStream extra = ClassLoader.getSystemResourceAsStream("hibernate.properties");
        if (extra != null) {
            extraProp.load(extra);
            properties.putAll(extraProp);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    onHibernateConfig(properties);
    cfg.mergeProperties(properties);
    ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(cfg.getProperties())
            .buildServiceRegistry();
    Setting.sessionFactory = cfg.buildSessionFactory(serviceRegistry);

}

From source file:cn.trymore.core.util.UtilEntity.java

License:Open Source License

private static Configuration getHibernateConf() {
    if (hibernateConf == null) {
        hibernateConf = new Configuration();
    }/*from w  w  w  . j  a v  a  2 s  . c om*/

    return hibernateConf;
}

From source file:co.com.codesoftware.persistencia.HibernateUtil.java

private static SessionFactory buildSessionFactory() {
    try {/*w w w.ja  v  a2  s.co  m*/
        if (sessionFactory == null) {
            config = obtieneParametroContext();
            Configuration configuration = new Configuration()
                    .configure(HibernateUtil.class.getResource(config));
            StandardServiceRegistryBuilder serviceRegistryBuilder = new StandardServiceRegistryBuilder();
            serviceRegistryBuilder.applySettings(configuration.getProperties());
            ServiceRegistry serviceRegistry = serviceRegistryBuilder.build();
            sessionFactory = configuration.buildSessionFactory(serviceRegistry);
        }
        return sessionFactory;
    } catch (Throwable ex) {
        System.err.println("Initial SessionFactory creation failed." + ex);
        throw new ExceptionInInitializerError(ex);
    }
}

From source file:co.com.siscomputo.conexion.HibernateUtil.java

private static SessionFactory buildSessionFactory() {
    try {// w  w  w  .  j  av  a  2  s  .c  om
        ResourceBundle rb = ResourceBundle.getBundle("co.com.siscomputo.archivos.BASECONFIG");
        String config = rb.getString("DATABASE").trim();
        System.out.println("CC: " + config);
        if (sessionFactory == null) {
            Configuration configuration = new Configuration()
                    .configure(HibernateUtil.class.getResource(config + ".cfg.xml"));
            StandardServiceRegistryBuilder serviceRegistryBuilder = new StandardServiceRegistryBuilder();
            serviceRegistryBuilder.applySettings(configuration.getProperties());
            ServiceRegistry serviceRegistry = serviceRegistryBuilder.build();
            sessionFactory = configuration.buildSessionFactory(serviceRegistry);
        }
        return sessionFactory;
    } catch (Throwable ex) {
        System.err.println("Initial SessionFactory creation failed." + ex);
        throw new ExceptionInInitializerError(ex);
    }
}

From source file:co.id.admin.connection.HibernateUtil.java

private static SessionFactory buildSessionFactory() {
    try {//from w w w . ja v  a 2 s .c  o m
        // Create the SessionFactory from hibernate.cfg.xml
        return new Configuration().configure().buildSessionFactory();
    } 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);
    }
}

From source file:com.abiquo.abiserver.persistence.DataAccessTestBase.java

License:Open Source License

private static void ensureFactoryInitialized() {
    if (factory == null) {
        factory = new Configuration().configure("/conf/db/hibernate/hibernate.cfg.xml").buildSessionFactory();
    }//from w ww  .  j  ava2s  .com
}

From source file:com.addshare.java

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");

    int m = 0;// w w w . j  av  a  2  s .c  o m

    HttpSession session = request.getSession(true);
    try {
        int comp = Integer.parseInt(request.getParameter("form1"));
        Configuration cfg = new Configuration();
        cfg.configure("hibernate.cfg.xml");//populates the data of the configuration file  
        SessionFactory factory = cfg.buildSessionFactory();
        Session session1 = factory.openSession();
        Transaction t = session1.beginTransaction();

        if (comp == 1) {
            String comp1 = request.getParameter("comp");
            String rate = request.getParameter("rate");

            String email = (String) session.getAttribute("email");
            int rate1 = Integer.parseInt(rate);
            int opening = rate1;
            Date date = new Date();

            ShareCom u = new ShareCom(rate1, opening, date, rate1, rate1, email, comp1);
            session1.persist(u);
            t.commit();
            session1.close();
            request.setAttribute("success", "suc");
            RequestDispatcher rd = request.getRequestDispatcher("admindash.jsp");
            rd.forward(request, response);

        }
    } catch (Exception e1) {

        request.setAttribute("success", "err");
        RequestDispatcher rd = request.getRequestDispatcher("admindash.jsp");
        rd.forward(request, response);
    }

}

From source file:com.advdb.footballclub.HibernateUtil.java

private static SessionFactory buildSessionFactory() {
    try {/*from   w  w w .j  a v  a  2s  .  c o m*/
        // Create the SessionFactory from hibernate.cfg.xml
        return new Configuration().configure().buildSessionFactory();
        //            return new AnnotationConfiguration().configure().buildSessionFactory();

    } 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);
    }
}

From source file:com.aegeus.db.DbSessionFactory.java

License:Apache License

protected void build(DbIdentity identity, List<Class> pojoGroup) {
    Configuration cfg = new Configuration();
    cfg.setProperty("hibernate.connection.driver", identity.getDriver())
            .setProperty("hibernate.dialect", identity.getDialect())
            .setProperty("hibernate.connection.url", identity.getUrl())
            .setProperty("hibernate.connection.username", identity.getUsername())
            .setProperty("hibernate.connection.password", identity.getPassword())
            .setProperty("hibernate.connection.CharSet", "utf-8")
            .setProperty("hibernate.connection.characterEncoding", "utf-8")
            .setProperty("hibernate.connection.useUnicode", "true")
            .setProperty("current_session_context_class", "thread").setProperty("connection.pool_size", "4")
            .setProperty("hibernate.show_sql", "true");

    for (Class pojo : pojoGroup) {
        cfg.addAnnotatedClass(pojo);/* w ww  .jav a 2s.  com*/
    }

    StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder();
    builder.applySettings(cfg.getProperties());

    factory = cfg.buildSessionFactory(builder.build());
}

From source file:com.agilejerry.orm.AnnotationsIllustrationTest.java

License:Open Source License

@Before
public void setUp() throws Exception {
    // A SessionFactory is set up once for an application
    sessionFactory = new Configuration().configure() // configures settings from hibernate.cfg.xml
            .buildSessionFactory();//from   w  w  w .  j  a  va 2s. c o  m
}