Example usage for org.hibernate Session persist

List of usage examples for org.hibernate Session persist

Introduction

In this page you can find the example usage for org.hibernate Session persist.

Prototype

void persist(Object object);

Source Link

Document

Make a transient instance persistent.

Usage

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);
    t.commit();/*  w  w w .  j  av a2s  .c  o  m*/

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

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

}

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   w ww  . j  av  a  2  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);
    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.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));
    t.commit();/*from www .ja v  a 2 s.  co  m*/
    session.close();
    factory.close();
    return new ModelAndView("redirect:/club/" + idClub + "/sections/" + idSection + "/teams/" + idTeam
            + "/players/" + idPlayer + "/");

}

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

@RequestMapping(value = "/create", method = RequestMethod.POST)
public ModelAndView createstatistics(PlayerStatisticsForm playerStatisticsForm, Model model,
        @PathVariable("idTeam") String idTeam, @PathVariable("idClub") String idClub,
        @PathVariable("idSection") String idSection, @PathVariable("idPlayer") String idPlayer) {
    model.addAttribute("Section", idSection);
    model.addAttribute("Club", idClub);
    model.addAttribute("Team", idTeam);
    model.addAttribute("Player", idPlayer);
    Configuration cfg = new Configuration();
    cfg.configure("hibernate.cfg.xml");
    SessionFactory factory = cfg.buildSessionFactory();
    Session session = factory.openSession();
    Transaction t = session.beginTransaction();

    ZawodnikStatystyki playerStatistics = new ZawodnikStatystyki();
    playerStatistics.setCzerwoneKartki(Integer.parseInt(playerStatisticsForm.getRedCards()));
    playerStatistics.setZolteKartki(Integer.parseInt(playerStatisticsForm.getYellowCards()));
    playerStatistics.setRozegraneMinuty(Integer.parseInt(playerStatisticsForm.getMinutesPlayed()));
    playerStatistics.setFaule(Integer.parseInt(playerStatisticsForm.getFaulsCommited()));
    playerStatistics.setStrzeloneBramki(Integer.parseInt(playerStatisticsForm.getScoredGoals()));
    playerStatistics.setStraconeBramki(Integer.parseInt(playerStatisticsForm.getLostGoals()));

    Query query = session.createQuery("from Sezon where rok=:rok");
    query.setParameter("rok", Integer.parseInt(playerStatisticsForm.getSeason()));
    List<Sezon> seasonList = query.getResultList();
    playerStatistics.setSezon(seasonList.get(0));

    ZawodnikStatystykiPK zspk = new ZawodnikStatystykiPK(Integer.parseInt(idPlayer),
            seasonList.get(0).getIdSezon());
    playerStatistics.setZawodnikStatystykiPK(zspk);
    session.persist(playerStatistics);
    t.commit();//from w ww . j av a  2  s.  co  m
    session.close();
    factory.close();

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

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 .  j av a2  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.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 www  .  j  av  a 2s  . 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.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());// ww w. j  a v a 2  s  .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.mycompany.dao.impl.BuyerImpl.java

@Override
public void save(Buyer buyer) {
    Session session = this.sessionFactory.getCurrentSession();
    session.persist(buyer);
}

From source file:com.mycompany.springrest.dao.RoleDAOImpl.java

@Override
public void addRole(Role role) {
    Session session = this.sessionFactory.getCurrentSession();
    session.persist(role);
    logger.info("Role saved successfully, Role Details=" + role);
}

From source file:com.mycompany.springrest.dao.UserDAOImpl.java

@Override
public void addUser(User user) {
    Session session = this.sessionFactory.getCurrentSession();
    session.persist(user);
    logger.info("User saved successfully, User Details=" + user);
}