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:co.com.ordersAgiles.controlador.OrdprfDaoImplements.java

@Override
public void modificar(Ordprf perfil) {

    System.out.println("Modificando en bd");

    Session sesion = null;

    try {//from  w w w.  j  av a  2  s.co  m
        sesion = NewHibernateUtil.getSessionFactory().openSession();
        sesion.beginTransaction();
        sesion.update(perfil);
        sesion.getTransaction().commit();
        sesion.flush();
    } catch (Exception e) {
        System.out.println("mensaje:" + e.getMessage());
        sesion.getTransaction().rollback();
    } finally {
        if (sesion != null) {
            sesion.close();
        }
    }
}

From source file:co.com.ordersAgiles.controlador.OrdprmDaoImplements.java

@Override
public void modificar(Ordprm permiso) {

    System.out.println("Modificando en bd");

    Session sesion = null;

    try {//from w w  w. j  a  va 2s . c  om
        sesion = NewHibernateUtil.getSessionFactory().openSession();
        sesion.beginTransaction();
        sesion.update(permiso);
        sesion.getTransaction().commit();
        sesion.flush();
    } catch (Exception e) {
        System.out.println("mensaje:" + e.getMessage());
        sesion.getTransaction().rollback();
    } finally {
        if (sesion != null) {
            sesion.close();
        }
    }
}

From source file:co.edu.udea.profarq.labcinco.dao.hibernate.impl.AbstractEntityDAO.java

public Serializable update(IEntityContext entityContext) throws LabCincoProfArqDAOException {
    Session session;
    Transaction transaction = null;/*from  w  ww  .j a v a2s  .c om*/

    try {
        SessionFactory sessionFactory = HibernateUtil.getSessionFactory();

        session = sessionFactory.openSession();
        transaction = session.beginTransaction();
        session.update(entityContext);
        transaction.commit();
        session.close();
    } catch (Exception ex) {
        if (transaction != null) {
            transaction.rollback();
        }

        throw new LabCincoProfArqDAOException(String.format("Error during procedure's \"%s\" for class: %s",
                "Update", entityContext.getClass().getSimpleName()), ex);
    }

    return (entityContext.getPrimaryKey());
}

From source file:co.id.admin.dao.impl.UserDaoImpl.java

@Override
public void updateUser(User user) {
    //User us=null;
    Transaction trns = null;//w  w w.  j a  va  2 s .c  om
    Session session = HibernateUtil.getSessionFactory().openSession();
    try {
        trns = session.beginTransaction();
        //(User) session.load(User.class, user.getUserId());
        String queryString = "from User where id=:id";
        Query query = session.createQuery(queryString);
        query.setParameter("id", user.getUserId());
        // User us=(User)query.list().get(0);
        user.setFirstName(user.getFirstName());
        user.setLastName(user.getLastName());
        user.setEmail(user.getEmail());
        user.setDob(user.getDob());
        session.update(user);
        trns.commit();
    } catch (RuntimeException e) {
        if (trns != null) {
            trns.rollback();
        }
        e.printStackTrace();
    } finally {
        session.flush();
        session.close();
    }
}

From source file:co.udea.edu.compumovil.gr10.discoapp.data.dao.implement.EventoDaoImplement.java

@Override
public void updateEvento(Evento evento) {
    Session session = null;

    try {/*  www . jav  a2  s . c o m*/
        session = HibernateSessionFactory.getInstance().getSession();
        Transaction transaction = null;
        try {
            transaction = session.getTransaction();
            transaction.begin();
            session.update(evento);
            transaction.commit();
        } catch (Exception e) {
            e.printStackTrace();
            transaction.rollback();
        }

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

    }
}

From source file:co.udea.edu.compumovil.gr10.discoapp.data.dao.implement.SolicitudCancionDaoImplement.java

