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.myapp.core.base.dao.impl.AbstractBaseDao.java

private void updateTreeData(Session sesion, CoreBaseTreeInfo treeInfo) {
    if (treeInfo == null)
        return;//from w  w  w.j av a 2  s  .c  o  m
    String longNumber = treeInfo.getNumber();
    String displayName = treeInfo.getName();
    if (treeInfo.getParent() != null) {
        CoreBaseTreeInfo ptreeInfo = (CoreBaseTreeInfo) treeInfo.getParent();
        longNumber = ptreeInfo.getLongNumber();
        if (!BaseUtil.isEmpty(longNumber)) {
            longNumber = longNumber + "!" + treeInfo.getNumber();
        } else {
            longNumber = treeInfo.getNumber();
        }
        displayName = ptreeInfo.getDisplayName();
        if (!BaseUtil.isEmpty(displayName)) {
            displayName = displayName + "_" + treeInfo.getName();
        } else {
            displayName = treeInfo.getName();
        }
    }

    treeInfo.setLongNumber(longNumber);
    treeInfo.setDisplayName(displayName);
    sesion.merge(treeInfo);

    Set<CoreBaseTreeInfo> children = treeInfo.getChildren();
    if (children != null && children.size() > 0) {
        for (CoreBaseTreeInfo tInfo : children) {
            updateTreeData(sesion, tInfo);
        }
    }
}

From source file:com.myapp.core.base.dao.impl.AbstractBaseDao.java

public void deleteEntity(Object entity) throws DeleteException {
    if (entity != null) {
        if (entity instanceof CoreBaseTreeInfo) {
            CoreBaseTreeInfo treeInfo = (CoreBaseTreeInfo) entity;
            Set treeChildren = treeInfo.getChildren();
            if (treeChildren != null && treeChildren.size() > 0) {
                throw new DeleteException("??!");
            }/*w w w  . ja v a2s . c  o  m*/
        }
        if (entity instanceof CoreBaseDataInfo) {
            CoreBaseDataInfo cbInfo = (CoreBaseDataInfo) entity;
            if (cbInfo.getEnabled() != null && cbInfo.getEnabled()) {
                throw new DeleteException("?????!");
            }
        } else if (entity instanceof CoreBaseBillInfo) {
            CoreBaseBillInfo cbInfo = (CoreBaseBillInfo) entity;
            BillState bs = cbInfo.getBillState();
            if (bs != null && bs.equals(BillState.AUDIT)) {
                throw new DeleteException("????!");
            }
        }
        Session session = getCurrentSession();
        if (entity instanceof CoreBaseTreeInfo) {
            CoreBaseTreeInfo treeInfo = (CoreBaseTreeInfo) entity;
            CoreBaseTreeInfo ptreeInfo = (CoreBaseTreeInfo) treeInfo.getParent();
            if (ptreeInfo != null) {
                Class clas = ptreeInfo.getClass();
                String hql = "select id from " + clas.getSimpleName() + " where id !=? and parent.id=?";
                Query query = initHqlParams(session.createQuery(hql),
                        new String[] { treeInfo.getId(), ptreeInfo.getId() });
                if (query != null) {
                    List datas = query.list();
                    if (datas != null && datas.size() > 0) {
                        ptreeInfo.setIsLeaf(true);
                        session.merge(ptreeInfo);
                    }
                }
            }
        }
        session.delete(entity);
        session.flush();
    }
}

From source file:com.myapp.core.base.dao.impl.AbstractBaseDao.java

