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.BuildingController.java

@RequestMapping(value = "/create", method = RequestMethod.POST)
public ModelAndView createbuilding(@Valid BuildingForm buildingForm, @PathVariable("idClub") String idClub,
        Model model) {//from  w w w . j av  a 2  s  .com
    Configuration cfg = new Configuration();
    cfg.configure("hibernate.cfg.xml");
    SessionFactory factory = cfg.buildSessionFactory();

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

    Transaction t = session.beginTransaction();

    Klub klub = session.find(Klub.class, Integer.parseInt(idClub));
    Budynek building = new Budynek();
    building.setKodPocztowy(buildingForm.getPostcode());
    building.setMiejscowosc(buildingForm.getCity());
    building.setUlicanumer(buildingForm.getStreet());
    building.setIdKlub(klub);
    session.persist(building);

    if (buildingForm.getCapacity() != null) {
        Stadion arena = new Stadion(building.getIdbudynek());
        arena.setDyscyplina(buildingForm.getDiscipline());
        arena.setIloscMiejsc(Integer.parseInt(buildingForm.getCapacity()));
        arena.setNazwa(buildingForm.getName());
        arena.setBudynek(building);
        session.persist(arena);
    } else if (buildingForm.getDiscipline() != null) {
        ObiektTreningowy ot = new ObiektTreningowy(building.getIdbudynek());
        ot.setBudynek(building);
        session.persist(ot);
    } else {
        BudynekAdministracyjny ba = new BudynekAdministracyjny(building.getIdbudynek());
        ba.setBudynek(building);
        session.persist(ba);
    }

    t.commit();

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

    return new ModelAndView("redirect:/club/" + idClub + "/buildings/");

}

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

@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public String clubPage(@PathVariable("id") String id, Model model) {

    Configuration cfg = new Configuration();
    cfg.configure("hibernate.cfg.xml");
    SessionFactory factory = cfg.buildSessionFactory();

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

    Klub club = session.find(Klub.class, Integer.parseInt(id));
    model.addAttribute("club", club);

    session.close();/*from   w  w w . j  a va 2 s . c o  m*/
    factory.close();

    return "/club/show_club_view";
}

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

@RequestMapping(value = "/image/{id}", method = RequestMethod.GET)
@ResponseBody//from   w  w w .  ja va 2 s.com
public byte[] clubPhoto(@PathVariable("id") String id) throws SQLException {
    Configuration cfg = new Configuration();
    cfg.configure("hibernate.cfg.xml");
    SessionFactory factory = cfg.buildSessionFactory();

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

    Klub club = session.find(Klub.class, Integer.parseInt(id));

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

    return club.getByteLogo();
}

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

@PostMapping("/create")
@ResponseBody/*w  w  w  . jav a2s  . c om*/
public ModelAndView createClub(@Valid ClubForm clubForm, BindingResult result, Model model) throws IOException {
    if (result.hasErrors()) {
        return new ModelAndView("redirect:/club/create");
    }

    byte[] bytes;
    bytes = clubForm.getLogo().getBytes();
    bytes = LogoConvertion(bytes);

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

    Klub club = new Klub();
    club.setNazwa(clubForm.getName());

    LobCreator lcreator = Hibernate.getLobCreator(session);
    Blob blob = (Blob) lcreator.createBlob(bytes);
    club.setLogo(blob);

    session.persist(club);
    t.commit();
    session.close();
    factory.close();

    model.addAttribute("club", club);
    return new ModelAndView("redirect:/club/" + club.getIdKlub());

}

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

@GetMapping("/{id}/edit")
public String editClub(ClubForm clubForm, Model model, @PathVariable("id") String id) {
    Configuration cfg = new Configuration();
    cfg.configure("hibernate.cfg.xml");
    SessionFactory factory = cfg.buildSessionFactory();

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

    Klub club = session.find(Klub.class, Integer.parseInt(id));
    model.addAttribute("club", club);

    session.close();/*from   www.  j  a  va2 s  .  c om*/
    factory.close();
    return "/club/edit_club_view";
}

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

@PostMapping("/{id}/edit")
@ResponseBody/*from www .ja  v a2s.c  om*/
public ModelAndView editClub(@Valid ClubForm clubForm, BindingResult result, Model model,
        @PathVariable("id") String id) throws IOException {
    if (result.hasErrors()) {
        return new ModelAndView("redirect:/club/" + id + "/edit");
    }

    byte[] bytes;
    bytes = clubForm.getLogo().getBytes();
    bytes = LogoConvertion(bytes);

    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(id));
    club.setNazwa(clubForm.getName());

    LobCreator lcreator = Hibernate.getLobCreator(session);
    Blob blob = (Blob) lcreator.createBlob(bytes);
    club.setLogo(blob);

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

    model.addAttribute("club", club);
    return new ModelAndView("redirect:/club/" + club.getIdKlub());

}

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

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

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

    Transaction t = session.beginTransaction();

    Klub club = session.find(Klub.class, Integer.parseInt(id));
    session.remove(club);//from www . j a  va2 s.  com
    t.commit();

    session.close();
    factory.close();
    return new ModelAndView("redirect:/home");
}

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

@RequestMapping(value = "/", method = RequestMethod.GET)
public String leaguePage(Model model) {

    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 a  v a2  s.c  o m

    return "/league/show_leagues_view";
}

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

@RequestMapping(value = "/show/{leagueId}", method = RequestMethod.GET)
public String clubPage(@PathVariable("leagueId") String leagueId, Model model) {

    Configuration cfg = new Configuration();
    cfg.configure("hibernate.cfg.xml");
    SessionFactory factory = cfg.buildSessionFactory();

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

    Liga league = session.find(Liga.class, Integer.parseInt(leagueId));
    model.addAttribute("league", league);

    Query query = session.createQuery("from Druzyna where id_liga=:id");
    query.setParameter("id", league.getIdLiga());
    List<Druzyna> teamList = query.getResultList();
    model.addAttribute("teamList", teamList);

    session.close();/*from  w  ww.  j  ava2  s .c  o m*/
    factory.close();

    return "/league/show_concrete_league_view";
}

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

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

    Liga league = new Liga();
    league.setNazwa(leagueForm.getName());
    league.setKraj(leagueForm.getCountry());

    session.persist(league);/*  w  w w.j a  va 2s . com*/
    t.commit();

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

    return new ModelAndView("redirect:/leagues/");

}