@Override
public void updatePeticion(SolicitudCancion solicitud) {
    Session session = null;
    try {/*from  w ww .  j  av a2  s  . co m*/
        session = HibernateSessionFactory.getInstance().getSession();
        Transaction transaction = null;
        try {
            transaction = session.getTransaction();
            transaction.begin();
            session.update(solicitud);
            transaction.commit();
        } catch (Exception e) {
            e.printStackTrace();
            transaction.rollback();
        }

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

}

From source file:co.udea.edu.compumovil.gr10.discoapp.data.dao.implement.UsuarioDaoImplement.java

@Override
public boolean updateUsuario(Usuario usuario) {
    Session session = null;
    Usuario usuario2 = this.getUsuario(usuario.getIdUsuario());
    if (usuario2 == null) {
        return false;
    }/*from  w  ww  .ja  v  a  2  s. com*/

    try {
        session = HibernateSessionFactory.getInstance().getSession();
        Transaction transaction = null;
        try {
            transaction = session.getTransaction();
            transaction.begin();
            session.update(usuario);
            transaction.commit();
        } catch (Exception e) {
            System.out.println(e.getMessage());
            System.out.println(e.getCause());
            transaction.rollback();
            return false;
        }

    } catch (Exception e) {
        System.out.println(e.getMessage());
        System.out.println(e.getCause());
        return false;
    } finally {
        if (session != null) {
            session.close();
        }

    }
    return true;
}

From source file:com.abiquo.abiserver.business.authentication.AuthenticationManagerDB.java

License:Open Source License

@Override
public BasicResult checkSession(final UserSession userSession) {
    BasicResult checkSessionResult = new BasicResult();

    Session session = null;
    Transaction transaction = null;//from  w  w w .j  a v  a 2  s .c  om

    UserSession sessionToCheck = null;

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

        sessionToCheck = (UserSession) HibernateUtil.getSession().createCriteria(UserSession.class)
                .add(Restrictions.eq("user", userSession.getUser()))
                .add(Restrictions.eq("key", userSession.getKey())).uniqueResult();

        if (sessionToCheck == null) {
            // The session does not exist, so is not valid
            checkSessionResult.setResultCode(BasicResult.SESSION_INVALID);
            logger.trace("Invalod session. Please login again");
        } else {
            // Checking if the session has expired
            Date currentDate = new Date();
            if (currentDate.before(sessionToCheck.getExpireDate())) {
                // The session is valid updating the expire Date
                int sessionTimeout = abiConfig.getSessionTimeout();
                long expireMilis = new Date().getTime() + sessionTimeout * 60 * 1000;
                Date expireDate = new Date(expireMilis);
                sessionToCheck.setExpireDate(expireDate);

                session.update(sessionToCheck);

                checkSessionResult.setSuccess(true);
                checkSessionResult
                        .setMessage(AuthenticationManagerDB.resourceManger.getMessage("checkSession.success"));
            } else {
                // The session has time out. Deleting the session from Data Base
                session.delete(sessionToCheck);

                checkSessionResult.setResultCode(BasicResult.SESSION_TIMEOUT);
                logger.trace("Session expired. Please login again");
            }

        }

        transaction.commit();
    } catch (Exception e) {
        if (transaction != null && transaction.isActive()) {
            transaction.rollback();
        }

        logger.trace("Unexpected error while checking the user session", e);
    }

    return checkSessionResult;
}

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

License:Open Source License

