Examples of SpiTransaction


Examples of com.avaje.ebeaninternal.api.SpiTransaction

  /**
   * Delete by Id or a List of Id's.
   */
  private int delete(BeanDescriptor<?> descriptor, Object id, List<Object> idList, Transaction transaction) {

    SpiTransaction t = (SpiTransaction) transaction;
    if (t.isPersistCascade()) {
      BeanPropertyAssocOne<?>[] propImportDelete = descriptor.propertiesOneImportedDelete();
      if (propImportDelete.length > 0) {
        // We actually need to execute a query to get the foreign key values
        // as they are required for the delete cascade. Query back just the
        // Id and the appropriate foreign key values
        Query<?> q = deleteRequiresQuery(descriptor, propImportDelete);
        if (idList != null) {
          q.where().idIn(idList);
          if (t.isLogSummary()) {
            t.logSummary("-- DeleteById of " + descriptor.getName() + " ids[" + idList + "] requires fetch of foreign key values");
          }
          List<?> beanList = server.findList(q, t);
          deleteList(beanList, t);
          return beanList.size();

        } else {
          q.where().idEq(id);
          if (t.isLogSummary()) {
            t.logSummary("-- DeleteById of " + descriptor.getName() + " id[" + id + "] requires fetch of foreign key values");
          }
          EntityBean bean = (EntityBean)server.findUnique(q, t);
          if (bean == null) {
            return 0;
          } else {
            delete(bean, t);
            return 1;
          }
        }
      }
    }

    if (t.isPersistCascade()) {
      // OneToOne exported side with delete cascade
      BeanPropertyAssocOne<?>[] expOnes = descriptor.propertiesOneExportedDelete();
      for (int i = 0; i < expOnes.length; i++) {       
        BeanDescriptor<?> targetDesc = expOnes[i].getTargetDescriptor();
        if (targetDesc.isDeleteRecurseSkippable() && !targetDesc.isBeanCaching()) {
          SqlUpdate sqlDelete = expOnes[i].deleteByParentId(id, idList);
          executeSqlUpdate(sqlDelete, t);
        } else {
          List<Object> childIds = expOnes[i].findIdsByParentId(id, idList, t);
          deleteChildrenById(t, targetDesc, childIds);
        }
      }

      // OneToMany's with delete cascade
      BeanPropertyAssocMany<?>[] manys = descriptor.propertiesManyDelete();
      for (int i = 0; i < manys.length; i++) {
        BeanDescriptor<?> targetDesc = manys[i].getTargetDescriptor();
        if (targetDesc.isDeleteRecurseSkippable() && !targetDesc.isBeanCaching()) {
          // we can just delete children with a single statement
          SqlUpdate sqlDelete = manys[i].deleteByParentId(id, idList);
          executeSqlUpdate(sqlDelete, t);
        } else {
          // we need to fetch the Id's to delete (recurse or notify L2 cache)
          List<Object> childIds = manys[i].findIdsByParentId(id, idList, t, null);
          if (!childIds.isEmpty()) {
            delete(targetDesc, null, childIds, t);
          }
        }
      }
    }

    // ManyToMany's ... delete from intersection table
    BeanPropertyAssocMany<?>[] manys = descriptor.propertiesManyToMany();
    for (int i = 0; i < manys.length; i++) {
      SqlUpdate sqlDelete = manys[i].deleteByParentId(id, idList);
      if (t.isLogSummary()) {
        t.logSummary("-- Deleting intersection table entries: " + manys[i].getFullBeanName());
      }
      executeSqlUpdate(sqlDelete, t);
    }

    // delete the bean(s)
    SqlUpdate deleteById = descriptor.deleteById(id, idList);
    if (t.isLogSummary()) {
      t.logSummary("-- Deleting " + descriptor.getName() + " Ids" + idList);
    }

    // use Id's to update L2 cache rather than Bulk table event
    deleteById.setAutoTableMod(false);
    if (idList != null) {
      t.getEvent().addDeleteByIdList(descriptor, idList);
    } else {
      t.getEvent().addDeleteById(descriptor, id);
    }
    int rows = executeSqlUpdate(deleteById, t);
   
    // Delete from the persistence context so that it can't be fetched again later
    PersistenceContext persistenceContext = ((SpiTransaction)t).getPersistenceContext();
View Full Code Here

Examples of com.avaje.ebeaninternal.api.SpiTransaction

   */
  private void saveAssocMany(boolean insertedParent, PersistRequestBean<?> request, boolean insertMode) {

    EntityBean parentBean = request.getEntityBean();
    BeanDescriptor<?> desc = request.getBeanDescriptor();
    SpiTransaction t = request.getTransaction();

    // exported ones with cascade save
    BeanPropertyAssocOne<?>[] expOnes = desc.propertiesOneExportedSave();
    for (int i = 0; i < expOnes.length; i++) {
      BeanPropertyAssocOne<?> prop = expOnes[i];

      // check for partial beans
      if (request.isLoadedProperty(prop)) {
        EntityBean detailBean = prop.getValueAsEntityBean(parentBean);
        if (detailBean != null) {
          if (prop.isSaveRecurseSkippable(detailBean)) {
            // skip saving this bean
          } else {
            t.depth(+1);
            prop.setParentBeanToChild(parentBean, detailBean);
            saveRecurse(detailBean, t, parentBean, insertMode);
            t.depth(-1);
          }
        }
      }
    }

View Full Code Here

Examples of com.avaje.ebeaninternal.api.SpiTransaction

      BeanCollection<?> c = (BeanCollection<?>) details;
      Set<?> modifyRemovals = c.getModifyRemovals();
      if (modifyRemovals != null && !modifyRemovals.isEmpty()) {

        SpiTransaction t = saveMany.getTransaction();
        // increase depth for batching order
        t.depth(+1);
        for (Object removedBean : modifyRemovals) {
          if (removedBean instanceof EntityBean) {
            EntityBean eb = (EntityBean)removedBean;
            if (eb._ebean_getIntercept().isLoaded()) {
              // only delete if the bean was loaded meaning that
              // it is know to exist in the DB
              deleteRecurse(removedBean, t);
            }
          }
        }
        t.depth(-1);
      }
    }
  }
