Example usage for org.hibernate Session saveOrUpdate

List of usage examples for org.hibernate Session saveOrUpdate

Introduction

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

Prototype

void saveOrUpdate(Object object);

Source Link

Document

Either #save(Object) or #update(Object) the given instance, depending upon resolution of the unsaved-value checks (see the manual for discussion of unsaved-value checking).

Usage

From source file:com.matel.pg.dao.impl.CustomerDAOImpl.java

@Override
public void saveUsers(Set<UserProfile> customersToSave) {
    for (UserProfile profile : customersToSave) {
        System.out.println(".............. QBOID: " + profile.getQboUserProfile().getQboId());
        try {/*from  www  .j a va2 s.com*/
            Session session = sessionFactory.getCurrentSession();
            session.saveOrUpdate(profile.getQboUserProfile());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

From source file:com.matel.pg.dao.impl.ProductDAOImpl.java

@Override
public void save(Set<Product> productsToSave) {
    for (Product product : productsToSave) {
        System.out.println(".............. QBOID: " + product.getQboProduct().getQboId());
        try {/*w ww .j  a  v a2s.c  o  m*/
            Session session = sessionFactory.getCurrentSession();
            session.saveOrUpdate(product.getQboProduct());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

From source file:com.maydesk.base.util.PDBinding.java

License:Mozilla Public License

public boolean applyChanges() {
    // validate values
    boolean validationOK = true;
    for (IChangeSupportable component : changeData.keySet()) {
        component.setBorder(PDBorderFactory.getBorder());
        if (component instanceof TextComponent) {
            ((TextComponent) component).setToolTipText(""); // FIXME
        }//w w  w  . j  ava 2s.  c  om

        MyChangeData cd = changeData.get(component);
        if (cd == null)
            continue;

        // validation
        try {
            if (!(cd.soplet instanceof Enum))
                continue;
            Annotation sopletAnnotation = cd.soplet.getClass().getField(((Enum) cd.soplet).name())
                    .getAnnotations()[0];
            if (sopletAnnotation == null)
                continue;
            Method m = sopletAnnotation.getClass().getMethod("validator");
            if (m == null)
                continue;
            Enum<?> validator = (Enum<?>) m.invoke(sopletAnnotation);
            if (validator == null)
                continue;
            Method validate = validator.getClass().getMethod("validate", Object.class);
            validate.setAccessible(true);
            Object value = component.getValue();
            Boolean validationResult = (Boolean) validate.invoke(validator, value);
            if (Boolean.TRUE.equals(validationResult))
                continue;

            // validation error found
            component.setBorder(PDBorderFactory.getBorderError());
            if (component instanceof TextComponent) {
                Field field = validator.getDeclaringClass().getField(validator.name());
                Annotation ann = field.getAnnotations()[0];
                Method m2 = ann.getClass().getMethod("textEN");
                if (m2 != null) {
                    String errorMessage = (String) m2.invoke(ann);
                    ((TextComponent) component).setToolTipText(errorMessage);
                }
            }
            validationOK = false;
        } catch (NoSuchMethodException ex) {
            // no validator
        } catch (InvocationTargetException ex) {
            // no validator
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    if (!validationOK) {
        return false;
    }

    if (hasChanges()) {
        if (target instanceof MBaseWithTitle) {
            ((MBaseWithTitle) target).updateCachedValues();
        }
        Session session = PDHibernateFactory.getSession();
        if (target.getId() == 0) {
            session.save(target);
            if (dataLink != null) {
                session.flush();
                session.refresh(target);
                dataLink.setTargetId(target.getId());
                session.saveOrUpdate(dataLink);
            }
        } else {
            session.update(target);
        }
    }
    for (PDBinding childBinding : children) {
        childBinding.applyChanges();
    }
    resetChanges();
    if (saveDoneListener != null) {
        ActionEvent e = new ActionEvent(target, null);
        saveDoneListener.actionPerformed(e);
    }
    return true;
}

From source file:com.mercatis.lighthouse3.persistence.environment.hibernate.DeploymentRegistryImplementation.java

License:Apache License

@SuppressWarnings({ "unchecked", "rawtypes" })
@Override//  w  ww. j  a  v  a  2 s .  co m
public void delete(Deployment deployment) {
    Deployment deploymentToDelete = find(deployment.getId());
    if (deploymentToDelete == null) {
        throw new PersistenceException("Entity not persistent", null);
    }
    Session session = unitOfWork.getCurrentSession();
    if (!session.contains(deploymentToDelete))
        deploymentToDelete = (Deployment) session.merge(deploymentToDelete);

    List<DeploymentCarryingDomainModelEntity> attachedTo = session
            .createCriteria(DeploymentCarryingDomainModelEntity.class).createCriteria("attachedDeployments")
            .add(Restrictions.eq("id", deploymentToDelete.getId())).setCacheable(true).list();

    for (DeploymentCarryingDomainModelEntity entity : attachedTo) {
        entity.detachDeployment(deploymentToDelete);
        session.saveOrUpdate(entity);
    }

    try {
        unitOfWork.flush();
        session.createSQLQuery(
                "delete from STATUS_METADATA where MD_STATUS_ID in (select distinct STA_ID from STATUS where STA_CONTEXT_ID = :deployment_id)")
                .setParameter("deployment_id", deploymentToDelete.getId()).executeUpdate();
        session.createSQLQuery(
                "delete from STATUS_NOTIFICATION_CHANNELS where SNC_STATUS_ID in (select distinct STA_ID from STATUS where STA_CONTEXT_ID = :deployment_id)")
                .setParameter("deployment_id", deploymentToDelete.getId()).executeUpdate();
        session.createSQLQuery(
                "update STATUS set LATEST_STATUS_CHANGE = null where STA_CONTEXT_ID = :deployment_id")
                .setParameter("deployment_id", deploymentToDelete.getId()).executeUpdate();
        session.createSQLQuery(
                "update STATUS_CHANGES set NEXT_STATUS_CHANGE = null where SCH_STATUS_ID in (select distinct STA_ID from STATUS where STA_CONTEXT_ID = :deployment_id)")
                .setParameter("deployment_id", deploymentToDelete.getId()).executeUpdate();
        session.createSQLQuery(
                "update STATUS_CHANGES set PREVIOUS_STATUS_CHANGE = null where SCH_STATUS_ID in (select distinct STA_ID from STATUS where STA_CONTEXT_ID = :deployment_id)")
                .setParameter("deployment_id", deploymentToDelete.getId()).executeUpdate();
        session.createSQLQuery(
                "delete from STATUS_CHANGES where SCH_STATUS_ID in (select distinct STA_ID from STATUS where STA_CONTEXT_ID = :deployment_id)")
                .setParameter("deployment_id", deploymentToDelete.getId()).executeUpdate();
        session.createSQLQuery("delete from STATUS where STA_CONTEXT_ID = :deployment_id")
                .setParameter("deployment_id", deploymentToDelete.getId()).executeUpdate();
    } catch (Exception ex) {
        if (logger.isWarnEnabled()) {
            logger.warn(
                    "Failed to cleanup status tables for deployment: "
                            + deployment.getDeployedComponent().getCode() + " @ " + deployment.getLocation(),
                    ex);
        }
    }

    try {
        unitOfWork.flush();
        session.createSQLQuery(
                "delete pie from PROCESS_INSTANCES_EVENTS pie join EVENTS e on pie.EVT_ID = e.EVT_ID where e.EVT_CONTEXT_ID = :deployment_id")
                .setParameter("deployment_id", deploymentToDelete.getId()).executeUpdate();
    } catch (Exception ex) {
        if (logger.isWarnEnabled()) {
            logger.warn(
                    "Failed to cleanup process instance tables for deployment: "
                            + deployment.getDeployedComponent().getCode() + " @ " + deployment.getLocation(),
                    ex);
        }
    }

    try {
        unitOfWork.flush();

        session.createSQLQuery(
                "delete from EVENT_TAGS where EVT_ID in (select distinct EVT_ID from EVENTS where EVT_CONTEXT_ID = "
                        + deploymentToDelete.getId() + ")")
                .executeUpdate();
        session.createSQLQuery(
                "delete from EVENT_TRANSACTION_IDS where EVT_ID in (select distinct EVT_ID from EVENTS where EVT_CONTEXT_ID = "
                        + deploymentToDelete.getId() + ")")
                .executeUpdate();

        session.createSQLQuery(
                "delete from EVENT_UDFS where EVT_ID in (select distinct EVT_ID from EVENTS where EVT_CONTEXT_ID = "
                        + deploymentToDelete.getId() + ")")
                .executeUpdate();
        session.createSQLQuery("delete from EVENTS where EVT_CONTEXT_ID = :deployment_id")
                .setParameter("deployment_id", deploymentToDelete.getId()).executeUpdate();
    } catch (Exception ex) {
        if (logger.isWarnEnabled()) {
            logger.warn(
                    "Failed to cleanup event tables for deployment: "
                            + deployment.getDeployedComponent().getCode() + " @ " + deployment.getLocation(),
                    ex);
        }
    }

    try {
        unitOfWork.flush();

        session.createSQLQuery(
                "delete from JOB_PARAMETER_VALUES where JPV_JOB_ID in (select distinct JOB_ID from JOBS where JOB_CALL_TARGET_ID in (select distinct OPI_ID from OPERATION_INSTALLATIONS where OPI_INSTALLATION_LOCATION_ID = :deployment_id))")
                .setParameter("deployment_id", deploymentToDelete.getId()).executeUpdate();

        session.createSQLQuery(
                "delete from JOBS where JOB_CALL_TARGET_ID in (select distinct OPI_ID from OPERATION_INSTALLATIONS where OPI_INSTALLATION_LOCATION_ID = :deployment_id)")
                .setParameter("deployment_id", deploymentToDelete.getId()).executeUpdate();
    } catch (Exception ex) {
        if (logger.isWarnEnabled()) {
            logger.warn(
                    "Failed to cleanup job tables for deployment: "
                            + deployment.getDeployedComponent().getCode() + " @ " + deployment.getLocation(),
                    ex);
        }
    }

    try {
        unitOfWork.flush();

        session.createSQLQuery(
                "delete from OPERATION_INSTALLATIONS where OPI_INSTALLATION_LOCATION_ID = :deployment_id")
                .setParameter("deployment_id", deploymentToDelete.getId()).executeUpdate();
    } catch (Exception ex) {
        if (logger.isWarnEnabled()) {
            logger.warn(
                    "Failed to cleanup operation installation tables for deployment: "
                            + deployment.getDeployedComponent().getCode() + " @ " + deployment.getLocation(),
                    ex);
        }
    }

    super.delete(deploymentToDelete);
}

From source file:com.mimp.hibernate.HiberFamilia.java

public void crearAFR(AsistenciaFR temp) {
    Session session = sessionFactory.getCurrentSession();
    session.beginTransaction();
    session.saveOrUpdate(temp);
}

From source file:com.mimp.hibernate.HiberMain.java

public void crearUpdateExpFam(ExpedienteFamilia temp) {
    Session session = sessionFactory.getCurrentSession();
    session.beginTransaction();/*from  w ww. j  a va 2s.  c  o m*/
    session.saveOrUpdate(temp);

}

From source file:com.mimp.hibernate.HiberMain.java

public void crearAdopcionAdoptantesExtranjero(Nna tempNna, Designacion tempDesig) {
    Session session = sessionFactory.getCurrentSession();
    session.beginTransaction();/*from  w  ww.j av a 2s  .  com*/
    session.saveOrUpdate(tempNna);
    session.saveOrUpdate(tempDesig);
}

From source file:com.mimp.hibernate.HiberMain.java

public void crearRevision(Revision temp) {
    Session session = sessionFactory.getCurrentSession();
    session.beginTransaction();
    session.saveOrUpdate(temp);

}

From source file:com.mimp.hibernate.HiberPersonal.java

public void crearActualizarAdoptante(Adoptante temp) {

    Session session = sessionFactory.getCurrentSession();
    session.beginTransaction();/*from   w  ww . j a v  a2  s  .co m*/
    session.saveOrUpdate(temp);
}

From source file:com.model.manegers.Agent_maneger.java

public static void updateAgent(Agents pro) {
    Transaction trns = null;//www.  j  ava  2s . c o  m
    Session session = HibernateUtil.getSessionFactory().openSession();
    try {
        trns = session.beginTransaction();
        //Abeer
        Agents soso = new Agents();
        soso.setId(pro.getId());
        soso.setFirstname(pro.getFirstname());
        soso.setLastname(pro.getLastname());
        soso.setAddress(pro.getAddress());
        soso.setTelephone(pro.getTelephone());
        soso.setEmail(pro.getEmail());
        soso.setImportAmmount(pro.getImportAmmount());
        soso.setImportType(pro.getImportType());
        soso.setDiscount(pro.getDiscount());

        //
        session.saveOrUpdate(soso);
        session.getTransaction().commit();
    } catch (RuntimeException e) {
        if (trns != null) {
            trns.rollback();
        }
        e.printStackTrace();
    } finally {
        session.flush();
        session.close();
    }
}