@SuppressWarnings("unchecked")
  @Override
  /**
   * Handle the given delete event. This is the cascaded form.
   *
   * @param event The delete event.
   * @param transientEntities The cache of entities already deleted
   * @throws HibernateException
   */
  public void onDelete(DeleteEvent event, Set transientEntities) throws HibernateException {
    final EventSource source = event.getSession();

    final PersistenceContext persistenceContext = source.getPersistenceContext();
    Object entity = persistenceContext.unproxyAndReassociate(event.getObject());
    EntityEntry entityEntry = persistenceContext.getEntry(entity);

    final EntityPersister persister = source.getEntityPersister(event.getEntityName(), entity);
    final Object version;

    if (persister.isVersioned()) {
      version = persister.getVersion(entity, source.getEntityMode());
      // Make sure version has not changed on deleted entities
      if ((entity instanceof TimelineEntity) && !((TimelineEntity) entity).isNew()) {
        if (!persister.getVersionType().isEqual(version, entityEntry.getVersion())) {
          throw new StaleObjectStateException(persister.getEntityName(), entityEntry.getId());
        }
      }
    }
    super.onDelete(event, transientEntities);
  }
Пример #2
0
  /**
   * Register the "hydrated" state of an entity instance, after the first step of 2-phase loading.
   *
   * <p>Add the "hydrated state" (an array) of an uninitialized entity to the session. We don't try
   * to resolve any associations yet, because there might be other entities waiting to be read from
   * the JDBC result set we are currently processing
   */
  public static void postHydrate(
      final EntityPersister persister,
      final Serializable id,
      final Object[] values,
      final Object rowId,
      final Object object,
      final LockMode lockMode,
      final boolean lazyPropertiesAreUnfetched,
      final SessionImplementor session)
      throws HibernateException {

    Object version = Versioning.getVersion(values, persister);
    session
        .getPersistenceContext()
        .addEntry(
            object,
            Status.LOADING,
            values,
            rowId,
            id,
            version,
            lockMode,
            true,
            persister,
            false,
            lazyPropertiesAreUnfetched);

    if (log.isTraceEnabled() && version != null) {
      String versionStr =
          persister.isVersioned()
              ? persister.getVersionType().toLoggableString(version, session.getFactory())
              : "null";
      log.trace("Version: " + versionStr);
    }
  }
  /** Convience method to retreive an entities next version value */
  private Object getNextVersion(FlushEntityEvent event) throws HibernateException {

    EntityEntry entry = event.getEntityEntry();
    EntityPersister persister = entry.getPersister();
    if (persister.isVersioned()) {

      Object[] values = event.getPropertyValues();

      if (entry.isBeingReplicated()) {
        return Versioning.getVersion(values, persister);
      } else {
        int[] dirtyProperties = event.getDirtyProperties();

        final boolean isVersionIncrementRequired =
            isVersionIncrementRequired(event, entry, persister, dirtyProperties);

        final Object nextVersion =
            isVersionIncrementRequired
                ? Versioning.increment(
                    entry.getVersion(), persister.getVersionType(), event.getSession())
                : entry.getVersion(); // use the current version

        Versioning.setVersion(values, nextVersion, persister);

        return nextVersion;
      }
    } else {
      return null;
    }
  }
  /**
   * Perform any property value substitution that is necessary (interceptor callback, version
   * initialization...)
   *
   * @param entity The entity
   * @param id The entity identifier
   * @param values The snapshot entity state
   * @param persister The entity persister
   * @param source The originating session
   * @return True if the snapshot state changed such that reinjection of the values into the entity
   *     is required.
   */
  protected boolean substituteValuesIfNecessary(
      Object entity,
      Serializable id,
      Object[] values,
      EntityPersister persister,
      SessionImplementor source) {
    boolean substitute =
        source
            .getInterceptor()
            .onSave(entity, id, values, persister.getPropertyNames(), persister.getPropertyTypes());

    // keep the existing version number in the case of replicate!
    if (persister.isVersioned()) {
      substitute =
          Versioning.seedVersion(
                  values, persister.getVersionProperty(), persister.getVersionType(), source)
              || substitute;
    }
    return substitute;
  }
