private void hardDelete(Collection ids) { String tblName = hasEO() ? eoClass.getSimpleName() : voClass.getSimpleName(); String sql = String.format("delete from %s eo where eo.%s in (:ids)", tblName, voPrimaryKeyField.getName()); Query q = getEntityManager().createQuery(sql); q.setParameter("ids", ids); q.executeUpdate(); logger.debug(String.format("hard delete %s records from %s", ids.size(), tblName)); fireHardDeleteExtension(ids); }
@Transactional(propagation = Propagation.REQUIRES_NEW) private void _eoCleanup(Class VOClazz, List ids) { EntityInfo info = getEntityInfo(VOClazz); if (!info.hasEO()) { logger.warn(String.format("Class[%s] doesn't has EO.", VOClazz)); return; } String deleted = info.eoSoftDeleteColumn.getName(); String sql = String.format("select eo.%s from %s eo where eo.%s is not null and eo.%s in (:ids)", info.voPrimaryKeyField.getName(), info.eoClass.getSimpleName(), deleted, info.voPrimaryKeyField.getName()); Query q = getEntityManager().createQuery(sql); q.setParameter("ids", ids); ids = q.getResultList(); if (ids.isEmpty()) { return; } info.hardDelete(ids); }
@Transactional(propagation = Propagation.REQUIRES_NEW) private void _eoCleanup(Class VOClazz) { EntityInfo info = getEntityInfo(VOClazz); if (!info.hasEO()) { logger.warn(String.format("Class[%s] doesn't has EO.", VOClazz)); return; } String deleted = info.eoSoftDeleteColumn.getName(); String sql = String.format("select eo.%s from %s eo where eo.%s is not null", info.voPrimaryKeyField.getName(), info.eoClass.getSimpleName(), deleted); Query q = getEntityManager().createQuery(sql); List ids = q.getResultList(); if (ids.isEmpty()) { return; } info.hardDelete(ids); }
if (!hasEO()) { List<SoftDeleteEntityExtensionPoint> exts = softDeleteExtensions.get(parent); if (exts == null) {
private void updateEO(Object entity, DataIntegrityViolationException de) { if (!MySQLIntegrityConstraintViolationException.class.isAssignableFrom(de.getRootCause().getClass())) { throw de; } MySQLIntegrityConstraintViolationException me = (MySQLIntegrityConstraintViolationException) de.getRootCause(); if (!(me.getErrorCode() == 1062 && "23000".equals(me.getSQLState()) && me.getMessage().contains("PRIMARY"))) { throw de; } if (!hasEO()) { throw de; } // at this point, the error is caused by a update tried on VO entity which has been soft deleted. This is mostly // caused by a deletion cascade(e.g deleting host will cause vm running on it to be deleted, and deleting vm is trying to return capacity // to host which has been soft deleted, because vm deletion is executed in async manner). In this case, we make the update to EO table Object idval = getEOPrimaryKeyValue(entity); Object eo = getEntityManager().find(eoClass, idval); final Object deo = ObjectUtils.copy(eo, entity); new Runnable() { @Override @Transactional(propagation = Propagation.REQUIRES_NEW) public void run() { getEntityManager().merge(deo); } }.run(); logger.debug(String.format("A EO[%s] update has been made", eoClass.getName())); }
@Transactional(propagation = Propagation.REQUIRES_NEW) void removeByPrimaryKey(Object id) { if (hasEO()) { softDelete(list(id)); } else { hardDelete(list(id)); } }
@Transactional(propagation = Propagation.REQUIRES_NEW) void remove(Object entity) { if (!hasEO()) { hardDelete(entity); } else { softDelete(entity); } }
@Transactional(propagation = Propagation.REQUIRES_NEW) void removeByPrimaryKeys(Collection ids) { if (hasEO()) { softDelete(ids); } else { hardDelete(ids); } }