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.mycompany.desktopinlamninguppgift2.repository.NewHibernateUtil.java

public static SessionFactory getSessionFactory() {
    if (sessionFactory == null) {
        Configuration configuration = new Configuration().configure();
        StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
                .applySettings(configuration.getProperties()).build();
        sessionFactory = configuration.buildSessionFactory(registry);
    }/*from   www  .j a  v  a  2 s  .c o m*/
    return sessionFactory;
}

From source file:com.mycompany.hibernateExp.HibernateUtil.java

private static SessionFactory buildSessionFactory() {
    try {//from w  w  w  .ja v a  2  s.  co  m
        //Create a sessionFactory based on the hibernate.cfg.xml
        //return new Configuration().buildSessionFactory(new StandardServiceRegistryBuilder().build());
        Configuration config = new Configuration();
        config.configure();

        ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
                .applySettings(config.getProperties()).build();
        return config.buildSessionFactory(serviceRegistry);
    } catch (Throwable ex) {
        System.err.println("Initial SessionFactory creation failed. \n" + ex);
        throw new ExceptionInInitializerError(ex);
    }
}

From source file:com.mycompany.labhibernate.GeoDatabase.java

public static void main(String arg[]) {
    try {//from   ww  w . ja va2s . c o m
        factory = new Configuration().configure().buildSessionFactory();
    } catch (Throwable ex) {
        System.err.println("Failed to create sessionFactory object." + ex);
        throw new ExceptionInInitializerError(ex);
    }
    System.out.println("Test");
    GeoDatabase d = new GeoDatabase();

    /* Add few employee records in database */
    String csvFile = "C:\\Users\\dell\\Documents\\NetBeansProjects\\LabHibernate\\src\\GeoLiteCity-Location.csv";
    String line = "";
    String csvSplitBy = ",";

    try (BufferedReader br = new BufferedReader(new FileReader(csvFile))) {
        br.readLine();
        br.readLine();
        while ((line = br.readLine()) != null) {

            String[] location = line.split(csvSplitBy);
            System.out.println(location.length);
            d.addLocation(location[0], location[1], location[3], location[5], location[6]);

        }

    } catch (IOException e) {
        e.printStackTrace();
    }

}

From source file:com.mycompany.testes.TesteTrocarConfiguracao.java

public static void main(String[] args) {
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    Usuario usuario = null;/*from w  ww. j a v  a 2  s.  co  m*/

    session.beginTransaction();

    Query query = session
            .createQuery("from Usuario u WHERE u.userName = 'mvassoler' and u.passwordUser = 'mitco'");
    usuario = (Usuario) query.uniqueResult();

    session.getSessionFactory().close();

    System.out.println(usuario.getName());

    Configuration config = new Configuration().configure("hibernate.cfgGen.xml");
    config.setProperty("connection.driver_class", usuario.getDriverClass());
    config.setProperty("connection.url", usuario.getUrl() + usuario.getDataBase());
    config.setProperty("hibernate.dialect", usuario.getDialect());
    SchemaExport se = new SchemaExport(config);
    se.create(true, true);
}

From source file:com.myfarmchat.farmchat.HibernateUtil.java

private static SessionFactory buildSessionFactory() {
    try {/*w  w w  .j a v  a 2  s .c om*/
        Configuration configuration = new Configuration().configure("hibernate.cfg.xml"); //ginetai h antistoixish twn objects me th bash
        StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder()
                .applySettings(configuration.getProperties());
        sessionFactory = configuration.buildSessionFactory(builder.build());
        return sessionFactory;
    } catch (Throwable ex) {
        System.err.println("Initial SessionFactory creation failed." + ex);
        ex.printStackTrace();
        throw new ExceptionInInitializerError(ex);
    }
}

From source file:com.mypkg.repository.repoimpl.DepartmentRepositoryImplementation.java

