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.ordecon.schmoo.Communication.java

License:Creative Commons License

public static void main(String[] args)
        throws ClassNotFoundException, IllegalAccessException, InstantiationException {
    PropertyConfigurator.configure("log4j.cfg");
    Logger log = Logger.getLogger(Communication.class);
    log.info("Schmoo Server starting...");

    Configuration configuration = new Configuration().configure(new File("hibernate.cfg.xml"));
    SessionFactory sf = configuration.buildSessionFactory();

    Class c = Class.forName("com.ordecon.schmoo.smsc.modules.http.HTTPConnector");
    Connector con = (Connector) c.newInstance();
    //con.$sendTestMessage();

    Session s = sf.openSession();//from w  ww  . j  a va2 s  . c  o  m
    ModuleTemplate ct = (ModuleTemplate) s.load(ModuleTemplate.class, 1);
    log.info(ct);

    for (Object o : ct.getParameters()) {
        ModuleParameterTemplate parameterTemplate = (ModuleParameterTemplate) o;
        log.info(parameterTemplate);
    }
}

From source file:com.org.rook.dal.DataAccess.java

public static SessionFactory getSessionFactory() {
    Configuration configuration = new Configuration().configure();
    StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder()
            .applySettings(configuration.getProperties());
    SessionFactory sessionFactory = configuration.buildSessionFactory(builder.build());
    return sessionFactory;
}

From source file:com.oy.shared.lm.ext.HBMCtoGRAPH.java

License:Open Source License

public static IGraphModel load(TaskOptions options) throws IOException {
    Configuration conf = new Configuration();

    // add all documents to Configuration
    for (int i = 0; i < options.inFileSet.length; i++) {
        conf.addFile(options.inFileSet[i]);
    }/*  w  w w .  j a v  a2s .  c o  m*/

    return new HBMCtoGRAPH().innerLoad(options, conf);
}

From source file:com.payment.java

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

        HttpSession session = request.getSession();

        Calendar cal = Calendar.getInstance();
        String month = new SimpleDateFormat("MMM").format(cal.getTime());
        String concept = request.getParameter("concept");
        String flat = (String) session.getAttribute("flat");
        // String month =  request.getParameter("month");
        int amount = Integer.parseInt(request.getParameter("amount"));

        Pay pay = new Pay();
        pay.setAmount(amount);/*  w ww . j av  a2 s . co m*/
        pay.setConcept(concept);
        pay.setFlat(flat);
        pay.setMonth(month);
        String email = (String) session.getAttribute("email");
        paymentmail pxx = new paymentmail();
        int n = pxx.send(email, amount, month);

        if (n == 0) {
            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();
            session1.persist(pay);
            t.commit();
            session1.close();

            request.setAttribute("success", "suc");
            RequestDispatcher rd = request.getRequestDispatcher("account.jsp");
            rd.forward(request, response);

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

    }

    catch (Exception e) {

    }
}

From source file:com.persinity.ndt.datamutator.hibernate.HibernateConfig.java

License:Apache License

/**
 * @return {@link Configuration}// ww w .ja  va 2 s  . c  o m
 */
public Configuration getHibernateConfiguration() {
    Configuration configuration = new Configuration().configure();

    configuration.setProperty("hibernate.connection.url", dbConfig.getDbUrl());
    configuration.setProperty("hibernate.connection.username", dbConfig.getDbUser());
    configuration.setProperty("hibernate.connection.password", dbConfig.getDbPass());
    configuration.setProperty("hibernate.dialect",
            config.getStringDefault(HIBERNATE_DIALECT_KEY, DEFAULT_HIBERNATE_DIALECT));
    configuration.setProperty("hibernate.connection.driver_class",
            config.getStringDefault(HIBERNATE_DRIVER_CLASS_KEY, DEFAULT_HIBERNATE_DRIVER_CLASS));

    if (config.getBooleanDefault(HIBERNATE_DEBUG_KEY, false)) {
        configuration.setProperty("show_sql", "true");
        configuration.setProperty("format_sql", "true");
    }

    configuration.setNamingStrategy(new NamingStrategy(TABLE_PREFIX));

    return configuration;
}

From source file:com.persistencia.utility.HibernateUtil.java

public static SessionFactory getSessionFactory() {
    if (sessionFactory == null) {
        sessionFactory = new Configuration().configure().buildSessionFactory();
    }// ww  w  .j ava  2 s .  c  om
    return sessionFactory;
}

From source file:com.persistent.azure.HibernateAccess.java

License:Apache License

/**
 * This method loads default hibernate.cfg.xml file and all the
 * configuration mentioned in it.//from  www.j  av  a  2  s  .c  o m
 * 
 * @return The session factory object
 */
private static SessionFactory buildSessionFactory() {
    try {
        // Create the SessionFactory from hibernate.cfg.xml
        return new Configuration().configure().buildSessionFactory();
    } catch (Throwable ex) {
        System.err.println("Initial SessionFactory creation failed." + ex);
        throw new ExceptionInInitializerError(ex);
    }
}