Пример #5
0
  /**
   * Perform the second step of 2-phase load. Fully initialize the entity instance.
   *
   * <p>After processing a JDBC result set, we "resolve" all the associations between the entities
   * which were instantiated and had their state "hydrated" into an array
   */
  public static void initializeEntity(
      final Object entity,
      final boolean readOnly,
      final SessionImplementor session,
      final PreLoadEvent preLoadEvent,
      final PostLoadEvent postLoadEvent)
      throws HibernateException {

    // TODO: Should this be an InitializeEntityEventListener??? (watch out for performance!)

    final PersistenceContext persistenceContext = session.getPersistenceContext();
    EntityEntry entityEntry = persistenceContext.getEntry(entity);
    if (entityEntry == null) {
      throw new AssertionFailure("possible non-threadsafe access to the session");
    }
    EntityPersister persister = entityEntry.getPersister();
    Serializable id = entityEntry.getId();
    Object[] hydratedState = entityEntry.getLoadedState();

    if (log.isDebugEnabled())
      log.debug(
          "resolving associations for "
              + MessageHelper.infoString(persister, id, session.getFactory()));

    Type[] types = persister.getPropertyTypes();
    for (int i = 0; i < hydratedState.length; i++) {
      final Object value = hydratedState[i];
      if (value != LazyPropertyInitializer.UNFETCHED_PROPERTY
          && value != BackrefPropertyAccessor.UNKNOWN) {
        hydratedState[i] = types[i].resolve(value, session, entity);
      }
    }

    // Must occur after resolving identifiers!
    if (session.isEventSource()) {
      preLoadEvent.setEntity(entity).setState(hydratedState).setId(id).setPersister(persister);
      PreLoadEventListener[] listeners = session.getListeners().getPreLoadEventListeners();
      for (int i = 0; i < listeners.length; i++) {
        listeners[i].onPreLoad(preLoadEvent);
      }
    }

    persister.setPropertyValues(entity, hydratedState, session.getEntityMode());

    final SessionFactoryImplementor factory = session.getFactory();
    if (persister.hasCache() && session.getCacheMode().isPutEnabled()) {

      if (log.isDebugEnabled())
        log.debug(
            "adding entity to second-level cache: "
                + MessageHelper.infoString(persister, id, session.getFactory()));

      Object version = Versioning.getVersion(hydratedState, persister);
      CacheEntry entry =
          new CacheEntry(
              hydratedState,
              persister,
              entityEntry.isLoadedWithLazyPropertiesUnfetched(),
              version,
              session,
              entity);
      CacheKey cacheKey =
          new CacheKey(
              id,
              persister.getIdentifierType(),
              persister.getRootEntityName(),
              session.getEntityMode(),
              session.getFactory());
      boolean put =
          persister
              .getCache()
              .put(
                  cacheKey,
                  persister.getCacheEntryStructure().structure(entry),
                  session.getTimestamp(),
                  version,
                  persister.isVersioned() ? persister.getVersionType().getComparator() : null,
                  useMinimalPuts(
                      session,
                      entityEntry)); // we could use persister.hasLazyProperties() instead of true

      if (put && factory.getStatistics().isStatisticsEnabled()) {
        factory
            .getStatisticsImplementor()
            .secondLevelCachePut(persister.getCache().getRegionName());
      }
    }

    if (readOnly || !persister.isMutable()) {
      // no need to take a snapshot - this is a
      // performance optimization, but not really
      // important, except for entities with huge
      // mutable property values
      persistenceContext.setEntryStatus(entityEntry, Status.READ_ONLY);
    } else {
      // take a snapshot
      TypeFactory.deepCopy(
          hydratedState,
          persister.getPropertyTypes(),
          persister.getPropertyUpdateability(),
          hydratedState, // after setting values to object, entityMode
          session);
      persistenceContext.setEntryStatus(entityEntry, Status.MANAGED);
    }

    persister.afterInitialize(entity, entityEntry.isLoadedWithLazyPropertiesUnfetched(), session);

    if (session.isEventSource()) {
      postLoadEvent.setEntity(entity).setId(id).setPersister(persister);
      PostLoadEventListener[] listeners = session.getListeners().getPostLoadEventListeners();
      for (int i = 0; i < listeners.length; i++) {
        listeners[i].onPostLoad(postLoadEvent);
      }
    }

    if (log.isDebugEnabled())
      log.debug(
          "done materializing entity "
              + MessageHelper.infoString(persister, id, session.getFactory()));

    if (factory.getStatistics().isStatisticsEnabled()) {
      factory.getStatisticsImplementor().loadEntity(persister.getEntityName());
    }
  }