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(String entityName, Object object);

Source Link

Document

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

Usage

From source file:Beans.ListaProfesoresBeans.java

public void delete(String pege_id) throws IOException {
        Disponibilidad d = new Disponibilidad();
        d = cod_dispoProfe(pege_id);/* w  w  w.  j  av a2s. c om*/
        ArrayList n = null;
        n = BuscarDispoAsesoria(d);
        System.out.println("Entroo delete  pege " + pege_id + " dispo " + d.getCodDis());
        if (n.size() > 0) {
            System.out.println("entro opcion 1");
            Session session = HibernateUtil.getSessionFactory().getCurrentSession();
            Transaction t = session.beginTransaction();
            try {
                d.setEstado("I");
                session.update("Disponibilidad", d);
                t.commit();

            } catch (Exception ex) {
                System.out.println("Error " + ex.toString());
                FacesContext.getCurrentInstance().addMessage(null,
                        new FacesMessage(FacesMessage.SEVERITY_ERROR, "ERROR", ""));

            }
        } else if (n.size() == 0) {
            System.out.println("entro opcion 2");
            try {
                BorrarDispo(d);
                System.out.println("-- 1");
                borrarDias(d);
                System.out.println("-- 2");
                Session session = HibernateUtil.getSessionFactory().openSession();
                Transaction t = session.beginTransaction();
                System.out.println("-- 3");
                session.delete("Disponibilidad", d);
                System.out.println("-- 4");
                t.commit();

            } catch (Exception ex) {
                System.out.println("Error hiber" + ex.toString());
                FacesContext.getCurrentInstance().addMessage(null,
                        new FacesMessage(FacesMessage.SEVERITY_ERROR, "ERROR", ""));

            }
        }
        FacesContext.getCurrentInstance().getExternalContext().redirect("Lista_Profesor.xhtml");

    }

From source file:Beans.ListaProyectosBeans.java

public void guardarCambios(String pege_id) {
    System.out.println("entro " + pege_id);
    ProyectosModelo p = new ProyectosModelo();
    if (FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("cambio_dire") != null) {
        System.out.println("exite");
    } else {/*from www  . j a va2s. c  o m*/
        System.out.println("No existe");
    }
    p = (ProyectosModelo) FacesContext.getCurrentInstance().getExternalContext().getSessionMap()
            .get("cambio_dire");
    System.out.println("proyecto " + p.getCod_pro());
    Session session = HibernateUtil.getSessionFactory().openSession();
    Transaction t = session.beginTransaction();
    ArrayList<UsuarioProyecto> up = new ArrayList();
    try {
        up = (ArrayList) session.createQuery("select up from UsuarioProyecto"
                + " up INNER JOIN up.proyectos p INNER JOIN up.usuarioByDirector u" + " where p.codigoProyecto="
                + p.getCod_pro()).list();
        System.out.println("proycetos " + up.size());
        Usuario u = new Usuario();
        u = traerUsu(pege_id);
        System.out.println("usuario qe trajo " + u.getUsuario());
        UsuarioProyecto temp = null;
        for (int i = 0; i < up.size(); i++) {
            System.out.println("cambio " + i);
            temp = (UsuarioProyecto) up.get(i);
            temp.setUsuarioByDirector(u);
            session.update("UsuarioProyecto", temp);
        }
        t.commit();
    } catch (Exception ex) {
        System.out.println("Error guardarCambios " + ex.toString());
    }
}

From source file:com.abiquo.abiserver.commands.impl.VirtualApplianceCommandImpl.java

License:Open Source License

/**
 * Private helper to update the state and the sub-state of a virtual appliance in the database.
 * /*from  www.j a v a2  s. c om*/
 * @param virtualappliance The Virtual appliance to update.
 * @param state The new state to update.
 * @param subState the new SubState to update.
 * @return A basic Result with the operation result.
 */
private DataResult<VirtualAppliance> updateStateAndSubStateInDB(VirtualAppliance virtualappliance,
        final State state, final State subState) {
    DataResult<VirtualAppliance> dataResult = new DataResult<VirtualAppliance>();
    Session session = null;
    Transaction transaction = null;

    try {
        session = HibernateUtil.getSession();
        transaction = checkOpenTransaction(session).getTransaction();// session.beginTransaction();

        VirtualappHB virtualAppliance = (VirtualappHB) session.get("VirtualappExtendedHB",
                virtualappliance.getId());

        virtualAppliance.setState(state.toEnum());
        // virtualAppliance.setSubState(subState.toEnum());

        session.update("VirtualappHB", virtualAppliance);

        virtualappliance = virtualAppliance.toPojo();

        transaction.commit();

        dataResult.setData(virtualappliance);
    } catch (HibernateException e) {
        if (transaction != null && transaction.isActive()) {
            transaction.rollback();
        }

        errorManager.reportError(resourceManager, dataResult, "updateStateInDB", e);
    }

    return dataResult;
}

From source file:com.abiquo.abiserver.commands.impl.VirtualApplianceCommandImpl.java

License:Open Source License

@Override
public DataResult<VirtualAppliance> updateStateInDB(VirtualAppliance virtualappliance,
        final StateEnum newState) {
    DataResult<VirtualAppliance> dataResult;
    dataResult = new DataResult<VirtualAppliance>();
    dataResult.setSuccess(true);//  ww  w. j a v a  2  s. c  om
    Session session = null;
    Transaction transaction = null;
    try {
        session = HibernateUtil.getSession();
        transaction = session.beginTransaction();

        VirtualappHB virtualAppPojo = (VirtualappHB) session.get("VirtualappExtendedHB",
                virtualappliance.getId());
        virtualAppPojo.setState(newState);
        // virtualAppPojo.setSubState(newState);
        session.update("VirtualappHB", virtualAppPojo);
        virtualappliance = virtualAppPojo.toPojo();
        transaction.commit();

        dataResult.setData(virtualappliance);
    } catch (HibernateException e) {
        if (transaction != null && transaction.isActive()) {
            transaction.rollback();
        }

        errorManager.reportError(resourceManager, dataResult, "updateStateInDB", e);
    }
    return dataResult;
}

From source file:com.abiquo.abiserver.commands.impl.VirtualApplianceCommandImpl.java

License:Open Source License

@Override
public DataResult<VirtualAppliance> updateOnlyStateInDB(VirtualAppliance virtualappliance,
        final StateEnum newState) {
    DataResult<VirtualAppliance> dataResult;
    dataResult = new DataResult<VirtualAppliance>();
    dataResult.setSuccess(true);// www .  j a  v a  2  s  .  c  o m
    Session session = null;
    Transaction transaction = null;
    try {
        session = HibernateUtil.getSession();
        transaction = session.beginTransaction();

        VirtualappHB virtualAppPojo = (VirtualappHB) session.get("VirtualappExtendedHB",
                virtualappliance.getId());
        virtualAppPojo.setState(newState);
        session.update("VirtualappHB", virtualAppPojo);
        virtualappliance = virtualAppPojo.toPojo();
        transaction.commit();

        dataResult.setData(virtualappliance);
    } catch (HibernateException e) {
        if (transaction != null && transaction.isActive()) {
            transaction.rollback();
        }

        errorManager.reportError(resourceManager, dataResult, "updateStateInDB", e);
    }
    return dataResult;
}