Example usage for org.hibernate.cfg Configuration buildSessionFactory

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

Introduction

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

Prototype

public SessionFactory buildSessionFactory() throws HibernateException 

Source Link

Document

Create a SessionFactory using the properties and mappings in this configuration.

Usage

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

@GetMapping("/edit/{idMember}")
public String editStaffMember(StaffMemberForm staffMemberForm, Model model,
        @PathVariable("idClub") String idClub, @PathVariable("idSection") String idSection,
        @PathVariable("idTeam") String idTeam, @PathVariable("idMember") String idMember) {
    Configuration cfg = new Configuration();
    cfg.configure("hibernate.cfg.xml");
    SessionFactory factory = cfg.buildSessionFactory();

    //creating session object  
    Session session = factory.openSession();

    CzlonekSztabu member = session.find(CzlonekSztabu.class, Integer.parseInt(idMember));

    model.addAttribute("club", idClub);
    model.addAttribute("Section", idSection);
    model.addAttribute("Team", idTeam);
    model.addAttribute("member", member);

    session.close();/*from  ww  w  . j a v  a 2s.co  m*/
    factory.close();
    return "/staffmember/edit_staffmember_view";
}

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

@PostMapping("/edit/{idMember}")
@ResponseBody/* w w  w.jav a  2 s  .c o m*/
public ModelAndView editStaffMember(@Valid StaffMemberForm staffMemberForm, BindingResult result, Model model,
        @PathVariable("idClub") String idClub, @PathVariable("idSection") String idSection,
        @PathVariable("idTeam") String idTeam, @PathVariable("idMember") String idMember) throws IOException {
    if (result.hasErrors()) {
        return new ModelAndView("redirect:/club/" + idClub + "/sections/" + idSection + "/teams/" + idTeam
                + "/staffmembers/edit/" + idMember);
    }

    Configuration cfg = new Configuration();
    cfg.configure("hibernate.cfg.xml");
    SessionFactory factory = cfg.buildSessionFactory();
    Session session = factory.openSession();
    Transaction t = session.beginTransaction();

    CzlonekSztabu member = session.find(CzlonekSztabu.class, Integer.parseInt(idMember));
    member.setImie(staffMemberForm.getFirstName());
    member.setNazwisko(staffMemberForm.getLastName());
    member.setStanowisko(staffMemberForm.getPosition());
    member.setPensja(staffMemberForm.getSalary());

    session.update(member);
    t.commit();
    session.close();
    factory.close();

    return new ModelAndView(
            "redirect:/club/" + idClub + "/sections/" + idSection + "/teams/" + idTeam + "/staffmembers/");

}

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

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

    //creating session object  
    Session session = factory.openSession();

    Transaction t = session.beginTransaction();

    CzlonekSztabu member = session.find(CzlonekSztabu.class, Integer.parseInt(idMember));
    session.remove(member);//from ww  w  .jav a  2s. c  o m
    t.commit();

    session.close();
    factory.close();

    return new ModelAndView(
            "redirect:/club/" + idClub + "/sections/" + idSection + "/teams/" + idTeam + "/staffmembers/");
}

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

@RequestMapping(value = "/", method = RequestMethod.GET)
public String showTeams(@PathVariable("idClub") String idClub, @PathVariable("idSection") String idSection,
        Model model) {//from   w  w  w. ja  va  2 s  . c o m

    Configuration cfg = new Configuration();
    cfg.configure("hibernate.cfg.xml");
    SessionFactory factory = cfg.buildSessionFactory();
    Session session = factory.openSession();
    Query query = session.createQuery("from Druzyna where id_sekcja=:id");
    query.setParameter("id", idSection);
    List<Druzyna> teamList = query.getResultList();
    model.addAttribute("Section", idSection);
    model.addAttribute("teamList", teamList);
    model.addAttribute("Club", idClub);
    session.close();
    return "/team/show_team_view";

}

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);/*from   w ww  . j ava 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 ww. ja v  a  2  s.  c o m*/
    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 w w w .ja  v  a  2  s  . c  om*/
    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 v a 2 s  .c  o  m*/
    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  w ww  .  java 2 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 .java 2s.co 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;

}