View Full Code Here

Examples of com.avaje.ebeaninternal.api.SpiTransaction

      // collect the Id's (to exclude from deleteManyDetails)
      detailIds = new ArrayList<Object>();
    }

    // increase depth for batching order
    SpiTransaction t = saveMany.getTransaction();
    t.depth(+1);

    // if a map, then we get the key value and
    // set it to the appropriate property on the
    // detail bean before we save it
    boolean isMap = ManyType.JAVA_MAP.equals(prop.getManyType());
    EntityBean parentBean = (EntityBean)saveMany.getParentBean();
    Object mapKeyValue = null;

    boolean saveSkippable = prop.isSaveRecurseSkippable();
    boolean skipSavingThisBean = false;

    for (Object detailBean : collection) {
      if (isMap) {
        // its a map so need the key and value
        Map.Entry<?, ?> entry = (Map.Entry<?, ?>) detailBean;
        mapKeyValue = entry.getKey();
        detailBean = entry.getValue();
      }

      if (detailBean instanceof EntityBean == false) {
        skipSavingThisBean = true;
        logger.debug("Skip non entity bean");
     
      } else {
        EntityBean detail = (EntityBean)detailBean;
        EntityBeanIntercept ebi = detail._ebean_getIntercept();
        if (prop.isManyToMany()) {
          skipSavingThisBean = targetDescriptor.isReference(ebi);
        } else {
          if (targetDescriptor.isReference(ebi)) {
            // we can skip this one
            skipSavingThisBean = true;

          } else if (ebi.isNewOrDirty()) {
            skipSavingThisBean = false;
            // set the parent bean to detailBean
            prop.setJoinValuesToChild(parentBean, detail, mapKeyValue);           

          } else {
            // unmodified so skip depending on prop.isSaveRecurseSkippable();
            skipSavingThisBean = saveSkippable;
          }
        }

        if (skipSavingThisBean) {
          // unmodified bean that does not recurse its save
          // so we can skip the save for this bean.
          // Reset skipSavingThisBean for the next detailBean
          skipSavingThisBean = false;

        } else {
          // normal save recurse
          saveRecurse(detail, t, parentBean, insertMode);
        }
        if (detailIds != null) {
          // remember the Id (other details not in the collection) will be removed
          Object id = targetDescriptor.getId(detail);
          if (!DmlUtil.isNullOrZero(id)) {
            detailIds.add(id);
          }
        }
      }
    }

    if (detailIds != null) {
      // deleteMissingChildren is true so deleting children that were not just processed
      deleteManyDetails(t, prop.getBeanDescriptor(), parentBean, prop, detailIds);
    }

    t.depth(-1);
  }
