2017-07-03 74 views
1

是否有任何其他不同的解決方案用於此代碼。 對於每個POJO類,我們必須檢查來自瀏覽器的修改後的數據,我們只會將修改後的數據存儲到數據庫中。當實體對象的只有幾個字段被修改並且其餘字段不應該爲空時,如何更新Hibernate實體記錄

見下文billingTax obj是從瀏覽器內被更新數據 和billingtaxDbObject obj是從數據庫中檢索,我們將與if條件檢查更新後的數據是否被更改或不

如果POJO類有20個領域的未來,我們必須寫條件如果條件 如果pojo類有5個字段,我們必須編寫5條件如果條件爲

而不是寫入如果檢查數據的條件是否被修改或者是否有其他最簡單的方法?

@Override 

    public BillingTax update(BillingTax billingTax) throws DataInsufficientException, RecordNotFoundException { 
     log.debug("BillingTaxServiceImpl.update()...................."); 
    try { 

     if (billingTax == null) 
      throw new DataInsufficientException("billingTax object is null"); 

     BillingTax billingtaxDbObject = get(billingTax.getId()); 

     if (billingtaxDbObject == null) 
      throw new RecordNotFoundException("billingTax object is not found in database"); 

     if (billingTax.getTaxApplyType() != null 
       && !billingTax.getTaxApplyType().equals(billingtaxDbObject.getTaxApplyType())) 
      billingtaxDbObject.setTaxApplyType(billingTax.getTaxApplyType()); 

     if (billingTax.getCode() != null && !billingTax.getCode().trim().equalsIgnoreCase("null") 
       && !billingTax.getCode().equalsIgnoreCase(billingtaxDbObject.getCode())) 
      billingtaxDbObject.setCode(billingTax.getCode()); 

     if (billingTax.getName() != null && !billingTax.getName().trim().equalsIgnoreCase("null") 
       && !billingTax.getName().equalsIgnoreCase(billingtaxDbObject.getName())) 
      billingtaxDbObject.setName(billingTax.getName()); 

     if (billingTax.getDescription() != null && !billingTax.getDescription().trim().equalsIgnoreCase("null") 
       && !billingTax.getDescription().equalsIgnoreCase(billingtaxDbObject.getDescription())) 
      billingtaxDbObject.setDescription(billingTax.getDescription()); 

     if (billingTax.getServiceTypeForTax() != null 
       && !billingTax.getServiceTypeForTax().equals(billingtaxDbObject.getServiceTypeForTax())) 
      billingtaxDbObject.setServiceTypeForTax(billingTax.getServiceTypeForTax()); 

     if (billingTax.getTaxValue() != null && !billingTax.getTaxValue().equals("null") 
       && !billingTax.getTaxValue().equals(billingtaxDbObject.getTaxValue())) 
      billingtaxDbObject.setTaxValue(billingTax.getTaxValue()); 

     if (billingTax.getStatus() != null && !billingTax.getStatus().equals(billingtaxDbObject.getStatus())) 
      billingtaxDbObject.setStatus(billingTax.getStatus()); 

     if (billingTax.getOrderNo() != null && !billingTax.getOrderNo().equals("null") 
       && !billingTax.getOrderNo().equals(billingtaxDbObject.getOrderNo())) 
      billingtaxDbObject.setOrderNo(billingTax.getOrderNo()); 

     if (billingTax.getId() != null && !billingTax.getId().trim().equalsIgnoreCase(billingtaxDbObject.getId()) 
       && !billingTax.getId().equalsIgnoreCase(billingtaxDbObject.getId())) 
      billingtaxDbObject.setId(billingTax.getId()); 

     if (billingTax.getStartDate()!= null && !billingTax.getStartDate().equals(billingtaxDbObject.getStartDate())) 
      billingtaxDbObject.setStartDate(billingTax.getStartDate()); 

     if (billingTax.getEndDate()!= null && !billingTax.getEndDate().equals(billingtaxDbObject.getEndDate())) 
      billingtaxDbObject.setEndDate(billingTax.getEndDate()); 

     billingtaxDbObject.setUpdatedDate(new Date()); 
     return billingTaxDAO.update(billingtaxDbObject); 

    } catch (Exception e) { 
     log.error("BillingTaxServiceImpl.update()....................exception:" + e.getMessage()); 
     throw e; 
    } 
} 
+0

爲什麼你需要檢查他們呼籲集之前有什麼不同? – shmosel

+0

以避免在數據庫中存儲空值。 –

+0

你已經在檢查空值了。爲什麼你需要檢查他們*不同*? – shmosel

回答

0

這是通過從pojo類獲取所有字段/屬性並使用null數據或修改數據進行檢查而開發的。 以下是代碼:

copyProperties(billingTax,billingtaxDbObject);

