Example usage for org.hibernate Session update

List of usage examples for org.hibernate Session update

Introduction

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

Prototype

void update(Object object);

Source Link

Document

Update the persistent instance with the identifier of the given detached instance.

Usage

From source file:bazydanych.OneToManyTest.java

private void updateEmployee(Integer EmployeeID, String fname, String lname, int salary, Set cert) {
    Session session = factory.openSession();
    Transaction tx = null;/*from  w w w .ja  va 2 s .co  m*/
    try {
        tx = session.beginTransaction();
        Employee employee = (Employee) session.get(Employee.class, EmployeeID);
        if (fname != null) {
            employee.setFirstName(fname);
        }
        if (lname != null) {
            employee.setLastName(lname);
        }
        if (salary != -1) {
            employee.setSalary(salary);
        }
        if (cert != null) {
            employee.setCertificates(cert);
        }
        session.update(employee);
        tx.commit();
    } catch (HibernateException e) {
        if (tx != null) {
            tx.rollback();
        }
        e.printStackTrace();
        fail();
    } finally {
        session.close();
    }
}

From source file:bd.ac.seu.hibernate.FXMLDocumentController.java

@FXML
private void handleRegisterAction(ActionEvent event) {
    Session session = null;
    Transaction transaction = null;//  w ww . j  av a2 s  .  co m

    try {
        session = SessionFactorySingleton.getSessionFactory().openSession();
        Student student = studentBox.getSelectionModel().getSelectedItem();
        Course course = courseBox.getSelectionModel().getSelectedItem();
        student.addCourse(course);
        course.addStudent(student);
        transaction = session.beginTransaction();
        session.update(student);
        session.update(course);
        transaction.commit();
    } catch (Exception e) {
        if (transaction != null)
            transaction.rollback();
    } finally {
        session.close();
    }

}

From source file:Beans.AsiganarDirector.java

public void guardar() {
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    Transaction t = session.beginTransaction();
    ArrayList<UsuarioProyecto> pro = new ArrayList();
    try {//  ww  w  .  ja  v a  2 s .  c o m
        ProyectosModelo temp2 = null;
        Profesor p = null;
        p = (Profesor) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("profesor");
        boolean r = false;
        Proyectos proyec = null;
        for (int i = 0; i < pro2.size(); i++) {
            temp2 = (ProyectosModelo) pro2.get(i);
            if (temp2.isEstado() == true) {
                ArrayList<UsuarioProyecto> list_U = new ArrayList();
                UsuarioProyecto usu = new UsuarioProyecto();
                list_U = (ArrayList) session.createQuery("select us from UsuarioProyecto us inner join "
                        + " us.proyectos p where p.codigoProyecto=" + temp2.getCod_pro()).list();
                usu = list_U.get(0);
                Usuario u = (Usuario) session.load(Usuario.class,
                        new BigDecimal(Integer.parseInt(p.getPege_id())));
                usu.setUsuarioByDirector(u);
                session.update(usu);
                Estados e = (Estados) session.load(Estados.class, new BigDecimal(1));
                EstadoProyecto ep = new EstadoProyecto();
                ep = (EstadoProyecto) session.createQuery("select e from Proyectos p "
                        + " INNER JOIN p.estadoProyectos e where p.codigoProyecto=" + temp2.getCod_pro())
                        .uniqueResult();
                ep.setEstados(e);
                session.update(ep);
                r = true;
            }

        }
        t.commit();

        if (r) {
            FacesContext.getCurrentInstance().getExternalContext().redirect("AsignarDirector.xhtml");
        } else {
            FacesContext.getCurrentInstance().addMessage(null,
                    new FacesMessage(FacesMessage.SEVERITY_ERROR, "ERROR", ""));

        }

    } catch (Exception ex) {
        if (t != null) {
            t.rollback();
        }
    }
}

From source file:beans.BackList.java

public void back(Book book) {
    Session session = hibernate.HibernateUtil.getSessionFactory().openSession();
    session.beginTransaction();/* ww w .  jav  a2s.co  m*/
    book.setAvailable(true);
    session.update(book);
    session.getTransaction().commit();
    backList.remove(book);
    bookList.getBookList().add(book);
    session.close();
}

From source file:beans.Catalog.java

public String editFilm() {
    for (Datastore ds : datastoreList) {
        if (ds.toString().equals(actDatastore)) {
            editFilmIns.setDatastore(ds);
        }//from   w  w  w.java 2  s .c o  m
    }
    Session session = hibernate.HibernateUtil.getSessionFactory().openSession();
    session.beginTransaction();
    session.update(editFilmIns);
    session.getTransaction().commit();
    session.close();
    changeStatusMessage("Editon of the film was successful.");
    queryFilmsFromDB();
    return "editfilm2index";
}

From source file:beans.Catalog.java

public String editDatastore() {
    Session session = hibernate.HibernateUtil.getSessionFactory().openSession();
    session.beginTransaction();/*from   w ww.j av  a 2s . c o m*/
    session.update(editDatastoreIns);
    session.getTransaction().commit();
    session.close();
    changeStatusMessage("Editon of the datastore was successful.");
    queryFilmsFromDB();
    return "editdatastore2datastore";
}

From source file:Beans.DatosProyecto.java

public void actualizar() {
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    Transaction t = session.beginTransaction();
    Proyectos p = new Proyectos();
    Proyectos Pr = new Proyectos();
    p = (Proyectos) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("calificar");
    System.out.println("proyecto " + p.getNombre());
    try {//from w w  w  . j  a  va  2  s.  c  om
        Pr = (Proyectos) session.createQuery("from Proyectos P where P.codigoProyecto=" + p.getCodigoProyecto())
                .uniqueResult();
        Pr.setCalificacion(Long.parseLong(Calificacion));
        session.update(Pr);
        t.commit();
        FacesContext.getCurrentInstance().getExternalContext().redirect("CalificarProyecto.xhtml");
    } catch (Exception ex) {
        FacesContext.getCurrentInstance().addMessage(null,
                new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error comunicarse con el administrador", ""));
        System.out.println("Error actualizar " + ex.toString());
    }

}

From source file:beans.Lend.java

public void lend() {
    if (actMember != null) {
        Session session = hibernate.HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();//  ww w  . j a v  a 2 s. c  om
        for (Book book : lendList) {
            book.setMember(actMember);
            session.update(book);
        }
        session.getTransaction().commit();
        session.close();
        lendList.clear();
    } else {
        actMemberText = "Select a member!!!";
    }
}

From source file:Beans.ListaProfesoresBeansUpdate.java

public boolean update_disponibilidad(Disponibilidad d) {
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    Transaction t = session.beginTransaction();
    boolean r = false;
    System.out.println("----- update " + d.getCodDis() + "----" + d.getNumHoras());
    try {/*from  ww  w.j av a 2 s  . c om*/
        session.update(d);
        t.commit();
        r = true;
    } catch (Exception ex) {
        System.out.println("error " + ex.getMessage());
        r = false;

    }
    System.out.println("+++++ " + r);
    return r;
}

From source file:Beans.ListaProfesoresBeansUpdate.java

public boolean update_dias(Disponibilidad d) {
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    Transaction t = session.beginTransaction();
    boolean r = false;
    Dia dias = new Dia();
    try {//from ww w. j a v  a 2s  .  c  om
        for (int i = 0; i < Dias.size(); i++) {
            dias = (Dia) Dias.get(i);
            dias.setDisponibilidad(d);
            session.update(dias);
        }

        t.commit();
        r = true;
    } catch (Exception ex) {
        System.out.println("Error al update de dias " + ex.getMessage());
        r = false;

    }
    return r;
}