List of usage examples for org.hibernate Session saveOrUpdate
void saveOrUpdate(Object object);
From source file:com.creative.dao.repository.GenericBatchDaoImpl.java
License:Apache License
private <T> int executeBatch(BatchType batchType, List<T> list) { Session session = sessionFactory.getCurrentSession(); session.setCacheMode(CacheMode.IGNORE); session.setFlushMode(FlushMode.MANUAL); logger.info("Executing Batch of size :" + list.size() + " given batch size is:" + batchSize); for (int i = 0; i < list.size(); i++) { switch (batchType) { case BATCH_INSERT: session.save(list.get(i));/*from w ww. j a va 2s . co m*/ break; case BATCH_DELETE: session.delete(list.get(i)); break; case BATCH_INSERT_OR_UPDATE: session.saveOrUpdate(list.get(i)); default: // nothing; } if (i > 0 && i % batchSize == 0) { logger.info("Flushing and clearing the cache" + " after row number :" + i); session.flush(); session.clear(); } } session.flush(); return list.size(); }
From source file:com.dell.asm.asmcore.asmmanager.db.DeviceConfigureDAO.java
License:Open Source License
public boolean updateDeviceConfigureEntity(DeviceConfigureEntity deviceConfigureEntity, ConfigureStatus status) throws AsmManagerCheckedException { Session session = null; Transaction tx = null;/*ww w .java2 s . com*/ boolean updatedFlag = false; try { session = _dao._database.getNewSession(); tx = session.beginTransaction(); String hql = "from DeviceConfigureEntity where id = :id"; Query query = session.createQuery(hql); query.setString("id", deviceConfigureEntity.getId()); DeviceConfigureEntity databaseDeviceConfigureEntity = (DeviceConfigureEntity) query.setMaxResults(1) .uniqueResult(); if (databaseDeviceConfigureEntity == null) { throw new AsmManagerCheckedException(AsmManagerCheckedException.REASON_CODE.RECORD_NOT_FOUND, AsmManagerMessages.notFound(deviceConfigureEntity.getId())); } databaseDeviceConfigureEntity.setStatus(status); databaseDeviceConfigureEntity.setUpdatedDate(new GregorianCalendar()); databaseDeviceConfigureEntity.setUpdatedBy(_dao.extractUserFromRequest()); session.saveOrUpdate(databaseDeviceConfigureEntity); // Commit transaction. tx.commit(); updatedFlag = true; } catch (Exception e) { logger.warn("Caught exception during update device configure entity : " + e); try { if (tx != null) { tx.rollback(); updatedFlag = false; } } catch (Exception ex) { logger.warn("Unable to rollback transaction during update device configure entity : " + ex); } if (e instanceof AsmManagerCheckedException) { throw e; } throw new AsmManagerInternalErrorException("Update device", "DeviceConfigureDAO", e); } finally { try { if (session != null) { session.close(); } } catch (Exception ex) { logger.warn("Unable to close session during update device configure entity : " + ex); } } return updatedFlag; }
From source file:com.dell.asm.asmcore.asmmanager.db.DeviceDiscoverDAO.java
License:Open Source License
public boolean updateDeviceDiscoverEntity(DeviceDiscoverEntity deviceDiscoverEntity) throws AsmManagerCheckedException { Session session = null; Transaction tx = null;//from ww w .ja va 2s .c om boolean updatedFlag = false; try { session = _dao._database.getNewSession(); tx = session.beginTransaction(); String hql = "from DeviceDiscoverEntity where id = :id"; Query query = session.createQuery(hql); query.setString("id", deviceDiscoverEntity.getId()); DeviceDiscoverEntity databaseDeviceDiscoverEntity = (DeviceDiscoverEntity) query.setMaxResults(1) .uniqueResult(); if (databaseDeviceDiscoverEntity == null) { throw new AsmManagerCheckedException(AsmManagerCheckedException.REASON_CODE.RECORD_NOT_FOUND, AsmManagerMessages.notFound(deviceDiscoverEntity.getId())); } databaseDeviceDiscoverEntity.setStatus(deviceDiscoverEntity.getStatus()); databaseDeviceDiscoverEntity.setUpdatedDate(new GregorianCalendar()); databaseDeviceDiscoverEntity.setUpdatedBy(_dao.extractUserFromRequest()); session.saveOrUpdate(databaseDeviceDiscoverEntity); // Commit transaction. tx.commit(); updatedFlag = true; } catch (Exception e) { logger.warn("Caught exception during update device discover entity : " + e); try { if (tx != null) { tx.rollback(); updatedFlag = false; } } catch (Exception ex) { logger.warn("Unable to rollback transaction during update device discover entity : " + ex); } if (e instanceof AsmManagerCheckedException) { throw e; } throw new AsmManagerInternalErrorException("Update device discover entity", "DeviceDiscoverDAO", e); } finally { try { if (session != null) { session.close(); } } catch (Exception ex) { logger.warn("Unable to close session during update device discover entity : " + ex); } } return updatedFlag; }
From source file:com.dell.asm.asmcore.asmmanager.db.DeviceGroupDAO.java
License:Open Source License
/** * Update Device Group/*from w w w .j a va 2s.c o m*/ * * @return updateEntity - updated device group entity * * @throws AsmManagerCheckedException */ public DeviceGroupEntity updateGroupDevice(DeviceGroupEntity updateEntity) throws AsmManagerCheckedException { if (updateEntity == null) { return null; } // Initialize locals. Session session = null; Transaction tx = null; DeviceGroupEntity entity = null; // Save the job history in the db. try { session = _dao._database.getNewSession(); tx = session.beginTransaction(); // Save the new DeviceGroupEntity. String hql = "from DeviceGroupEntity where seqId = :id"; Query query = session.createQuery(hql); query.setLong("id", updateEntity.getSeqId()); entity = (DeviceGroupEntity) query.setMaxResults(1).uniqueResult(); if (null != entity) { if (null != updateEntity.getName() || !"".equals(updateEntity.getName())) entity.setName(updateEntity.getName()); if (null != updateEntity.getDescription()) entity.setDescription(updateEntity.getDescription()); entity.setUpdatedBy(_dao.extractUserFromRequest()); entity.setUpdatedDate(new GregorianCalendar()); if (null != updateEntity.getDeviceInventories()) entity.setDeviceInventories(updateEntity.getDeviceInventories()); if (null != updateEntity.getGroupsUsers()) entity.setGroupsUsers(updateEntity.getGroupsUsers()); session.saveOrUpdate(entity); // Commit transaction and clean up. tx.commit(); } else { throw new AsmManagerCheckedException(AsmManagerCheckedException.REASON_CODE.INVALID_REQUEST, AsmManagerMessages.updateDeviceGroupError(String.valueOf(updateEntity.getSeqId()))); } } catch (Exception e) { logger.warn("Caught exception during updating device group: " + e); try { if (tx != null) { tx.rollback(); } } catch (Exception ex) { logger.warn("Unable to rollback transaction during updating device group: " + ex); } throw new AsmManagerCheckedException(AsmManagerCheckedException.REASON_CODE.INVALID_REQUEST, AsmManagerMessages.updateDeviceGroupError(String.valueOf(updateEntity.getSeqId()))); } finally { try { if (session != null) { session.close(); } } catch (Exception ex) { logger.warn("Unable to close session during updating device group: " + ex); } } return entity; }
From source file:com.dell.asm.asmcore.asmmanager.db.DeviceInventoryComplianceDAO.java
License:Open Source License
public DeviceInventoryComplianceEntity saveOrUpdate(final DeviceInventoryComplianceEntity entity) { Session session = null; Transaction tx = null;// ww w .j av a 2 s.c o m try { session = dao.getNewSession(); tx = session.beginTransaction(); final String user = dao.extractUserFromRequest(); final Date now = new Date(); final DeviceInventoryComplianceEntity persisted = (DeviceInventoryComplianceEntity) session .get(DeviceInventoryComplianceEntity.class, entity.getDeviceInventoryComplianceId()); if (persisted == null) { entity.setCreatedBy(user); entity.setCreatedDate(now); entity.setUpdatedBy(user); entity.setUpdatedDate(now); session.save(entity); } else { persisted.setUpdatedBy(user); persisted.setUpdatedDate(now); persisted.setCompliance(entity.getCompliance()); session.saveOrUpdate(persisted); } tx.commit(); } catch (Exception e) { logger.warn("Caught exception during saveOrUpdate: " + e); try { if (tx != null) { tx.rollback(); } } catch (Exception ex) { logger.warn("Unable to rollback transaction during saveOrUpdate: " + ex); } throw new AsmManagerInternalErrorException("saveOrUpdate", "DeviceInventoryComplianceDAO", e); } finally { dao.cleanupSession(session, "saveOrUpdate"); } return entity; }
From source file:com.dell.asm.asmcore.asmmanager.db.DeviceInventoryDAO.java
License:Open Source License
/** * Update Device Inventory.// w w w . ja v a 2 s .c o m * * @param newDevice the device to update. */ public void updateDeviceInventory(DeviceInventoryEntity newDevice) throws AsmManagerCheckedException { Session session = null; Transaction tx = null; logger.info("DeviceInventoryDAO.updateDeviceInventory for device with refId " + newDevice.getRefId()); logger.info( "DeviceInventoryDAO.updateDeviceInventory for device with ipAddress " + newDevice.getIpAddress()); logger.info( "DeviceInventoryDAO.updateDeviceInventory for device with serviceTag " + newDevice.getServiceTag()); try { session = _dao._database.getNewSession(); tx = session.beginTransaction(); String hql = "from DeviceInventoryEntity where refId = :refId"; Query query = session.createQuery(hql); query.setString("refId", newDevice.getRefId()); DeviceInventoryEntity deviceInventoryEntity = (DeviceInventoryEntity) query.setMaxResults(1) .uniqueResult(); if (deviceInventoryEntity == null) { throw new AsmManagerCheckedException(AsmManagerCheckedException.REASON_CODE.RECORD_NOT_FOUND, AsmManagerMessages.notFound(newDevice.getRefId())); } deviceInventoryEntity.setRefType(newDevice.getRefType()); deviceInventoryEntity.setDeviceType(newDevice.getDeviceType()); deviceInventoryEntity.setManagedState(newDevice.getManagedState()); deviceInventoryEntity.setState(newDevice.getState()); deviceInventoryEntity.setDisplayName(newDevice.getDisplayName()); deviceInventoryEntity.setServiceTag(newDevice.getServiceTag()); deviceInventoryEntity.setIpAddress(newDevice.getIpAddress()); deviceInventoryEntity.setModel(newDevice.getModel()); deviceInventoryEntity.setComplianceCheckDate(newDevice.getComplianceCheckDate()); deviceInventoryEntity.setDiscoveredDate(newDevice.getDiscoveredDate()); deviceInventoryEntity.setHealth(newDevice.getHealth()); deviceInventoryEntity.setHealthMessage(newDevice.getHealthMessage()); deviceInventoryEntity.setInfraTemplateDate(newDevice.getInfraTemplateDate()); deviceInventoryEntity.setInfraTemplateId(newDevice.getInfraTemplateId()); deviceInventoryEntity.setIdentityRefId(newDevice.getIdentityRefId()); deviceInventoryEntity.setInventoryDate(newDevice.getInventoryDate()); deviceInventoryEntity.setServerTemplateDate(newDevice.getServerTemplateDate()); deviceInventoryEntity.setServerTemplateId(newDevice.getServerTemplateId()); deviceInventoryEntity.setConfig(newDevice.getConfig()); deviceInventoryEntity.setSystemId(newDevice.getSystemId()); deviceInventoryEntity.setOsImageType(newDevice.getOsImageType()); deviceInventoryEntity.setOsAdminPassword(newDevice.getOsAdminPassword()); deviceInventoryEntity.setOsIpAddress(newDevice.getOsIpAddress()); deviceInventoryEntity.setFailuresCount(newDevice.getFailuresCount()); deviceInventoryEntity.setCompliant(newDevice.getCompliant()); deviceInventoryEntity.setChassisId(newDevice.getChassisId()); deviceInventoryEntity.setUpdatedDate(new GregorianCalendar()); deviceInventoryEntity.setUpdatedBy(_dao.extractUserFromRequest()); if (newDevice.getDeviceGroupList() != null && !newDevice.getDeviceGroupList().isEmpty()) { deviceInventoryEntity.setDeviceGroupList(newDevice.getDeviceGroupList()); } deviceInventoryEntity.setFacts(newDevice.getFacts()); session.saveOrUpdate(deviceInventoryEntity); // Commit transaction. tx.commit(); } catch (Exception e) { logger.warn("Caught exception during update device: " + e, e); try { if (tx != null) { tx.rollback(); } } catch (Exception ex) { logger.warn("Unable to rollback transaction during update device: " + ex); } if (e instanceof StaleObjectStateException) { StaleObjectStateException sose = ((StaleObjectStateException) e); logger.warn("StaleObjectStateException for device with refId " + newDevice.getRefId()); logger.warn("StaleObjectStateException for device with ipAddress " + newDevice.getIpAddress()); logger.warn("StaleObjectStateException for device with serviceTag " + newDevice.getServiceTag()); } if (e instanceof AsmManagerCheckedException) { throw e; } throw new AsmManagerInternalErrorException("Update device", "DeviceInventoryDAO", e); } finally { try { if (session != null) { session.close(); } } catch (Exception ex) { logger.warn("Unable to close session during update device: " + ex); } } }
From source file:com.dell.asm.asmcore.asmmanager.db.DeviceInventoryDAO.java
License:Open Source License
/** * Create a new or update existing last job state table based on given device ref id * and job type./* w w w. j a va 2s . co m*/ * * @param deviceRefId * @param jobType * @param successful * @param description */ public void createOrUpdateLastJob(String deviceRefId, JobType jobType, DeviceState successful, String description) { Session session = null; Transaction tx = null; try { session = _dao._database.getNewSession(); tx = session.beginTransaction(); String hql = "from DeviceLastJobStateEntity where deviceRefId = :deviceRefId and jobType = :jobType"; Query query = session.createQuery(hql); query.setString("deviceRefId", deviceRefId); query.setString("jobType", jobType.name()); DeviceLastJobStateEntity lastJob = (DeviceLastJobStateEntity) query.setMaxResults(1).uniqueResult(); if (lastJob == null) { lastJob = new DeviceLastJobStateEntity(); lastJob.setDeviceRefId(deviceRefId); lastJob.setJobType(jobType); lastJob.setCreatedDate(new GregorianCalendar()); } else { lastJob.setUpdatedDate(new GregorianCalendar()); } lastJob.setDescription(description); lastJob.setJobState(successful); session.saveOrUpdate(lastJob); // Commit transaction. tx.commit(); } catch (Exception e) { logger.warn("Caught exception during create or update last job: ", e); try { if (tx != null) { tx.rollback(); } } catch (Exception ex) { logger.warn("Unable to rollback transaction during create or update last job: " + ex); } throw new AsmManagerInternalErrorException("Create or Update Last Job", "DeviceInventoryDAO", e); } finally { try { if (session != null) { session.close(); } } catch (Exception ex) { logger.warn("Unable to close session during create or update last job: " + ex); } } }
From source file:com.dell.asm.asmcore.asmmanager.db.DiscoveryResultDAO.java
License:Open Source License
/** * create or Update Discovery Result./*from w w w .j av a2 s .c o m*/ * @param newResult the result to update. */ public void createOrUpdateDiscoveryResult(DiscoveryResultEntity newResult) throws AsmManagerCheckedException { Session session = null; Transaction tx = null; DiscoveryResultEntity discoveryResultEntity = new DiscoveryResultEntity(); try { session = _dao._database.getNewSession(); tx = session.beginTransaction(); String hql = "from DiscoveryResultEntity where refId = :refId"; Query query = session.createQuery(hql); query.setString("refId", newResult.getRefId()); discoveryResultEntity = (DiscoveryResultEntity) query.setMaxResults(1).uniqueResult(); if (discoveryResultEntity == null) { discoveryResultEntity = new DiscoveryResultEntity(); discoveryResultEntity.setRefId(newResult.getRefId()); discoveryResultEntity.setRefType(newResult.getRefType()); discoveryResultEntity.setParentJobId(newResult.getParentJobId()); discoveryResultEntity.setJobId(newResult.getJobId()); } discoveryResultEntity.setDeviceRefId(newResult.getDeviceRefId()); discoveryResultEntity.setDeviceType(newResult.getDeviceType()); discoveryResultEntity.setStatus(newResult.getStatus()); discoveryResultEntity.setStatusMessage(newResult.getStatusMessage()); discoveryResultEntity.setServiceTag(newResult.getServiceTag()); discoveryResultEntity.setIpaddress(newResult.getIpaddress()); discoveryResultEntity.setModel(newResult.getModel()); //logger.info("serer count and iom count: " + newResult.getServerCount() + " : "+newResult.getIomCount()); discoveryResultEntity.setServerCount(newResult.getServerCount()); discoveryResultEntity.setIomCount(newResult.getIomCount()); discoveryResultEntity.setServerType(newResult.getServerType()); discoveryResultEntity.setHealthState(newResult.getHealthState()); discoveryResultEntity.setHealthStatusMsg(newResult.getHealthStatusMsg()); discoveryResultEntity.setVendor(newResult.getVendor()); discoveryResultEntity.setSystem_id(newResult.getSystem_id()); discoveryResultEntity.setUpdatedDate(new Date()); discoveryResultEntity.setUpdatedBy(_dao.extractUserFromRequest()); discoveryResultEntity.setFacts(newResult.getFacts()); discoveryResultEntity.setUnmanaged(newResult.isUnmanaged()); discoveryResultEntity.setReserved(newResult.isReserved()); discoveryResultEntity.setConfig(newResult.getConfig()); discoveryResultEntity.setDiscoverDeviceType(newResult.getDiscoverDeviceType()); discoveryResultEntity.setServerPoolId(newResult.getServerPoolId()); if (newResult.getFirmwareList() != null) for (FirmwareDeviceInventoryEntity fdie : newResult.getFirmwareList()) { discoveryResultEntity.addFirmwareDeviceInventoryEntity(fdie); } session.saveOrUpdate(discoveryResultEntity); // Commit transaction. tx.commit(); } catch (ConstraintViolationException cve) { logger.warn("Caught exception during discovery result creation: " + cve); try { if (tx != null) { tx.rollback(); } } catch (Exception ex) { logger.warn("Unable to rollback transaction during create discovery result: " + ex); } if (cve.getConstraintName().contains("refid")) { //throw new AsmManagerDAOException(AsmManagerDAOException.REASON_CODE.DUPLICATE_JOBID, cve); throw new AsmManagerCheckedException(AsmManagerCheckedException.REASON_CODE.DUPLICATE_JOBID, AsmManagerMessages.duplicateRefId(cve.getSQLException().getMessage())); } else { //throw new AsmManagerDAOException(AsmManagerDAOException.REASON_CODE.DUPLICATE_RECORD, cve); throw new AsmManagerCheckedException(AsmManagerCheckedException.REASON_CODE.DUPLICATE_RECORD, AsmManagerMessages.duplicateRefId(cve.getSQLException().getMessage())); } } catch (Exception e) { logger.warn("Caught exception during update device: " + e); try { if (tx != null) { tx.rollback(); } } catch (Exception ex) { logger.warn("Unable to rollback transaction during update device: " + ex); } if (e instanceof AsmManagerCheckedException) { throw e; } throw new AsmManagerInternalErrorException("Error updating the discovery result", "DiscoveryResultDAO", e); } finally { try { if (session != null) { session.close(); } } catch (Exception ex) { logger.warn("Unable to close session during update device: " + ex); } } }
From source file:com.dell.asm.asmcore.asmmanager.db.FirmwareRepositoryDAO.java
License:Open Source License
public FirmwareRepositoryEntity merge(final FirmwareRepositoryEntity original, final FirmwareRepositoryEntity update) { Session session = null; Transaction tx = null;/* w w w.j av a 2s . com*/ try { session = dao.getNewSession(); tx = session.beginTransaction(); if (original != null) { final String originalId = original.getId(); session.load(original, originalId); original.getDeployments().clear(); original.getSoftwareBundles().clear(); original.getSoftwareComponents().clear(); original.getTemplates().clear(); original.getDeviceInventoryComplianceEntities().clear(); session.flush(); // reload the original entity in cache so we can merge session.get(FirmwareRepositoryEntity.class, originalId); update.setId(originalId); update.setCreatedDate(original.getCreatedDate()); update.setCreatedBy(original.getCreatedBy()); update.setUpdatedDate(new Date()); update.setUpdatedBy(dao.extractUserFromRequest()); session.merge(update); } else { update.setCreatedDate(new Date()); update.setCreatedBy(dao.extractUserFromRequest()); update.setUpdatedDate(update.getCreatedDate()); update.setUpdatedBy(update.getCreatedBy()); session.saveOrUpdate(update); } // Commit transaction. tx.commit(); } catch (Exception e) { logger.warn("Caught exception during merge: " + e); try { if (tx != null) { tx.rollback(); } } catch (Exception ex) { logger.warn("Unable to rollback transaction during merge: " + ex); } throw new AsmManagerInternalErrorException("merge", "FirmwareRepositoryDAO", e); } finally { dao.cleanupSession(session, "merge"); } return original; }
From source file:com.dell.asm.asmcore.asmmanager.db.ServiceTemplateDAO.java
License:Open Source License
public ServiceTemplateEntity updateTemplate(ServiceTemplateEntity updatedTemplate) throws AsmManagerInternalErrorException { Session session = null; Transaction tx = null;/*ww w .j a v a 2s . c o m*/ ServiceTemplateEntity templateEntity = null; try { session = _dao._database.getNewSession(); tx = session.beginTransaction(); String hql = "from ServiceTemplateEntity where template_id = :id"; Query query = session.createQuery(hql); query.setString("id", updatedTemplate.getTemplateId()); templateEntity = (ServiceTemplateEntity) query.setMaxResults(1).uniqueResult(); if (templateEntity != null) { templateEntity.setTemplateDesc(updatedTemplate.getTemplateDesc()); templateEntity.setTemplateVersion(updatedTemplate.getTemplateVersion()); templateEntity.setTemplateValid(updatedTemplate.isTemplateValid()); templateEntity.setTemplateLocked(updatedTemplate.isTemplateLocked()); // templateEntity.setDisplayName(updatedTemplate.getDisplayName()); // templateEntity.setDeviceType(updatedTemplate.getDeviceType()); templateEntity.setDraft(updatedTemplate.isDraft()); templateEntity.setName(updatedTemplate.getName()); templateEntity.setUpdatedBy(_dao.extractUserFromRequest()); //TODO remove the set updated date when @PreUpdate is working GregorianCalendar now = new GregorianCalendar(TimeZone.getTimeZone("GMT")); templateEntity.setUpdatedDate(now); templateEntity.setLastDeployedDate(updatedTemplate.getLastDeployedDate()); templateEntity.setMarshalledTemplateData(updatedTemplate.getMarshalledTemplateData()); templateEntity.setManageFirmware(updatedTemplate.isManageFirmware()); templateEntity.setUseDefaultCatalog(updatedTemplate.isUseDefaultCatalog()); templateEntity.setFirmwareRepositoryEntity(updatedTemplate.getFirmwareRepositoryEntity()); templateEntity.setAllUsersAllowed(updatedTemplate.isAllUsersAllowed()); if (templateEntity.getAssignedUserList() != null) { templateEntity.getAssignedUserList().clear(); } else { Set<TemplateUserRefEntity> userRefEntities = new HashSet<>(); templateEntity.setAssignedUserList(userRefEntities); } for (TemplateUserRefEntity uRef : updatedTemplate.getAssignedUserList()) { uRef.setId(UUID.randomUUID().toString()); templateEntity.getAssignedUserList().add(uRef); } updateAddOnModules(templateEntity, updatedTemplate); session.saveOrUpdate(templateEntity); //commit tx.commit(); } else { String msg = "unable to update template with name " + updatedTemplate.getName(); logger.warn(msg); } } catch (Exception e) { logger.warn("Caught exception during update template: " + e); try { if (tx != null) { tx.rollback(); } } catch (Exception ex) { logger.warn("Unable to rollback transaction during update template: " + ex); } // TODO: Reviewer: instanceof will always return false since a RuntimeException can't be a com.dell.asm.asmcore.asmmanager.exception.AsmManagerCheckedException //if (e instanceof AsmManagerCheckedException) { // throw e; //} throw new AsmManagerInternalErrorException("update Template", "TemplateDAO", e); } finally { try { if (session != null) { session.close(); } } catch (Exception ex) { logger.warn("Unable to close session during update template: " + ex); } } return templateEntity; }