公共無效copyProperties(BillingTax源,BillingTax DEST)拋出異常{

if (source == null) 
     throw new DataInsufficientException("billingTax object is null"); 

    if (dest == null) 
     throw new RecordNotFoundException("billingtaxDbObject object is not found in database"); 

    try{ 
     for (Field field : source.getClass().getDeclaredFields()) { 

      field.setAccessible(true); 
      Object sourceValue = field.get(source); 
      Object destValue=field.get(dest); 

      if(sourceValue!=null && sourceValue!="" && sourceValue!="null"){ 

       if(sourceValue instanceof String){ 
        if(!sourceValue.toString().trim().equalsIgnoreCase("null")){ 
         if(!sourceValue.toString().equalsIgnoreCase(destValue.toString())){ 
          field.set(dest, sourceValue); 
          System.err.println(field.getName()+" is modified:"+field.get(dest)); 
         } 
        } 
       } 
       else{ 
        if(!sourceValue.equals("null") && !sourceValue.equals(destValue)){ 
         field.set(dest, sourceValue); 
         System.err.println(field.getName()+" is modified:"+field.get(dest)); 
        } 
       } 
      } 
     } 
     System.out.println(); 
    }catch (Exception e) { 
     throw e; 
    } 
} 
1

可以與Hibernate的動態更新做,如果你能避免DTO和實體之間檢查的變化和更新來自網絡的所有領域。如果你需要從web和實體中檢查dto,你可以使用apache bean util來找到所有更改的值(或者如果你有java或反射,使用spring util),並用動態更新進行更新。

看到:BeanUtils

BeanUtils.copyProperties()//有3種方法。

檢查它在源代碼中的工作原理。 創建您自己的util方法,類似於BeanUtils.copyProperties(),但使用您需要的邏輯(不爲null且不等於源實體值)。

而且使用方法從BeanUtils的,得到的PropertyDescriptor:

公共靜態的PropertyDescriptor [] getPropertyDescriptors(類clazz所) 拋出BeansException

疊代的PropertyDescriptor的數組,並做檢查,你需要(將值設定爲ReflectionUtils)。

使用這種方法,您只能將不爲空的屬性填充到billingtaxDbObject中並更新它(如果需要的話)。

您可以將您的複製/合併方法放入某個util類中,並將其用於需要從dto複製到具有一些檢查的實體的所有位置。

+0

我們正在使用SpringBoot.but是新的spring.i想知道如何使用spring util.could你請解釋一個例子 –

+0

通過使用BeanUtils,我無法理解源代碼。因此,我創建了自己的邏輯,將檢查不是空字段,並修改從源到目標obj的數據。 –

+0

能否請你解釋一個pojo類與兩個領域。 –

0
public class CopyConverter<T> { 

    private List<String> errorMessages = new ArrayList<>(); 
    private int countSuccess = 0; 

    public CopyConverter<T> convert(T source, T target, Set<String> ignoreFields){ 
     copyProperties(source,target,ignoreFields); 
     return this; 
    } 

    public boolean hasError(){ 
     return errorMessages.isEmpty(); 
    } 

    public List<String> getErrorMessages(){ 
     return errorMessages; 
    } 

    private boolean copyProperties(T source ,T target , Set<String> ignoreFields) { 

     Objects.requireNonNull(source , "..error message..."); 
     Objects.requireNonNull(target , "..error message..."); 

     try { 
      Map<String, Field> fieldNameMapping = buildFiledMap(target.getClass(). 
                 getDeclaredFields()); 
      ignoreFields = (ignoreFields == null ? new HashSet<>() : ignoreFields); 


      for (Map.Entry<String, Field> fieldEntry : fieldNameMapping.entrySet()) { 
       if (ignoreFields.contains(fieldEntry.getKey())) { 
        continue; 
       } 
       Field field = fieldEntry.getValue(); 
       field.setAccessible(true); 
       Object sourceValue = field.get(source); 
       Object targetValue = field.get(source); 

       if (isChangedAsString(sourceValue, targetValue)) { 
        field.set(target, sourceValue); 
        continue; 
       } 
       if (isChangedAsObject(sourceValue, targetValue)) { 
        field.set(target, sourceValue); 
       } 
       countSuccess++; 
      } 
     } catch (Exception e) { 
      errorMessages.add("......."); 
      log.info(....); 
     } 
     return countSuccess == 0; 
    } 

    private Map<String, Field> buildFiledMap(Field[] fields) { 
     Map<String, Field> fieldMap = new HashMap<>(fields.length); 
     //Stream.of(fields).collect(Collectors.toMap()) 
     for (Field field : fields) { 
      fieldMap.put(field.getName(), field); 
     } 
     return fieldMap; 
    } 

    private boolean isChangedAsObject(Object obj1, Object obj2) { 
     return (obj1 == null && obj2 != null) || (obj1 != null && !obj1.equals(obj1)); 
    } 


    private boolean isChangedAsString(Object obj1, Object obj2) { 
     if (obj1 instanceof String && obj2 instanceof String) { 
      String value1 = (String) obj1; 
      String value2 = (String) obj2; 
      return value1 != null && 
        !value1.trim().equalsIgnoreCase("null") &&//strange check 
        !value1.equalsIgnoreCase(value2); 
     } 
     return false; 
    } 
} 
相關問題