View Full Code Here

Examples of com.avaje.ebeaninternal.api.SpiTransaction

  }

  public void saveAssociation(EntityBean parentBean, String propertyName, Transaction t) {

    BeanDescriptor<?> descriptor = beanDescriptorManager.getBeanDescriptor(parentBean.getClass());
    SpiTransaction trans = (SpiTransaction) t;

    BeanProperty prop = descriptor.getBeanProperty(propertyName);
    if (prop == null) {
      String msg = "Could not find property [" + propertyName + "] on bean " + parentBean.getClass();
      throw new PersistenceException(msg);
    }

    if (prop instanceof BeanPropertyAssocMany<?>) {
      BeanPropertyAssocMany<?> manyProp = (BeanPropertyAssocMany<?>) prop;
      saveMany(new SaveManyPropRequest(manyProp, parentBean, (SpiTransaction) t), true);

    } else if (prop instanceof BeanPropertyAssocOne<?>) {
      BeanPropertyAssocOne<?> oneProp = (BeanPropertyAssocOne<?>) prop;
      EntityBean assocBean = oneProp.getValueAsEntityBean(parentBean);

      int depth = oneProp.isOneToOneExported() ? 1 : -1;
      int revertDepth = -1 * depth;

      trans.depth(depth);
      saveRecurse(assocBean, t, parentBean, true);
      trans.depth(revertDepth);

    } else {
      String msg = "Expecting [" + prop.getFullBeanName() + "] to be a OneToMany, OneToOne, ManyToOne or ManyToMany property?";
      throw new PersistenceException(msg);
    }
View Full Code Here

Examples of com.avaje.ebeaninternal.api.SpiTransaction

    Object value = prop.getValue(saveManyPropRequest.getParentBean());
    if (value == null) {
      return;
    }

    SpiTransaction t = saveManyPropRequest.getTransaction();
    Collection<?> additions = null;
    Collection<?> deletions = null;

    boolean vanillaCollection = (value instanceof BeanCollection<?> == false);

    if (vanillaCollection || deleteMissingChildren) {
      // delete all intersection rows and then treat all
      // beans in the collection as additions
      deleteAssocManyIntersection(saveManyPropRequest.getParentBean(), prop, t);
    }

    if (saveManyPropRequest.isInsertedParent() || vanillaCollection || deleteMissingChildren) {
      // treat everything in the list/set/map as an intersection addition
      if (value instanceof Map<?, ?>) {
        additions = ((Map<?, ?>) value).values();
      } else if (value instanceof Collection<?>) {
        additions = (Collection<?>) value;
      } else {
        String msg = "Unhandled ManyToMany type " + value.getClass().getName() + " for " + prop.getFullBeanName();
        throw new PersistenceException(msg);
      }
      if (!vanillaCollection) {
        ((BeanCollection<?>) value).modifyReset();
      }
    } else {
      // BeanCollection so get the additions/deletions
      BeanCollection<?> manyValue = (BeanCollection<?>) value;
      additions = manyValue.getModifyAdditions();
      deletions = manyValue.getModifyRemovals();
      // reset so the changes are only processed once
      manyValue.modifyReset();
    }

    t.depth(+1);

    if (additions != null && !additions.isEmpty()) {
      for (Object other : additions) {
        EntityBean otherBean = (EntityBean)other;
        // the object from the 'other' side of the ManyToMany
        if (deletions != null && deletions.remove(otherBean)) {
          String m = "Inserting and Deleting same object? " + otherBean;
          if (t.isLogSummary()) {
            t.logSummary(m);
          }
          logger.warn(m);

        } else {
          if (!prop.hasImportedId(otherBean)) {
            String msg = "ManyToMany bean " + otherBean + " does not have an Id value.";
            throw new PersistenceException(msg);

          } else {
            // build a intersection row for 'insert'
            IntersectionRow intRow = prop.buildManyToManyMapBean(saveManyPropRequest.getParentBean(), otherBean);
            SqlUpdate sqlInsert = intRow.createInsert(server);
            executeSqlUpdate(sqlInsert, t);
          }
        }
      }
    }
    if (deletions != null && !deletions.isEmpty()) {
      for (Object other : deletions) {
        EntityBean otherDelete = (EntityBean)other;
        // the object from the 'other' side of the ManyToMany
        // build a intersection row for 'delete'
        IntersectionRow intRow = prop.buildManyToManyMapBean(saveManyPropRequest.getParentBean(), otherDelete);
        SqlUpdate sqlDelete = intRow.createDelete(server);
        executeSqlUpdate(sqlDelete, t);
      }
    }

    // decrease the depth back to what it was
    t.depth(-1);
  }