From source file:com.persistent.cloudninja.utils.SessionFactoryConfigurer.java

License:Apache License

public SessionFactory createSessionFactoryForTenant(String tenantDB) {
    TenantDataConnectionEntity dataConnectionEntity = tenantDataConnectionDao.find(tenantDB.substring(4));
    StringBuffer strBufServerURL = new StringBuffer("jdbc:sqlserver://");
    strBufServerURL.append(dataConnectionEntity.getServer());
    strBufServerURL.append(";databaseName=");
    strBufServerURL.append(tenantDB);/*ww  w  . jav  a 2 s .c  o  m*/
    strBufServerURL.append(";");

    String userName = dataConnectionEntity.getUser();
    String password = dataConnectionEntity.getPassword();

    Configuration cfg = new Configuration();
    cfg.addClass(TaskList.class);
    cfg.setProperty("hibernate.dialect", "org.hibernate.dialect.SQLServerDialect");
    cfg.setProperty("hibernate.show_sql", "true");
    cfg.setProperty("hibernate.hbm2ddl.auto", "update");
    cfg.setProperty("hibernate.connection.driver_class", driverClassName);
    cfg.setProperty("hibernate.connection.url", strBufServerURL.toString());
    cfg.setProperty("hibernate.connection.username", userName);
    cfg.setProperty("hibernate.connection.password", password);

    return cfg.buildSessionFactory();
}

From source file:com.pfm.personalfinancemanagergrid.mainClasses.DataGridDataManager.java

public String getData() {
    SessionFactory factory = new Configuration().configure().buildSessionFactory();
    Session session = factory.openSession();
    try {//from  w  w  w .j a va2 s  . c  o  m

        Class<?> cls = Class.forName(cache.getEntity());
        Table table = cls.getAnnotation(javax.persistence.Table.class);
        char firstLetter = cache.getEntity().charAt(0);
        Query query = this.buildQuery(session, params, false);
        Query q = this.buildQuery(session, params, true);
        List<Serializable> allResults = query.list();
        List<Serializable> resultList = q.list();
        List<List<String>> resultArray = new ArrayList<>();
        for (Serializable serializable : resultList) {
            Field[] fields = cls.getDeclaredFields();
            List<String> innerResult = new ArrayList<>();
            for (GridCacheColumnObject column : cache.getColumns()) {
                if (!column.isOptionsColumn()) {
                    for (Field field : fields) {
                        field.setAccessible(true);
                        String cachedFieldName = column.getColumnName();
                        String rootEntityField;
                        if (cachedFieldName.contains(".")) {
                            String[] innerObjects = cachedFieldName.split("\\.");
                            rootEntityField = innerObjects[0];
                        } else {
                            rootEntityField = cachedFieldName;
                        }
                        if (!field.getName().equals(rootEntityField)) {
                            continue;
                        }
                        String value = "";
                        if (column.getColumnName().contains(".")) {
                            String[] innerObjects = cachedFieldName.split("\\.");
                            Field currentField;
                            Class currentEntity = cls;
                            Object valueHolder = serializable;
                            for (String innerObject : innerObjects) {
                                currentField = currentEntity.getDeclaredField(innerObject);
                                currentField.setAccessible(true);
                                valueHolder = currentField.get(valueHolder);
                                currentEntity = valueHolder.getClass();
                            }
                            value = valueHolder.toString();
                        } else {
                            value = field.get(serializable).toString();
                        }
                        innerResult.add(value);
                    }
                }
            }
            resultArray.add(innerResult);
        }
        Integer itemsCount = allResults.size();
        session.close();
        factory.close();
        Gson gson = new Gson();
        DataGridResponseObject<Serializable> resp = new DataGridResponseObject<>();
        resp.setData(resultArray);
        resp.setDraw(params.getDraw());
        resp.setRecordsFiltered(itemsCount);
        resp.setRecordsTotal(itemsCount);
        String json = gson.toJson(resp);
        return json;
    } catch (ClassNotFoundException | IllegalAccessException | IllegalArgumentException | InstantiationException
            | NoSuchFieldException | SecurityException | ParseException | HibernateException ex) {
        session.close();
        factory.close();
        System.err.println("Initial SessionFactory creation failed." + ex);
        throw new ExceptionInInitializerError(ex);
    }
}

From source file:com.processpuzzle.persistence.domain.HibernatePersistenceProvider.java

License:Open Source License

public boolean configure() {
    super.configure();
    hibernateConfiguration = new Configuration();
    addPropertiesToTheHibernateConfiguration();
    handleDatabaseInitializationStrategy();
    addEntityClassesToTheHibernateConfiguration();

    try {//from w  ww .ja v a2  s.  co  m
        initDatabase();
    } catch (Exception e) {
        throw new PersistenceProviderInitializationException(this.getClass(), e);
    }

    isConfigured = true;
    return isConfigured();
}