Ejemplo n.º 1
0
 /**
  * Clear associated models' save state. After this method, the associated models of baseObj which
  * data is removed from database will become unsaved.
  *
  * @param baseObj The record to delete.
  * @param associationInfos The associated info.
  */
 private void clearAssociatedModelSaveState(
     DataSupport baseObj, Collection<AssociationsInfo> associationInfos) {
   try {
     for (AssociationsInfo associationInfo : associationInfos) {
       if (associationInfo.getAssociationType() == Const.Model.MANY_TO_ONE
           && !baseObj.getClassName().equals(associationInfo.getClassHoldsForeignKey())) {
         Collection<DataSupport> associatedModels = getAssociatedModels(baseObj, associationInfo);
         if (associatedModels != null && !associatedModels.isEmpty()) {
           for (DataSupport model : associatedModels) {
             if (model != null) {
               model.resetBaseObjId();
             }
           }
         }
       } else if (associationInfo.getAssociationType() == Const.Model.ONE_TO_ONE) {
         DataSupport model = getAssociatedModel(baseObj, associationInfo);
         if (model != null) {
           model.resetBaseObjId();
         }
       }
     }
   } catch (Exception e) {
     throw new DataSupportException(e.getMessage());
   }
 }
Ejemplo n.º 2
0
 /**
  * Analyze the associations of modelClass and store the associated tables. The associated tables
  * might be used when deleting referenced data of a specified row.
  *
  * @param modelClass To get associations of this class.
  */
 private void analyzeAssociations(Class<?> modelClass) {
   Collection<AssociationsInfo> associationInfos = getAssociationInfo(modelClass.getName());
   for (AssociationsInfo associationInfo : associationInfos) {
     String associatedTableName =
         DBUtility.getTableNameByClassName(associationInfo.getAssociatedClassName());
     if (associationInfo.getAssociationType() == Const.Model.MANY_TO_ONE
         || associationInfo.getAssociationType() == Const.Model.ONE_TO_ONE) {
       String classHoldsForeignKey = associationInfo.getClassHoldsForeignKey();
       if (!modelClass.getName().equals(classHoldsForeignKey)) {
         getForeignKeyTableToDelete().add(associatedTableName);
       }
     } else if (associationInfo.getAssociationType() == Const.Model.MANY_TO_MANY) {
       String joinTableName =
           DBUtility.getIntermediateTableName(getTableName(modelClass), associatedTableName);
       joinTableName = BaseUtility.changeCase(joinTableName);
       getForeignKeyTableToDelete().add(joinTableName);
     }
   }
 }