@Override
public List<Department> getAllDepartments() {
    listOfDepartments = new ArrayList<Department>();
    Configuration cf = new Configuration();
    cf.configure("hibernate.cfg.xml");

    SessionFactory sf = cf.buildSessionFactory();
    Session hsession = sf.openSession();
    Transaction tx = hsession.beginTransaction();
    List<Object[]> deptObjects = hsession.createSQLQuery("SELECT dept_id,dept_name FROM department").list();
    for (Object[] deptObject : deptObjects) {
        Department dept = new Department();
        int id = ((int) deptObject[0]);

        String dname = (String) deptObject[1];
        dept.setDeptId(id);//from w ww .j a va2 s  .c  o  m
        dept.setDeptName(dname);

        listOfDepartments.add(dept);
    }

    tx.commit();
    return listOfDepartments;

}

From source file:com.mypkg.repository.repoimpl.EmployeeRepositoryImplementation.java

@Override
public List<Employee> getAllEmployees() {
    listOfEmployees = new ArrayList<Employee>();
    Configuration cf = new Configuration();
    cf.configure("hibernate.cfg.xml");

    SessionFactory sf = cf.buildSessionFactory();
    Session hsession = sf.openSession();
    Transaction tx = hsession.beginTransaction();
    List<Object[]> empObjects = hsession
            .createSQLQuery(/*from   ww  w .j  a v  a2s .com*/
                    "SELECT id,emp_name,dept_name FROM employee e, department d where e.dept_id=d.dept_id")
            .list();
    for (Object[] employeeObject : empObjects) {
        Employee employee = new Employee();
        int id = ((int) employeeObject[0]);

        String ename = (String) employeeObject[1];
        String dname = (String) employeeObject[2];
        employee.setId(id);
        employee.setEmpName(ename);
        employee.setDeptName(dname);

        listOfEmployees.add(employee);
    }

    tx.commit();
    return listOfEmployees;

}

From source file:com.mypkg.repository.repoimpl.EmployeeRepositoryImplementation.java

@Override
public void deleteEmployee(int id) {
    //  String sql = "DELETE FROM employee WHERE id=?";
    //jdbc.update(sql, id);
    Configuration cf = new Configuration();
    cf.configure("hibernate.cfg.xml");

    SessionFactory sf = cf.buildSessionFactory();
    Session hsession = sf.openSession();
    Transaction tx = hsession.beginTransaction();
    String hql = "DELETE from Employee where id=:emp_id";
    Query q = hsession.createQuery(hql);
    q.setParameter("emp_id", id);
    int result = q.executeUpdate();

    if (result == 1) {
        tx.commit();//from  w w  w . j  a  v a2s . co  m
    } else {
        tx.rollback();
    }
}

From source file:com.mypkg.repository.repoimpl.EmployeeRepositoryImplementation.java

@Override
public void addEmployee(int id, String emp_name, int dept_id) {
    Configuration cf = new Configuration();
    cf.configure("hibernate.cfg.xml");

    SessionFactory sf = cf.buildSessionFactory();
    Session hsession = sf.openSession();
    Transaction tx = hsession.beginTransaction();
    Query query = hsession.createSQLQuery("INSERT INTO EMPLOYEE VALUES (:id,:ename,:deptid)");
    query.setInteger("id", id);
    query.setString("ename", emp_name);
    query.setInteger("deptid", dept_id);
    query.executeUpdate();//from w  w w.j a  va2 s. c  o m
    tx.commit();

}

From source file:com.mysema.query.jpa.codegen.HibernateDomainExporterTest.java

License:Apache License

@Test
public void Execute_MyEntity() throws IOException {
    FileUtils.delete(new File("target/gen6"));
    File myEntity = new File("src/test/resources/entity.hbm.xml");
    Configuration config = new Configuration();
    config.addFile(myEntity);// ww  w.  j  ava  2 s.  c  o  m
    HibernateDomainExporter exporter = new HibernateDomainExporter("Q", new File("target/gen6"), config);
    exporter.execute();

    File targetFile = new File("target/gen6/com/mysema/query/jpa/codegen/QMyEntity.java");
    assertContains(targetFile, "StringPath pk1", "StringPath pk2", "StringPath prop1");
}