@SuppressWarnings("unchecked")
 @Override
 public void modifying(ScalarEvent e) {
   FrozenLazyInitializerImpl declaring = FrozenLazyInitializerImpl.this;
   if (!declaring.disableTargetListener) {
     ObjectModelFactory<ObjectModel> omFactory = declaring.omFactory;
     LazyInitializer rawLazyInitializer = declaring.lazyInitializer;
     EventAttributeContext localAttributeContext = e.getAttributeContext(AttributeScope.LOCAL);
     if (rawLazyInitializer.isUninitialized()
         || omFactory.get(rawLazyInitializer.getImplementation()) != e.getSource()) {
       ((ObjectModel) e.getSource()).removeScalarListener(this);
       return;
     }
     localAttributeContext.addAttribute(AK_MODIFIYING_EXECUTED, true);
     if (e.getProperty() == declaring.objectModelMetadata.getEntityIdProperty()) {
       FrozenContext<Object> ctx = (FrozenContext<Object>) declaring.idFrozenContext;
       localAttributeContext.addAttribute(AK_FROZENCONTEXT, ctx);
       FrozenContext.suspendFreezing(ctx, declaring.owner);
     }
     ScalarListener scalarListener = declaring.scalarListener;
     if (scalarListener != null) {
       declaring.executeModifying(e.dispatch(declaring.omFactory.get(declaring.owner)));
     }
   }
 }
예제 #2
0
  /**
   * Pega o objeto real que esta sendo utilizado. Isto é necessário pois o hibernate empacota alguns
   * objetos em proxies.
   *
   * @param example o exemplo do bean que será utilizado na conversão
   * @return o objeto real que esta sendo utilizado
   */
  public static Object getBean(Object example) {
    if (example instanceof HibernateProxy) {
      HibernateProxy proxy = (HibernateProxy) example;
      LazyInitializer initializer = proxy.getHibernateLazyInitializer();

      // este código é soh para garantir que não vai dar um lazy initialization.
      SessionImplementor implementor = initializer.getSession();
      if (initializer.isUninitialized()) {
        try {
          // getImplementation is going to want to talk to a session
          if (implementor.isClosed()) {
            // Give up and return example.getClass();
            return example;
          }
        } catch (NoSuchMethodError ex) {
          // We must be using Hibernate 3.0/3.1 which doesn't have
          // this method
        }
      }

      return initializer.getImplementation();
    } else {
      return example;
    }
  }
예제 #3
0
  private EntityPersister guessEntityPersister(Object object) {
    if (scope == null) {
      return null;
    }

    String entityName = null;

    // this code is largely copied from Session's bestGuessEntityName
    Object entity = object;
    if (entity instanceof HibernateProxy) {
      final LazyInitializer initializer = ((HibernateProxy) entity).getHibernateLazyInitializer();
      if (initializer.isUninitialized()) {
        entityName = initializer.getEntityName();
      }
      entity = initializer.getImplementation();
    }

    if (entityName == null) {
      for (EntityNameResolver resolver : scope.resolveFactory().iterateEntityNameResolvers()) {
        entityName = resolver.resolveEntityName(entity);
        if (entityName != null) {
          break;
        }
      }
    }

    if (entityName == null) {
      // the old-time stand-by...
      entityName = object.getClass().getName();
    }

    return scope.resolveFactory().getEntityPersister(entityName);
  }
  /**
   * Get the entity instance underlying the given proxy, throwing an exception if the proxy is
   * uninitialized. If the given object is not a proxy, simply return the argument.
   */
  public Object unproxy(Object maybeProxy) throws HibernateException {
    if (maybeProxy instanceof ElementWrapper) {
      maybeProxy = ((ElementWrapper) maybeProxy).getElement();
    }

    if (maybeProxy instanceof HibernateProxy) {
      HibernateProxy proxy = (HibernateProxy) maybeProxy;
      LazyInitializer li = proxy.getHibernateLazyInitializer();
      if (li.isUninitialized()) {
        throw new PersistentObjectException(
            "object was an uninitialized proxy for " + li.getEntityName());
      }
      return li.getImplementation(); // unwrap the object
    } else {
      return maybeProxy;
    }
  }
 @Override
 public void setImplementation(Object target) {
   LazyInitializer rawLazyInitializer = this.lazyInitializer;
   Object oldTarget =
       rawLazyInitializer.isUninitialized() ? null : rawLazyInitializer.getImplementation();
   if (target != oldTarget) {
     ScalarListener listener = this.new TargetScalarListener();
     if (oldTarget != null) {
       this.omFactory.get(oldTarget).removeScalarListener(listener);
     }
     rawLazyInitializer.setImplementation(target);
     this.oldTarget = target;
     if (target != null) {
       ObjectModel targetOM = this.omFactory.get(target);
       targetOM.removeScalarListener(listener); // remove the duplicated listener.
       targetOM.addScalarListener(listener);
     }
   }
 }
