Example usage for org.hibernate.cfg Configuration configure

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

Introduction

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

Prototype

@Deprecated
public Configuration configure(org.w3c.dom.Document document) throws HibernateException 

Source Link

Usage

From source file:com.mycompany.controllers.TeamController.java

@GetMapping("/remove/{idTeam}")
public ModelAndView removeTeam(Model model, @PathVariable("idClub") String idClub,
        @PathVariable("idSection") String idSection, @PathVariable("idTeam") String idTeam) {
    Configuration cfg = new Configuration();
    cfg.configure("hibernate.cfg.xml");
    SessionFactory factory = cfg.buildSessionFactory();

    Session session = factory.openSession();
    Transaction t = session.beginTransaction();

    Druzyna team = session.find(Druzyna.class, Integer.parseInt(idTeam));
    session.remove(team);//w w  w  . j  a v a  2  s.  com
    t.commit();
    session.close();
    factory.close();
    return new ModelAndView("redirect:/club/" + idClub + "/sections/" + idSection + "/teams/");
}

From source file:com.mycompany.controllers.TeamController.java

@GetMapping("/create")
public String createTeam(TeamForm teamForm, Model model, @PathVariable("idClub") String idClub,
        @PathVariable("idSection") String idSection) {
    model.addAttribute("Section", idSection);
    model.addAttribute("Club", idClub);

    Configuration cfg = new Configuration();
    cfg.configure("hibernate.cfg.xml");
    SessionFactory factory = cfg.buildSessionFactory();
    //creating session object  
    Session session = factory.openSession();
    List<Liga> leagueList = session.createCriteria(Liga.class).list();

    model.addAttribute("leagueList", leagueList);

    session.close();/*w w w  . j ava2s .  c  om*/
    return "/team/create_team_view";
}

From source file:com.mycompany.controllers.TeamController.java

@RequestMapping(value = "/create", method = RequestMethod.POST)
public ModelAndView createteam(@Valid TeamForm teamForm, @PathVariable("idClub") String idClub,
        @PathVariable("idSection") String idSection, Model model) {
    Configuration cfg = new Configuration();
    cfg.configure("hibernate.cfg.xml");
    SessionFactory factory = cfg.buildSessionFactory();
    Session session = factory.openSession();
    Transaction t = session.beginTransaction();
    Sekcja section = session.find(Sekcja.class, Integer.parseInt(idSection));

    Druzyna team = new Druzyna();
    team.setNazwa(teamForm.getName());//from ww  w.java 2s .  c o  m
    team.setIdSekcja(section);

    Query query = session.createQuery("from Liga where nazwa=:name");
    query.setParameter("name", teamForm.getLeague());
    List<Liga> leagueList = query.getResultList();
    if (!leagueList.isEmpty())
        team.setIdLiga(leagueList.get(0));

    session.persist(team);
    t.commit();
    session.close();
    factory.close();
    return new ModelAndView("redirect:/club/" + idClub + "/sections/" + idSection + "/teams/");
}

From source file:com.mycompany.controllers.TeamController.java

@RequestMapping(value = "/edit/{idTeam}", method = RequestMethod.POST)
public ModelAndView editteam(@Valid TeamForm teamForm, @PathVariable("idClub") String idClub,
        @PathVariable("idSection") String idSection, @PathVariable("idTeam") String idTeam, Model model) {
    Configuration cfg = new Configuration();
    cfg.configure("hibernate.cfg.xml");
    SessionFactory factory = cfg.buildSessionFactory();
    Session session = factory.openSession();
    Transaction t = session.beginTransaction();

    Druzyna team = session.find(Druzyna.class, Integer.parseInt(idTeam));
    team.setNazwa(teamForm.getName());/* w w  w.j  a  va 2 s .c om*/
    session.update(team);

    t.commit();
    session.close();
    return new ModelAndView("redirect:/club/" + idClub + "/sections/" + idSection + "/teams/");
}

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   www  .ja  v a  2 s .c om*/
        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  www.  j  a  v  a  2  s .c  o m
                    "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 va 2s.  c o  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  ww .ja va2  s.com
    tx.commit();

}

From source file:com.nanosolution.aecontrol.util.HibernateUtil.java

private static SessionFactory buildSessionFactory() {

    try {//ww w .j a  v  a  2  s  . c  o  m
        Configuration configuration = new Configuration();
        configuration.configure("hibernate.cfg.xml");
        ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
                .applySettings(configuration.getProperties()).build();
        SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
        return sessionFactory;
    } catch (Throwable ex) {
        System.err.println("Initial SessionFactory creation failed." + ex);
        System.err.println(ex.getCause());
        throw new ExceptionInInitializerError(ex);
    }
}

From source file:com.nbr.testhibernate.controller.DAOclass.java

private DAOclass() {
    Configuration cnf = new Configuration();
    cnf.configure("hibernate.cfg");
    ServiceRegistry srv = new StandardServiceRegistryBuilder().applySettings(cnf.getProperties()).build();
    ;//from   w ww . ja v a 2s  .c  om
    sf = cnf.buildSessionFactory(srv);

}