List of usage examples for org.hibernate.cfg Configuration buildSessionFactory
public SessionFactory buildSessionFactory() throws HibernateException
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 w w w.j ava2 s .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 va 2 s. co 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);//w ww . j av a2 s. c o m 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();/*from ww w.ja v a 2s . co m*/ 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();//w ww.j a v a2 s . com 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);/*from w w w. ja v a 2s .c o 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);/*from www .j a va2 s . co 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); 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 .ja va 2 s . co 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/"); }
From source file:com.mycompany.controllers.PlayerStatisticsController.java
@RequestMapping(value = "/", method = RequestMethod.GET) public String showPlayerStatistics(@PathVariable("idClub") String idClub, @PathVariable("idSection") String idSection, @PathVariable("idTeam") String idTeam, @PathVariable("idPlayer") String idPlayer, Model model) { Configuration cfg = new Configuration(); cfg.configure("hibernate.cfg.xml"); SessionFactory factory = cfg.buildSessionFactory(); Session session = factory.openSession(); Query query = session.createQuery("from ZawodnikStatystyki where id_zawodnik=:id"); query.setParameter("id", idPlayer); List<ZawodnikStatystyki> statisticsList = query.getResultList(); Zawodnik player = session.find(Zawodnik.class, Integer.parseInt(idPlayer)); model.addAttribute("statisticsList", statisticsList); model.addAttribute("Club", idClub); model.addAttribute("Team", idTeam); model.addAttribute("Section", idSection); model.addAttribute("Player", player); session.close();//from w w w .j ava 2 s .com factory.close(); return "player/show_concreteplayer_view"; }
From source file:com.mycompany.controllers.PlayerStatisticsController.java
@RequestMapping(value = "/edit/{idSeason}", method = RequestMethod.POST) public ModelAndView editPlayerStatistics(@Valid PlayerStatisticsForm playerStatisticsForm, @PathVariable("idClub") String idClub, @PathVariable("idSection") String idSection, @PathVariable("idTeam") String idTeam, Model model, @PathVariable("idPlayer") String idPlayer, @PathVariable("idSeason") String idSeason) throws ParseException { Configuration cfg = new Configuration(); cfg.configure("hibernate.cfg.xml"); SessionFactory factory = cfg.buildSessionFactory(); Session session = factory.openSession(); Transaction t = session.beginTransaction(); Query query = session.createQuery("from ZawodnikStatystyki where id_zawodnik=:id and id_sezon=:ids"); query.setParameter("id", idPlayer); query.setParameter("ids", idSeason); List<ZawodnikStatystyki> statistic = query.getResultList(); statistic.get(0).setCzerwoneKartki(Integer.parseInt(playerStatisticsForm.getRedCards())); statistic.get(0).setZolteKartki(Integer.parseInt(playerStatisticsForm.getYellowCards())); statistic.get(0).setRozegraneMinuty(Integer.parseInt(playerStatisticsForm.getMinutesPlayed())); statistic.get(0).setFaule(Integer.parseInt(playerStatisticsForm.getFaulsCommited())); statistic.get(0).setStrzeloneBramki(Integer.parseInt(playerStatisticsForm.getScoredGoals())); statistic.get(0).setStraconeBramki(Integer.parseInt(playerStatisticsForm.getLostGoals())); session.persist(statistic.get(0));/*from w w w. j a v a2 s. c o m*/ t.commit(); session.close(); factory.close(); return new ModelAndView("redirect:/club/" + idClub + "/sections/" + idSection + "/teams/" + idTeam + "/players/" + idPlayer + "/"); }