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:model.AddFestival_dao.java

public void addFestival(Festival festival, Set<Link> set, List<Attendencie> per, List<Media> media) {
    Session session = NewHibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();/*from  w  w  w .  j  av a 2  s. c  o  m*/
    Festival f = new Festival();
    f.setName(festival.getName());
    f.setPlace(festival.getPlace());
    f.setStartDate(festival.getStartDate());
    f.setEndDate(festival.getEndDate());
    f.setRating(0.0);
    f.setNumRatings(0);
    f.setViewCount(0);
    f.setTicketsSold(0);
    f.setAllDays(festival.getAllDays());
    f.setSingleDay(festival.getSingleDay());
    f.setDescription(festival.getDescription());
    f.setTicketsPerDay(festival.getTicketsPerDay());
    f.setTicketsPerReservation(festival.getTicketsPerReservation());

    session.persist(f);
    session.getTransaction().commit();

    for (Link l : set) {
        if (!l.getLink().equals("")) {
            l.setFestival(f);
            l.getId().setIdFestival(f.getIdFestival());
            addLink(l);
        }

    }

    for (Attendencie a : per) {
        a.setFestival(f);
        a.getId().setIdFestival(f.getIdFestival());
        addPerformer(a);
    }

    for (Media m : media) {
        m.setFestival(f);
        addMedia(m);
    }
}

From source file:model.AddFestival_dao.java

private void addLink(Link l) {
    Session session = NewHibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();//from  ww  w  .j a v  a  2s  . com
    Link link = new Link();
    link.setFestival(l.getFestival());
    link.setLink(l.getLink());
    link.setId(l.getId());
    session.persist(link);
    session.getTransaction().commit();
}

From source file:model.AddFestival_dao.java

private void addPerformer(Attendencie a) {
    Session session = NewHibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();//  ww w  . jav  a2s .  com
    Attendencie attendencie = new Attendencie();
    attendencie.setFestival(a.getFestival());
    attendencie.setId(a.getId());
    attendencie.setStartTime(a.getStartTime());
    attendencie.setEndTime(a.getEndTime());
    session.persist(attendencie);
    session.getTransaction().commit();
}

From source file:model.AddFestival_dao.java

private void addMedia(Media m) {
    Session session = NewHibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();/* w w w.j ava  2  s . co  m*/
    Media media = new Media();
    media.setFestival(m.getFestival());
    media.setFile(m.getFile());
    session.persist(media);
    session.getTransaction().commit();
}

From source file:model.daos.AlumnoDAO.java

public void create(Alumno a) throws Exception {
    Session session = NewHibernateUtil.getSessionFactory().openSession();
    Transaction tx = session.getTransaction();
    try {// w  w w.  ja  v a  2  s . co m
        tx.begin();
        session.persist(a);
        //session.save(a);
        tx.commit();

        session.close();
    } catch (HibernateError he) {
        if (tx != null && tx.isActive())
            tx.rollback();
        System.out.println("Se ha cerrado la transaccion");
    }
}

From source file:model.daos.ProfesorDAO.java

public int create(Profesor p) throws Exception {
    Session session = NewHibernateUtil.getSessionFactory().openSession();
    Transaction tx = session.getTransaction();
    try {/*ww w.jav a2s. c  o  m*/
        tx.begin();
        session.persist(p);
        //session.save(a);
        tx.commit();

        session.close();
        return 1;
    } catch (HibernateError he) {
        if (tx != null && tx.isActive())
            tx.rollback();
        System.out.println("Se ha cerrado la transaccion");
        return 0;
    }
}

From source file:model.TESTE.java

public void hibernate() {
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    session.getTransaction().begin();/*from   ww w .ja v  a  2 s . c o m*/
    List<Endereco> enderecos = new ArrayList<>();
    try {
        Cliente c = new Cliente();
        c.setCpf("215.049.798-00");
        c.setEmail("jsilva.moises@gmail.com");
        //c.setEnderecos(null);
        c.setNome("MOISES JUVENAL DA SILVA");
        c.setTipoPessoa(TipoPessoa.JURIDICA);

        Endereco endereco = new Endereco();
        endereco.setCep("13.221-301");
        endereco.setCidade("JUNDIAI");
        endereco.setCliente(c);
        endereco.setComplemento("CASA");
        endereco.setLogradouro("RUA MARIANA");
        endereco.setNumero("158");
        endereco.setUf("NG");
        enderecos.add(endereco);

        endereco = new Endereco();
        endereco.setCep("13.221-300");
        endereco.setCidade("VARZEA PAULISTA");
        endereco.setCliente(c);
        endereco.setComplemento("CADA");
        endereco.setLogradouro("RUA BERTIOGA");
        endereco.setNumero("49");
        endereco.setUf("SP");

        enderecos.add(endereco);
        c.setEnderecos(enderecos);

        session.persist(c);
        session.getTransaction().commit();
    } catch (Exception e) {
        session.getTransaction().rollback();
    }
}

From source file:model.User_dao.java

public void addMedia(Media m, Integer idFestival) {
    Session session = NewHibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();//w  w  w .  ja v a 2  s . c o  m
    Media media = new Media();
    media.setFestival(m.getFestival());
    media.setFile(m.getFile());
    session.persist(media);
    session.getTransaction().commit();
}

From source file:Modelo.AlumnoDAO.java

/**
 * Mtodo que guarda una alumno en la base de datos
 * @param alumno //from  w  w  w  .  java  2s  .c  o  m
 */
public void guardar(Alumno alumno) {

    Session session = sessionFactory.openSession();
    Transaction tx = null;
    try {
        tx = session.beginTransaction();

        session.persist(alumno);

        tx.commit();
    } catch (Exception e) {
        if (tx != null) {
            tx.rollback();
        }
        e.printStackTrace();
    } finally {
        session.close();
    }

}

From source file:Modelo.EstudianteDAO.java

/**
 * Mtodo que guarda una estudiante en la base de datos
 * @param estudiante //from   ww  w  . j a v a  2s  . co  m
 */
public void guardar(Estudiante estudiante) {

    Session session = sessionFactory.openSession();
    Transaction tx = null;
    try {
        tx = session.beginTransaction();

        session.persist(estudiante);

        tx.commit();
    } catch (Exception e) {
        if (tx != null) {
            tx.rollback();
        }
        e.printStackTrace();
    } finally {
        session.close();
    }

}