public Serializable addNewEntity(Object entity) throws SaveException {
    if (entity != null) {
        checkLicense(entity);/*from   www.  j ava2  s  .c  o m*/
        Session session = getCurrentSession();
        if (!(entity instanceof SubsystemTreeInfo)) {
            Class claz = entity.getClass();
            String entityClaz = claz.getName();
            String hql = "select id from SubsystemTreeInfo where entityClaz=?";
            List datas = findByHQL(hql, new String[] { entityClaz });
            if (datas == null || datas.size() <= 0) {
                SubsystemTreeInfo subTree = new SubsystemTreeInfo();
                AbstractEntityPersister cmd = (AbstractEntityPersister) session.getSessionFactory()
                        .getClassMetadata(claz);
                subTree.setEntityClaz(entityClaz);
                String tableName = cmd.getTableName();
                subTree.setEntityTable(tableName);
                long seq = UuidUtils.getStringLong(entityClaz);
                subTree.setEntityObjectType(UuidUtils.getEntityType(entityClaz));
                subTree.setEntitySeq(new Date().getTime());
                subTree.setSeq(seq);
                subTree.setEntityType(getEntityType(entity));
                String entityName = claz.getSimpleName();
                MyEntityAnn myA = (MyEntityAnn) claz.getAnnotation(MyEntityAnn.class);
                if (myA != null) {
                    entityName = myA.name();
                }
                subTree.setEntityName(entityName);
                session.save(subTree);
            }
        }
        if (entity instanceof CoreBaseTreeInfo) {
            CoreBaseTreeInfo treeInfo = (CoreBaseTreeInfo) entity;
            Integer level = 1;
            String longNumber = treeInfo.getNumber();
            String displayName = treeInfo.getName();
            Boolean isLeaf = Boolean.TRUE;
            if (treeInfo.getParent() != null) {
                CoreBaseTreeInfo ptreeInfo = (CoreBaseTreeInfo) treeInfo.getParent();
                longNumber = ptreeInfo.getLongNumber();
                if (!BaseUtil.isEmpty(longNumber)) {
                    longNumber = longNumber + "!" + treeInfo.getNumber();
                } else {
                    longNumber = treeInfo.getNumber();
                }
                displayName = ptreeInfo.getDisplayName();
                if (!BaseUtil.isEmpty(displayName)) {
                    displayName = displayName + "_" + treeInfo.getName();
                } else {
                    displayName = treeInfo.getName();
                }
                level = ptreeInfo.getLevel();
                if (level != null) {
                    level = level + 1;
                } else {
                    level = 1;
                }
            }
            treeInfo.setIsLeaf(isLeaf);
            treeInfo.setLevel(level);
            treeInfo.setLongNumber(longNumber);
            treeInfo.setDisplayName(displayName);
        }
        if (entity instanceof CoreBaseInfo) {
            CoreBaseInfo cbinfo = (CoreBaseInfo) entity;
            Date curDate = new Date();
            cbinfo.setCreateDate(curDate);
            cbinfo.setLastUpdateDate(curDate);
        }
        Serializable pk = session.save(entity);
        session.merge(entity);
        if (entity instanceof CoreBaseTreeInfo) {
            CoreBaseTreeInfo treeInfo = (CoreBaseTreeInfo) entity;
            Object pObj = treeInfo.getParent();
            if (pObj != null && pObj instanceof CoreBaseTreeInfo) {
                CoreBaseTreeInfo pTreeInfo = (CoreBaseTreeInfo) pObj;
                pTreeInfo.setIsLeaf(false);
                session.merge(pTreeInfo);
            }
        }
        session.flush();
        return pk;
    }
    return null;
}

From source file:com.mycompany.thymeleafspringapp.dao.DealsDaoImpl.java

protected void persistDeal(Deals deal) throws HibernateException {
    Transaction tx = null;/*www .  ja  v a  2  s  .  co  m*/
    Session session = null;
    try {
        session = sessionFactory.openSession();
        tx = session.beginTransaction();
        tx.setTimeout(5);
        session.merge(deal);
        tx.commit();

    } catch (HibernateException e) {
        try {
            tx.rollback();
        } catch (RuntimeException rbe) {
            log.error("Couldnt roll back transaction", rbe);
        }
        throw e;
    } finally {
        if (session != null) {
            session.close();
        }
    }
}

From source file:com.nec.crud.CrudRepositoryImpl.java

License:Open Source License

/** {@inheritDoc} */
@Override
public T merge(final Session session, T entity) {
    return (T) session.merge(entity);
}

From source file:com.npower.dm.hibernate.management.ProvisionJobManagementBeanImpl.java

License:Open Source License

/**
 * @param jobID/*w  w  w  .ja v a2  s . c  om*/
 * @param newState
 * @throws DMException
 */
private void updateJobState(long jobID, String newState) throws DMException {
    try {
        ProvisionJob job = this.loadJobByID(jobID);
        job.setState(newState);
        Session session = this.getHibernateSession();
        session.saveOrUpdate(job);

        Criteria mainCriteria = session.createCriteria(DeviceProvisionRequestEntity.class);
        Criteria subCriteria = mainCriteria.createCriteria("provisionElement");
        subCriteria.add(Expression.eq("provisionRequest", job));
        List<DeviceProvisionRequestEntity> deviceStatusList = subCriteria.list();
        for (int i = 0; i < deviceStatusList.size(); i++) {
            DeviceProvisionRequestEntity deviceStatus = (DeviceProvisionRequestEntity) deviceStatusList.get(i);
            String state = deviceStatus.getState();
            // Where the device's status is not done, broadcast the provisionjob's status to device' status.
            if (!state.equalsIgnoreCase(ProvisionJobStatus.DEVICE_JOB_STATE_DONE)) {
                if (newState.equalsIgnoreCase(ProvisionJob.JOB_STATE_CANCELLED)) {
                    deviceStatus.setState(ProvisionJobStatus.DEVICE_JOB_STATE_CANCELLED);
                } else if (newState.equalsIgnoreCase(ProvisionJob.JOB_STATE_DISABLE)) {
                    deviceStatus.setState(ProvisionJobStatus.DEVICE_JOB_STATE_CANCELLED);
                } else if (newState.equalsIgnoreCase(ProvisionJob.JOB_STATE_APPLIED)) {
                    deviceStatus.setState(ProvisionJobStatus.DEVICE_JOB_STATE_READY);
                }
            }
            //session.saveOrUpdate(deviceStatus);
            session.merge(deviceStatus);
        }
    } catch (HibernateException e) {
        throw new DMException(e);
    }
}

