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.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.  ja  v  a  2 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 av a  2 s.  c o  m*/
    t.commit();

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

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

}

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

@GetMapping("/edit/{idLeague}")
public String editLeague(LeagueForm leagueForm, Model model, @PathVariable("idLeague") String idLeague) {
    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(idLeague));

    model.addAttribute("league", league);

    session.close();//from ww  w.  j av a  2s  . c  o m
    factory.close();
    return "/league/edit_league_view";
}

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

@PostMapping("/edit/{idLeague}")
@ResponseBody//from   www  . j a  va2s  .  c  o  m
public ModelAndView editLeague(@Valid LeagueForm leagueForm, BindingResult result, Model model,
        @PathVariable("idLeague") String idLeague) throws IOException {
    if (result.hasErrors()) {
        return new ModelAndView("redirect:/league/edit/" + idLeague);
    }

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

    Liga league = session.find(Liga.class, Integer.parseInt(idLeague));
    league.setNazwa(leagueForm.getName());
    league.setKraj(leagueForm.getCountry());

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

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

}

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

@GetMapping("/remove/{idLeague}")
public ModelAndView removeLeague(Model model, @PathVariable("idLeague") String idLeague) {
    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 = session.find(Liga.class, Integer.parseInt(idLeague));
    session.remove(league);/*from  w  w  w. j a v  a2s  . com*/
    t.commit();

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

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

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

@RequestMapping({ "/home", "/" })
public String homePage(Model model) {

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

    //creating session object  
    Session session = factory.openSession();
    List<Klub> clubList = session.createCriteria(Klub.class).list();
    model.addAttribute("clubList", clubList);
    session.close();//  www. j ava  2 s  . c om
    factory.close();

    return "/home_view";
}

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

@RequestMapping(value = "/", method = RequestMethod.GET)
public String showPlayers(@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();
    Query query = session.createQuery("from Zawodnik where id_druzyna=:id");
    query.setParameter("id", idTeam);
    List<Zawodnik> playerList = query.getResultList();
    model.addAttribute("playersList", playerList);
    model.addAttribute("Club", idClub);
    model.addAttribute("Team", idTeam);
    model.addAttribute("Section", idSection);
    session.close();/*from w  w  w  .j a va  2 s.  c  o  m*/
    return "player/show_player_view";
}

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

@GetMapping("/remove/{idPlayer}")
public ModelAndView removePlayer(Model model, @PathVariable("idClub") String idClub,
        @PathVariable("idSection") String idSection, @PathVariable("idTeam") String idTeam,
        @PathVariable("idPlayer") String idPlayer) {
    Configuration cfg = new Configuration();
    cfg.configure("hibernate.cfg.xml");
    SessionFactory factory = cfg.buildSessionFactory();
    Session session = factory.openSession();
    Transaction t = session.beginTransaction();
    Zawodnik player = session.find(Zawodnik.class, Integer.parseInt(idPlayer));
    session.remove(player);/*w ww. ja  va 2  s . co  m*/
    t.commit();
    session.close();
    factory.close();
    return new ModelAndView(
            "redirect:/club/" + idClub + "/sections/" + idSection + "/teams/" + idTeam + "/players/");
}

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

@RequestMapping(value = "/create", method = RequestMethod.POST)
public ModelAndView createplayer(@Valid PlayerForm playerForm, @PathVariable("idClub") String idClub,
        @PathVariable("idSection") String idSection, @PathVariable("idTeam") String idTeam, Model model)
        throws ParseException {
    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));

    Zawodnik player = new Zawodnik();
    player.setImie(playerForm.getName());

    String year = playerForm.getYear();
    String day = playerForm.getDay();
    String month = playerForm.getMonth();

    DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
    StringBuilder s = new StringBuilder(year);
    s.append("-");
    s.append(month);//www.j a  v a2  s .  c  om
    s.append("-");
    s.append(day);
    Date myDate = formatter.parse(s.toString());
    java.sql.Date sqlDate = new java.sql.Date(myDate.getTime());
    player.setDataUrodzenia(sqlDate);
    player.setNazwisko(playerForm.getLastname());
    player.setWaga(Integer.parseInt(playerForm.getWeight()));
    player.setWzrost(Integer.parseInt(playerForm.getHeight()));
    player.setIdDruzyna(team);
    session.persist(player);
    t.commit();
    session.close();
    factory.close();
    return new ModelAndView(
            "redirect:/club/" + idClub + "/sections/" + idSection + "/teams/" + idTeam + "/players/");
}

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

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

    Zawodnik player = session.find(Zawodnik.class, Integer.parseInt(idPlayer));
    player.setImie(playerForm.getName());
    player.setNazwisko(playerForm.getLastname());
    String year = playerForm.getYear();
    String day = playerForm.getDay();
    String month = playerForm.getMonth();
    player.setWaga(Integer.parseInt(playerForm.getWeight()));
    player.setWzrost(Integer.parseInt(playerForm.getHeight()));
    DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
    StringBuilder s = new StringBuilder(year);
    s.append("-");
    s.append(month);//from  w  w  w .j a  v a2 s . c o  m
    s.append("-");
    s.append(day);
    Date myDate = formatter.parse(s.toString());
    java.sql.Date sqlDate = new java.sql.Date(myDate.getTime());
    player.setDataUrodzenia(sqlDate);
    t.commit();
    session.close();
    return new ModelAndView(
            "redirect:/club/" + idClub + "/sections/" + idSection + "/teams/" + idTeam + "/players/");

}