Example usage for org.hibernate Session merge

List of usage examples for org.hibernate Session merge

Introduction

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

Prototype

Object merge(Object object);

Source Link

Document

Copy the state of the given object onto the persistent object with the same identifier.

Usage

From source file:com.library.Database.DataHelper.java

public void update(ArrayList<Book> books) {
    Session session = sessionFactory.openSession();
    Transaction transaction = session.beginTransaction();
    for (Book b : books) {
        //session.update(b);
        if (b.isIsBookChecked()) {
            Book book = (Book) (session.createCriteria(Book.class).add(Restrictions.eq("id", b.getId()))
                    .uniqueResult());//w  w  w .j  a  v  a2s.  c  o m
            b.setContent(book.getContent());
            b.setImage(book.getImage());
            session.merge(b);
        }

    }
    transaction.commit();
    session.flush();
    session.close();
}

From source file:com.mac.green_leaves.v1.master.MasterRepository.java

public int save(Model model) {
    Session session = getSession();
    return (Integer) session.merge(model);
}

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

@Override
public void save(Company company) {
    try {//from  w  w w  .  ja  v a 2  s. co m
        Session session = sessionFactory.getCurrentSession();
        session.merge(company);

    } catch (HibernateException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.mclub.server.impl.ChangeStatusServiceImpl.java

License:GNU General Public License

/**
 * Bir film verildiginde Trans tablosuna hareket eklenir ve filmin durumu degistirilir
 *  // w w w . ja  v a2 s.  c  o m
 */
@Override
public RetVal afterSave(Session session, ChangeStatus serverModel, List<EffectedDetail> effectedDetailList) {

    for (ChangeStatusDetail detail : serverModel.getDetails()) {
        Trans trans = null;
        if (detail.getTransId() != null) {
            trans = (Trans) session.get(Trans.class, detail.getTransId());
        }

        if (trans == null)
            trans = new Trans();

        trans.setActionId(detail.getId());
        trans.setType(ActionType.ChangeStatus);
        trans.setTransDate(serverModel.getTransDate());
        trans.setMovie(detail.getMovie());
        trans.setStatus(detail.getStatus());
        trans.setUser(serverModel.getUser());
        session.save(trans);

        detail.setTransId(trans.getId());
        detail.setChangeStatus(serverModel);

        detail.getMovie().setLastTransId(trans.getId());
        detail.getMovie().setLastTransDate(serverModel.getTransDate());
        detail.getMovie().setLastStatus(trans.getStatus());
        session.merge(detail.getMovie());
    }

    //Varsa, silinen detaylardaki filmler eski haline getirilir
    List<IModel> deletedList = effectedDetailList.get(0).getDeletedList();
    for (int i = 0; i < deletedList.size(); i++) {
        ChangeStatusDetail detail = convertToServer(deletedList.get(i));
        DBWorks.undoMovieStatus(session, detail.getMovie(), detail.getTransId());
    }
    return null;
}

From source file:com.mclub.server.impl.GivenMovieServiceImpl.java

License:GNU General Public License

/**
 * Bir film verildiginde Trans tablosuna hareket eklenir ve filmin durumu degistirilir 
 *///from w w  w  . j a v  a 2s .c  o  m
@Override
public RetVal afterSave(Session session, GivenMovie serverModel, List<EffectedDetail> effectedDetailList) {

    for (GivenMovieDetail detail : serverModel.getDetails()) {
        Trans trans = null;
        if (detail.getTransId() != null) {
            trans = (Trans) session.get(Trans.class, detail.getTransId());
        }

        if (trans == null)
            trans = new Trans();

        trans.setActionId(detail.getId());
        trans.setType(ActionType.GivenMovie);
        trans.setCustomer(serverModel.getCustomer());
        trans.setTransDate(serverModel.getTransDate());
        trans.setMovie(detail.getMovie());
        trans.setStatus(detail.getStatus());
        trans.setUser(serverModel.getUser());
        session.save(trans);

        detail.setTransId(trans.getId());
        detail.setGivenMovie(serverModel);

        detail.getMovie().setLastTransId(trans.getId());
        detail.getMovie().setLastTransDate(serverModel.getTransDate());
        detail.getMovie().setLastCustomer(serverModel.getCustomer());
        detail.getMovie().setLastStatus(trans.getStatus());
        session.merge(detail.getMovie());
    }

    //Varsa, silinen detaylardaki filmler eski haline getirilir
    List<IModel> deletedList = effectedDetailList.get(0).getDeletedList();
    for (int i = 0; i < deletedList.size(); i++) {
        GivenMovieDetail detail = convertToServer(deletedList.get(i));
        DBWorks.undoMovieStatus(session, detail.getMovie(), detail.getTransId());
    }
    return null;
}

From source file:com.mclub.server.impl.IncomingMovieServiceImpl.java

License:GNU General Public License

/**
 * Bir film verildiginde Trans tablosuna hareket eklenir ve filmin durumu degistirilir 
 * //ww  w.ja  va2s . c  o m
 */
@Override
public RetVal afterSave(Session session, IncomingMovie serverModel, List<EffectedDetail> effectedDetailList) {

    for (IncomingMovieDetail detail : serverModel.getDetails()) {
        Trans trans = null;
        if (detail.getTransId() != null) {
            trans = (Trans) session.get(Trans.class, detail.getTransId());
        }

        if (trans == null) {
            trans = new Trans();
        } else {
            if (!trans.getMovie().equals(detail.getMovie())) {
                DBWorks.undoMovieStatus(session, trans.getMovie(), trans.getId());
            }
        }

        trans.setActionId(detail.getId());
        trans.setType(ActionType.IncomingMovie);
        trans.setCustomer(serverModel.getCustomer());
        trans.setTransDate(serverModel.getTransDate());
        trans.setMovie(detail.getMovie());
        trans.setStatus(FilmStatus.IN_STORE);
        trans.setUser(serverModel.getUser());
        session.save(trans);

        detail.setTransId(trans.getId());
        detail.setIncomingMovie(serverModel);

        detail.getMovie().setLastTransId(trans.getId());
        detail.getMovie().setLastTransDate(serverModel.getTransDate());
        detail.getMovie().setLastCustomer(serverModel.getCustomer());
        detail.getMovie().setLastStatus(trans.getStatus());
        session.merge(detail.getMovie());
    }

    //Varsa, silinen detaylardaki filmler eski haline getirilir
    List<IModel> deletedList = effectedDetailList.get(0).getDeletedList();
    for (int i = 0; i < deletedList.size(); i++) {
        IncomingMovieDetail detail = convertToServer(deletedList.get(i));
        DBWorks.undoMovieStatus(session, detail.getMovie(), detail.getTransId());
    }
    return null;
}

From source file:com.mclub.server.util.DBWorks.java

License:GNU General Public License

/**
 * Durumu degisen filmin, hareketlerini bir geri alir
 * /*w w  w .j  ava2 s .  c om*/
 * @param session
 * @param movie
 * @param unwantedTransId istenmeyen yani silinecek olan trans id
 * 
 */
@SuppressWarnings("unchecked")
public static void undoMovieStatus(Session session, Movie movie, Long unwantedTransId) {
    List<Trans> transList = session
            .createQuery("from Trans t " + "where t.movie = :movie " + "  and t.id != :unwantedTransId "
                    + "order by t.actionId desc")
            .setParameter("movie", movie).setParameter("unwantedTransId", unwantedTransId).setFetchSize(1)
            .list();

    Trans prevTrans = (transList.size() > 0 ? transList.get(0) : null);
    if (prevTrans != null) {
        movie.setLastTransId(prevTrans.getId());
        movie.setLastTransDate(prevTrans.getTransDate());
        movie.setLastCustomer(prevTrans.getCustomer());
        movie.setLastStatus(prevTrans.getStatus());
    } else {
        movie.setLastTransId(null);
        movie.setLastTransDate(null);
        movie.setLastCustomer(null);
        movie.setLastStatus(FilmStatus.IN_STORE);
    }
    session.merge(movie);

    Trans trans = (Trans) session.get(Trans.class, unwantedTransId);
    if (trans != null) {
        session.delete(trans);
    }
}

From source file:com.mercatis.lighthouse3.persistence.commons.hibernate.DomainModelEntityDAOImplementation.java

License:Apache License

@SuppressWarnings("unchecked")
public void delete(Entity entityToDelete) {
    if (!alreadyPersisted(entityToDelete))
        throw new PersistenceException("Entity not persistent", null);
    try {/*from  w ww .jav a  2  s .  c o m*/
        Session sess = unitOfWork.getCurrentSession();
        if (!sess.contains(entityToDelete))
            entityToDelete = (Entity) sess.merge(entityToDelete);
        sess.delete(entityToDelete);
    } catch (Exception cause) {
        throw new PersistenceException("Deletion of entity failed", cause);
    }
}

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

License:Apache License

@SuppressWarnings({ "unchecked", "rawtypes" })
@Override/*from w  w  w.java 2s  . c o  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.mercatis.lighthouse3.persistence.users.hibernate.GroupRegistryImplementation.java

License:Apache License

@Override
public void persist(Group entityToPersist) {
    Session session = unitOfWork.getCurrentSession();
    Set<UserCodeRegistration> regs = entityToPersist.getUserCodes();
    for (UserCodeRegistration userCode : regs.toArray(new UserCodeRegistration[0])) {
        if (existsUserCode(userCode.getUserCode())) {
            regs.remove(userCode);/*from   w w  w .j  a  v a2  s  .  c  om*/
            userCode = (UserCodeRegistration) session.merge(userCode);
            regs.add(userCode);
        }
    }
    super.persist(entityToPersist);
}