Example usage for org.hibernate SessionFactory close

List of usage examples for org.hibernate SessionFactory close

Introduction

In this page you can find the example usage for org.hibernate SessionFactory close.

Prototype

void close() throws HibernateException;

Source Link

Document

Destroy this SessionFactory and release all resources (caches, connection pools, etc).

Usage

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

@RequestMapping(value = "/create", method = RequestMethod.POST)
public ModelAndView createsection(@Valid SectionForm sectionForm, @PathVariable("idClub") String idClub,
        Model model) {/*from w w  w  . jav  a 2 s  . co  m*/
    Configuration cfg = new Configuration();
    cfg.configure("hibernate.cfg.xml");
    SessionFactory factory = cfg.buildSessionFactory();

    Session session = factory.openSession();
    Transaction t = session.beginTransaction();
    Klub club = session.find(Klub.class, Integer.parseInt(idClub));
    Sekcja section = new Sekcja();
    section.setDyscyplina(sectionForm.getDiscipline());
    section.setPlec(sectionForm.getSex());
    section.setIdKlub(club);
    session.persist(section);
    t.commit();
    session.close();
    factory.close();
    return new ModelAndView("redirect:/club/" + idClub + "/sections/");
}

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

@RequestMapping(value = "/edit/{idSection}", method = RequestMethod.GET)
public String sectionEdit(SectionForm sectionForm, @PathVariable("idSection") String idSection, Model model) {
    Configuration cfg = new Configuration();
    cfg.configure("hibernate.cfg.xml");
    SessionFactory factory = cfg.buildSessionFactory();

    //creating session object  
    Session session = factory.openSession();
    Sekcja section = session.find(Sekcja.class, Integer.parseInt(idSection));
    model.addAttribute("section", section);
    session.close();//from w w w .jav a2s  . c  o  m
    factory.close();
    return "/section/edit_section_view";
}

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

@RequestMapping(value = "/edit/{idSection}", method = RequestMethod.POST)
public ModelAndView sectionedit(@Valid SectionForm sectionForm, @PathVariable("idSection") String idSection,
        @PathVariable("idClub") String idClub, Model model) {
    Configuration cfg = new Configuration();
    cfg.configure("hibernate.cfg.xml");
    SessionFactory factory = cfg.buildSessionFactory();

    //creating session object  
    Session session = factory.openSession();
    Transaction t = session.beginTransaction();
    Sekcja section = session.find(Sekcja.class, Integer.parseInt(idSection));

    section.setDyscyplina(sectionForm.getDiscipline());
    section.setPlec(sectionForm.getSex());
    session.update(section);//from  www .ja  va 2  s.  co  m

    t.commit();

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

    return new ModelAndView("redirect:/club/" + section.getIdKlub() + "/sections/");

}

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

@RequestMapping(value = "/create", method = RequestMethod.POST)
public ModelAndView createmember(@Valid StaffMemberForm staffMemberForm, @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();

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

    Transaction t = session.beginTransaction();

    Druzyna druzyna = session.find(Druzyna.class, Integer.parseInt(idTeam));

    CzlonekSztabu member = new CzlonekSztabu();
    member.setImie(staffMemberForm.getFirstName());
    member.setNazwisko(staffMemberForm.getLastName());
    member.setStanowisko(staffMemberForm.getPosition());
    member.setPensja(staffMemberForm.getSalary());
    member.setIdDruzyna(druzyna);/*from  ww w  .j  a  va2 s.  c  o  m*/

    session.persist(member);
    t.commit();

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

    model.addAttribute("club", idClub);
    model.addAttribute("Section", idSection);
    model.addAttribute("Team", idTeam);
    return new ModelAndView(
            "redirect:/club/" + idClub + "/sections/" + idSection + "/teams/" + idTeam + "/staffmembers/");

}

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  w  w w .  j ava2 s  . c  o  m
    factory.close();
    return "/staffmember/edit_staffmember_view";
}

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

@PostMapping("/edit/{idMember}")
@ResponseBody//from  ww  w .j  a  v  a 2s . 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);// ww  w. j ava2  s.c om
    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

@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  av a2 s  .  c  o  m*/
    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 = "/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 ww. j  ava 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.ola.firsthibernateproject.Main.java

/**
 * @param args the command line arguments
 *//*from  w w w .  java  2 s .  c  o  m*/
public static void main(String[] args) {
    // TODO code application logic here

    /*StudentCertification studentCertification1 = new StudentCertification();
    studentCertification1.setCertifiactionName("Core JAVA Certification Exam");
            
    StudentCertification studentCertification2 = new StudentCertification();
    studentCertification2.setCertifiactionName("Oracle DB Certification Exam");
            
    StudentAddress studentAddress = new StudentAddress();
    studentAddress.setAddressDetail("Bialystok, Polska");
            
    Student student = new Student();
    student.setName("Ola5");
    student.setStudentAddress(studentAddress);
    student.getStudentCertification().add(studentCertification1);
    //student.setBirthDate(new Date());
            
    StudentDetail studentDetail = new StudentDetail();
    studentDetail.setStudentMobileNumber("123456789");
    studentDetail.setStudent(student); //to musi byc
            
    student.setStudentDetail(studentDetail); //to niekoniecznie(jezeli robimy save(studentDetail)), jezeli nie robimy save(studentDetail) to nie bedzie w bazie wstawionych wierszy STUDENT_DETAIL 
            
    Student student2 = new Student();
    student2.setName("Kasia");
    student2.setStudentAddress(studentAddress);
    student2.getStudentCertification().add(studentCertification1);
    student2.getStudentCertification().add(studentCertification2);
    //student.setBirthDate(new Date());
            
    StudentDetail studentDetail2 = new StudentDetail();
    studentDetail2.setStudentMobileNumber("123456780");
    studentDetail2.setStudent(student2); //to musi byc
            
    student2.setStudentDetail(studentDetail2);
            
    (studentAddress.getStudents()).add(student); //jezeli robimy save(studentAddress) to od razu wszystko nam sie wstawia, jezeli robilibysmy save(student) save(student2) to byloby to niepotrzebne
    (studentAddress.getStudents()).add(student2);
            
    SessionFactory sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
    Session session = sessionFactory.openSession();
    session.beginTransaction();
            
    //session.save(student);
    //session.save(student2);
    session.save(studentAddress);
            
    session.getTransaction().commit();
    session.close();
    sessionFactory.close();*/

    Student student = new Student();
    student.setName("Ala");

    SessionFactory sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
    Session session = sessionFactory.openSession();
    session.beginTransaction();

    student.setName("Ala modified before save function");
    session.save(student);
    student = (Student) session.get(Student.class, 1); //pobieramy Studenta o id=1
    System.out.println("Student Object having student name as: " + student.getName());

    student.setName("Modifying student name id as 2");
    //student.setName("AlaUpdate"); //o dziwo jezeli zrobilismy session.save(student) i potem to, to w bazie memy AlaUpdate, update sam sie robi, bez pisania (do zamkniecia sesji) :O, save dziala w trybie persistent(?), nie transient
    //session.update(student);
    //session.delete(student);
    //session.delete(student);//change persistent state to transient

    session.getTransaction().commit();
    session.close();

    student.setName("modifying student name in the detached state");

    Session session2 = sessionFactory.openSession();
    session2.beginTransaction();

    session2.update(student); //zmienia stan z detached na persistent :)
    student.setName("modifying student after detached state ufter update");

    session2.getTransaction().commit();
    session2.close();

    sessionFactory.close();
}