View Full Code Here

Examples of com.avaje.ebeaninternal.api.SpiTransaction

   * This is called prior to deleting the parent bean.
   * </p>
   */
  private void deleteAssocMany(PersistRequestBean<?> request) {

    SpiTransaction t = request.getTransaction();
    t.depth(-1);

    BeanDescriptor<?> desc = request.getBeanDescriptor();
    EntityBean parentBean = request.getEntityBean();

    BeanPropertyAssocOne<?>[] expOnes = desc.propertiesOneExportedDelete();
    if (expOnes.length > 0) {

      DeleteUnloadedForeignKeys unloaded = null;
      for (int i = 0; i < expOnes.length; i++) {
        BeanPropertyAssocOne<?> prop = expOnes[i];
        if (request.isLoadedProperty(prop)) {
          Object detailBean = prop.getValue(parentBean);
          if (detailBean != null) {
            deleteRecurse(detailBean, t);
          }
        } else {
          if (unloaded == null) {
            unloaded = new DeleteUnloadedForeignKeys(server, request);
          }
          unloaded.add(prop);
        }
      }
      if (unloaded != null) {
        unloaded.queryForeignKeys();
        unloaded.deleteCascade();
      }
    }

    // Many's with delete cascade
    BeanPropertyAssocMany<?>[] manys = desc.propertiesManyDelete();
    for (int i = 0; i < manys.length; i++) {
      if (manys[i].isManyToMany()) {
        // delete associated rows from intersection table
        deleteAssocManyIntersection(parentBean, manys[i], t);

      } else {

        if (ModifyListenMode.REMOVALS.equals(manys[i].getModifyListenMode())) {
          // PrivateOwned ...
          Object details = manys[i].getValue(parentBean);
          if (details instanceof BeanCollection<?>) {
            Set<?> modifyRemovals = ((BeanCollection<?>) details).getModifyRemovals();
            if (modifyRemovals != null && !modifyRemovals.isEmpty()) {

              // delete the orphans that have been removed from the collection
              for (Object detail : modifyRemovals) {
                EntityBean detailBean = (EntityBean)detail;
                if (manys[i].hasId(detailBean)) {
                  deleteRecurse(detailBean, t);
                }
              }
            }
          }
        }

        deleteManyDetails(t, desc, parentBean, manys[i], null);
      }
    }

    // restore the depth
    t.depth(+1);
  }
View Full Code Here

Examples of com.avaje.ebeaninternal.api.SpiTransaction

            // skip saving the parent as already saved
          } else if (prop.isSaveRecurseSkippable(detailBean)) {
            // we can skip saving this bean

          } else {
            SpiTransaction t = request.getTransaction();
            t.depth(-1);
            saveRecurse(detailBean, t, null, insertMode);
            t.depth(+1);
          }
        }
      }
    }
  }
View Full Code Here

Examples of com.avaje.ebeaninternal.api.SpiTransaction

   * Note that the transaction is stored in a ThreadLocal variable.
   * </p>
   */
  public Transaction beginTransaction() {
    // start an explicit transaction
    SpiTransaction t = transactionManager.createTransaction(true, -1);
    transactionScopeManager.set(t);
    return t;
  }
View Full Code Here

Examples of com.avaje.ebeaninternal.api.SpiTransaction

   * Note that the transaction is stored in a ThreadLocal variable.
   * </p>
   */
  public Transaction beginTransaction(TxIsolation isolation) {
    // start an explicit transaction
    SpiTransaction t = transactionManager.createTransaction(true, isolation.getLevel());
    transactionScopeManager.set(t);
    return t;
  }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.