From source file:com.npower.dm.hibernate.management.ProvisionJobManagementBeanImpl.java

License:Open Source License

public void update(ProvisionJobStatus jobStatus) throws DMException {
    try {//from w  w  w .j  av a  2s  .c o  m
        Session session = this.getHibernateSession();
        jobStatus.setLastUpdatedTime(new Date());
        //session.saveOrUpdate(jobStatus);
        session.merge(jobStatus);

        // , 
        if (jobStatus.isFinished()) {
            ProvisionJob job = jobStatus.getProvisionElement().getProvisionRequest();
            for (ProvisionJobStatus status : job.getAllOfProvisionJobStatus()) {
                if (!status.isFinished()) {
                    return;
                }
            }
            job.setState(ProvisionJob.JOB_STATE_FINISHED);
            session.merge(job);
            //this.updateJobState(job.getID(), ProvisionJob.JOB_STATE_FINISHED);
        }

    } catch (HibernateException e) {
        throw new DMException(e);
    }
}

From source file:com.npower.dm.hibernate.management.ProvisionJobManagementBeanImpl.java

License:Open Source License

public void copy(DeviceDMState dms, ProvisionJob job) throws DMException {
    // Load DeviceState from DM queued.
    String deviceExternalID = dms.deviceId;

    ManagementBeanFactory factory = null;
    try {//from   w  w  w .j a v  a2 s.c o  m
        factory = this.getManagementBeanFactory();
        ProvisionJobBean jobBean = factory.createProvisionJobBean();
        DeviceBean deviceBean = factory.createDeviceBean();
        Device device = deviceBean.getDeviceByExternalID(deviceExternalID);

        // Copy status from DeviceDMState
        ProvisionJobStatus status = jobBean.getProvisionJobStatus(job, device);
        if (dms.state == DeviceDMState.STATE_MANAGEABLE) {
            status.setState(ProvisionJobStatus.DEVICE_JOB_STATE_READY);
        } else if (dms.state == DeviceDMState.STATE_IN_PROGRESS) {
            status.setState(ProvisionJobStatus.DEVICE_JOB_STATE_DOING);
        } else if (dms.state == DeviceDMState.STATE_COMPLETED) {
            status.setState(ProvisionJobStatus.DEVICE_JOB_STATE_DONE);
        } else if (dms.state == DeviceDMState.STATE_ABORTED) {
            status.setState(ProvisionJobStatus.DEVICE_JOB_STATE_CANCELLED);
        } else if (dms.state == DeviceDMState.STATE_ERROR) {
            status.setState(ProvisionJobStatus.DEVICE_JOB_STATE_ERROR);
        } else if (dms.state == DeviceDMState.STATE_IN_PROGRESS) {
            status.setState(ProvisionJobStatus.DEVICE_JOB_STATE_WAITING_CLIENT_INITIALIZED_SESSION);
        }
        Session session = this.getHibernateSession();
        //session.saveOrUpdate(status);
        session.merge(status);
        session.saveOrUpdate(job);
    } catch (DMException ex) {
        throw new DMException("Error reading the device DM state " + dms.toString(), ex);
    } finally {
        if (factory != null) {
            //factory.release();
        }
    }

}

From source file:com.oracle.coherence.hibernate.cachestore.HibernateCacheStore.java

License:CDDL license

/**
 * Store a collection of Hibernate entities given a Map of ids (keys) and
 * entities (values)/*from   w w w .  ja v a2 s  .c o  m*/
 *
 * @param entries   a mapping of ids (keys) to entities (values)
 */
public void storeAll(Map entries) {
    ensureInitialized();

    Transaction tx = null;

    Session session = openSession();

    try {
        tx = session.beginTransaction();

        // We just iterate through the incoming set and individually
        // save each one. Note that this is still part of a single
        // Hibernate transaction so it may batch them.
        for (Iterator iter = entries.entrySet().iterator(); iter.hasNext();) {
            Map.Entry entry = (Map.Entry) iter.next();
            Serializable id = (Serializable) entry.getKey();
            Object entity = entry.getValue();
            validateIdentifier(id, entity, (SessionImplementor) session);
            session.merge(entity);
        }

        tx.commit();
    } catch (Exception e) {
        if (tx != null) {
            tx.rollback();
        }

        throw ensureRuntimeException(e);
    } finally {
        closeSession(session);
    }
}

From source file:com.pagos.Dao.DaoAbonos.java

@Override
public boolean actualizar(Session session, Abonos abonos) throws Exception {
    session.merge(abonos);
    return true;//  w w  w  .  j  a  va 2s  . co m
}