예제 #6
0
  /**
   * Check if the property is initialized. If the named property does not exist or is not
   * persistent, this method always returns <tt>true</tt>.
   *
   * @param proxy The potential proxy
   * @param propertyName the name of a persistent attribute of the object
   * @return true if the named property of the object is not listed as uninitialized; false
   *     otherwise
   */
  public static boolean isPropertyInitialized(Object proxy, String propertyName) {

    Object entity;
    if (proxy instanceof HibernateProxy) {
      LazyInitializer li = ((HibernateProxy) proxy).getHibernateLazyInitializer();
      if (li.isUninitialized()) {
        return false;
      } else {
        entity = li.getImplementation();
      }
    } else {
      entity = proxy;
    }

    if (FieldInterceptionHelper.isInstrumented(entity)) {
      FieldInterceptor interceptor = FieldInterceptionHelper.extractFieldInterceptor(entity);
      return interceptor == null || interceptor.isInitialized(propertyName);
    } else {
      return true;
    }
  }
예제 #7
0
  protected Class<?> getClass(ExtendedObjectOutput out, Object v) {
    Class<?> cls = v.getClass();

    if (v instanceof HibernateProxy) {
      LazyInitializer initializer = ((HibernateProxy) v).getHibernateLazyInitializer();

      String className =
          (initializer.isUninitialized()
              ? initializer.getEntityName()
              : initializer.getImplementation().getClass().getName());

      if (className != null && className.length() > 0) {
        try {
          cls = out.getReflection().loadClass(className);
        } catch (ClassNotFoundException e) {
          cls = initializer.getPersistentClass();
        }
      }
    }

    return cls;
  }
 @SuppressWarnings("unchecked")
 private void initTransient(Class<?> persistentClass) {
   LazyInitializer rawLazyInitializer = this.lazyInitializer;
   if (persistentClass == null) {
     persistentClass = getPersistentClass((BasicLazyInitializer) rawLazyInitializer);
   }
   HibernateObjectModelMetadata objectModelMetadata = HibernateMetadatas.of(persistentClass);
   this.objectModelMetadata = objectModelMetadata;
   this.omFactory =
       (ObjectModelFactory<ObjectModel>)
           ObjectModelFactoryFactory.factoryOf(objectModelMetadata.getObjectModelClass());
   try {
     this.getIdentifierMethod = (Method) GET_IDENTIFIER_METHOD_FIELD.get(rawLazyInitializer);
     this.setIdentifierMethod = (Method) SET_IDENTIFIER_METHOD_FIELD.get(rawLazyInitializer);
   } catch (IllegalArgumentException | IllegalAccessException e) {
     throw new AssertionError();
   }
   if (!rawLazyInitializer.isUninitialized()) {
     ObjectModel targetOM = this.omFactory.get(rawLazyInitializer.getImplementation());
     ScalarListener listener = this.new TargetScalarListener();
     targetOM.removeScalarListener(listener);
     targetOM.addScalarListener(listener);
   }
 }