@Override
public DataResult<ArrayList<HyperVisor>> editPhysicalMachine(final UserSession userSession,
        final PhysicalMachineCreation physicalMachineCreation) throws InfrastructureCommandException {

    DataResult<ArrayList<HyperVisor>> dataResult = new DataResult<ArrayList<HyperVisor>>();

    Session session = null;
    Transaction transaction = null;//from w  w  w.ja  va2 s.  co m
    try {
        PhysicalMachine pm = physicalMachineCreation.getPhysicalMachine();
        checkPhysicalMachineData(pm);

        session = HibernateUtil.getSession();
        transaction = session.beginTransaction();

        PhysicalmachineHB physicalMachineHb = (PhysicalmachineHB) session.get(PhysicalmachineHB.class,
                pm.getId());

        PhysicalMachine physicalMachineAux = physicalMachineHb.toPojo();
        if (pm.getAssignedTo() instanceof UcsRack
                && !pm.getName().equalsIgnoreCase(physicalMachineHb.getName())) {
            dataResult.setSuccess(false);
            dataResult.setMessage("The Machine is managed and its name cannot change");
            // Log the event
            traceLog(SeverityType.CRITICAL, ComponentType.MACHINE, EventType.MACHINE_MODIFY, userSession,
                    physicalMachineCreation.getPhysicalMachine().getDataCenter(), null,
                    "The Machine is managed and its name cannot change", null,
                    (Rack) physicalMachineCreation.getPhysicalMachine().getAssignedTo(),
                    physicalMachineHb.toPojo(), null, null);
            return dataResult;
        }

        if (pm.getAssignedTo() instanceof UcsRack) {
            dataResult.setSuccess(false);
            dataResult.setMessage("The Machine is managed and its name cannot change");
            // Log the event
            traceLog(SeverityType.CRITICAL, ComponentType.MACHINE, EventType.MACHINE_MODIFY, userSession,
                    physicalMachineCreation.getPhysicalMachine().getDataCenter(), null,
                    "The Machine is managed and its name cannot change", null,
                    (Rack) physicalMachineCreation.getPhysicalMachine().getAssignedTo(),
                    physicalMachineHb.toPojo(), null, null);
            return dataResult;
        }
        final String ipService = pm.getHypervisor().getIpService();

        // Updating the other attributes
        physicalMachineHb.setName(pm.getName());
        physicalMachineHb.setDescription(pm.getDescription());
        physicalMachineHb.setCpu(pm.getCpu());
        physicalMachineHb.setRam(pm.getRam());
        physicalMachineHb.setIdState(pm.getIdState());
        physicalMachineHb.getHypervisor().setIpService(ipService);
        physicalMachineHb.setVswitchName(pm.getVswitchName());
        physicalMachineHb.setIpmiIp(pm.getIpmiIp());
        physicalMachineHb.setIpmiPort(pm.getIpmiPort());
        physicalMachineHb.setIpmiUser(pm.getIpmiUser());
        physicalMachineHb.setIpmiPassword(pm.getIpmiPassword());

        // Updating virtual machines
        PhysicalMachineDAO pmDAO = factory.getPhysicalMachineDAO();
        List<VirtualmachineHB> vmList = pmDAO
                .getDeployedVirtualMachines(physicalMachineHb.getIdPhysicalMachine());

        if (vmList != null && !vmList.isEmpty()) {
            for (VirtualmachineHB vm : vmList) {
                if (StringUtils.hasText(vm.getVdrpIp())) {
                    vm.setVdrpIp(ipService);
                }
            }
        }

        session.update(physicalMachineHb);

        dataResult.setSuccess(true);
        dataResult.setMessage("Physical Machine edited successfully");
        dataResult.setData(physicalMachineCreation.getHypervisors());

        transaction.commit();

        traceLog(SeverityType.INFO, ComponentType.MACHINE, EventType.MACHINE_MODIFY, userSession,
                physicalMachineAux.getDataCenter(), null,
                "Physical machine '" + physicalMachineAux.getName() + "' has been modified [Name: "
                        + physicalMachineHb.getName() + ", " + +physicalMachineHb.getCpu() + "CPUs, "
                        + physicalMachineHb.getRam() + " RAM, "
                        + physicalMachineHb.getHypervisor().getType().getValue() + " hypervisor]",
                null, (Rack) physicalMachineAux.getAssignedTo(), physicalMachineAux, null, null);
    } catch (HibernateException e) {
        if (transaction != null && transaction.isActive()) {
            transaction.rollback();
        }

        errorManager.reportError(resourceManager, dataResult, "editPhysicalMachine", e);

        PhysicalmachineHB physicalMachineHb = (PhysicalmachineHB) session.get(PhysicalmachineHB.class,
                physicalMachineCreation.getPhysicalMachine().getId());

        // Log the event
        traceLog(SeverityType.CRITICAL, ComponentType.MACHINE, EventType.MACHINE_MODIFY, userSession,
                physicalMachineCreation.getPhysicalMachine().getDataCenter(), null, e.getMessage(), null,
                (Rack) physicalMachineCreation.getPhysicalMachine().getAssignedTo(), physicalMachineHb.toPojo(),
                null, null);

    }

    return dataResult;
}

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

License:Open Source License

@Override
public BasicResult editHypervisor(final UserSession userSession, final HyperVisor hypervisor) {

    BasicResult basicResult;/*w  ww.j av a 2 s  .co  m*/
    basicResult = new BasicResult();

    Session session = null;
    Transaction transaction = null;

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

        HypervisorHB hypervisorHB = (HypervisorHB) session.get(HypervisorHB.class, hypervisor.getId());
        PhysicalmachineHB physicalMachineHB = (PhysicalmachineHB) session.get(PhysicalmachineHB.class,
                hypervisor.getAssignedTo().getId());

        // Updating the Hypervisor
        hypervisorHB.setType(HypervisorType.fromValue(hypervisor.getType().getName()));
        hypervisorHB.setIp(hypervisor.getIp());
        hypervisorHB.setIpService(hypervisor.getIpService());
        hypervisorHB.setPort(hypervisor.getPort());
        hypervisorHB.setPhysicalMachine(physicalMachineHB);

        session.update(hypervisorHB);

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

        errorManager.reportError(resourceManager, basicResult, "editHypervisor", e);
    }

    basicResult.setMessage(InfrastructureCommandImpl.resourceManager.getMessage("editHypervisor.success"));
    basicResult.setSuccess(true);

    return basicResult;
}