Example #1
0
  /**
   * 1. record the collection role that this collection is referenced by 2. decide if the collection
   * needs deleting/creating/updating (but don't actually schedule the action yet)
   */
  @SuppressWarnings({"JavaDoc"})
  private static void prepareCollectionForUpdate(
      PersistentCollection collection, CollectionEntry entry, SessionFactoryImplementor factory) {

    if (entry.isProcessed()) {
      throw new AssertionFailure("collection was processed twice by flush()");
    }
    entry.setProcessed(true);

    final CollectionPersister loadedPersister = entry.getLoadedPersister();
    final CollectionPersister currentPersister = entry.getCurrentPersister();
    if (loadedPersister != null
        || currentPersister != null) { // it is or was referenced _somewhere_

      boolean ownerChanged =
          loadedPersister != currentPersister
              || // if either its role changed,
              !currentPersister
                  .getKeyType()
                  .isEqual( // or its key changed
                      entry.getLoadedKey(), entry.getCurrentKey(), factory);

      if (ownerChanged) {

        // do a check
        final boolean orphanDeleteAndRoleChanged =
            loadedPersister != null
                && currentPersister != null
                && loadedPersister.hasOrphanDelete();

        if (orphanDeleteAndRoleChanged) {
          throw new HibernateException(
              "Don't change the reference to a collection with cascade=\"all-delete-orphan\": "
                  + loadedPersister.getRole());
        }

        // do the work
        if (currentPersister != null) {
          entry.setDorecreate(true); // we will need to create new entries
        }

        if (loadedPersister != null) {
          entry.setDoremove(true); // we will need to remove ye olde entries
          if (entry.isDorecreate()) {
            LOG.trace("Forcing collection initialization");
            collection.forceInitialization();
          }
        }
      } else if (collection.isDirty()) {
        // the collection's elements have changed
        entry.setDoupdate(true);
      }
    }
  }