Java Code Examples for org.hibernate.proxy.HibernateProxy.getHibernateLazyInitializer()

The following are Jave code examples for showing how to use getHibernateLazyInitializer() of the org.hibernate.proxy.HibernateProxy class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: lams   File: StatefulPersistenceContext.java   Source Code and License Vote up 7 votes
@Override
public Object unproxyAndReassociate(Object maybeProxy) throws HibernateException {
	if ( maybeProxy instanceof ElementWrapper ) {
		maybeProxy = ( (ElementWrapper) maybeProxy ).getElement();
	}

	if ( maybeProxy instanceof HibernateProxy ) {
		final HibernateProxy proxy = (HibernateProxy) maybeProxy;
		final LazyInitializer li = proxy.getHibernateLazyInitializer();
		reassociateProxy( li, proxy );
		//initialize + unwrap the object and return it
		return li.getImplementation();
	}
	else {
		return maybeProxy;
	}
}
 
Example 2
Project: lams   File: StatefulPersistenceContext.java   Source Code and License Vote up 6 votes
@Override
public boolean reassociateIfUninitializedProxy(Object value) throws MappingException {
	if ( value instanceof ElementWrapper ) {
		value = ( (ElementWrapper) value ).getElement();
	}

	if ( !Hibernate.isInitialized( value ) ) {
		final HibernateProxy proxy = (HibernateProxy) value;
		final LazyInitializer li = proxy.getHibernateLazyInitializer();
		reassociateProxy( li, proxy );
		return true;
	}
	else {
		return false;
	}
}
 
Example 3
Project: lams   File: StatefulPersistenceContext.java   Source Code and License Vote up 6 votes
@Override
public Object unproxy(Object maybeProxy) throws HibernateException {
	if ( maybeProxy instanceof ElementWrapper ) {
		maybeProxy = ( (ElementWrapper) maybeProxy ).getElement();
	}

	if ( maybeProxy instanceof HibernateProxy ) {
		final HibernateProxy proxy = (HibernateProxy) maybeProxy;
		final LazyInitializer li = proxy.getHibernateLazyInitializer();
		if ( li.isUninitialized() ) {
			throw new PersistentObjectException(
					"object was an uninitialized proxy for " + li.getEntityName()
			);
		}
		//unwrap the object and return
		return li.getImplementation();
	}
	else {
		return maybeProxy;
	}
}
 
Example 4
Project: cacheonix-core   File: StatefulPersistenceContext.java   Source Code and License Vote up 6 votes
/**
 * Takes the given object and, if it represents a proxy, reassociates it with this event source.
 *
 * @param value The possible proxy to be reassociated.
 * @return Whether the passed value represented an actual proxy which got initialized.
 * @throws MappingException
 */
public boolean reassociateIfUninitializedProxy(Object value) throws MappingException {
	if ( value instanceof ElementWrapper ) {
		value = ( (ElementWrapper) value ).getElement();
	}
	
	if ( !Hibernate.isInitialized(value) ) {
		HibernateProxy proxy = (HibernateProxy) value;
		LazyInitializer li = proxy.getHibernateLazyInitializer();
		reassociateProxy(li, proxy);
		return true;
	}
	else {
		return false;
	}
}
 
Example 5
Project: cacheonix-core   File: StatefulPersistenceContext.java   Source Code and License Vote up 6 votes
/**
 * 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;
	}
}
 
Example 6
Project: cacheonix-core   File: StatefulPersistenceContext.java   Source Code and License Vote up 6 votes
/**
 * Possibly unproxy the given reference and reassociate it with the current session.
 *
 * @param maybeProxy The reference to be unproxied if it currently represents a proxy.
 * @return The unproxied instance.
 * @throws HibernateException
 */
public Object unproxyAndReassociate(Object maybeProxy) throws HibernateException {
	if ( maybeProxy instanceof ElementWrapper ) {
		maybeProxy = ( (ElementWrapper) maybeProxy ).getElement();
	}
	
	if ( maybeProxy instanceof HibernateProxy ) {
		HibernateProxy proxy = (HibernateProxy) maybeProxy;
		LazyInitializer li = proxy.getHibernateLazyInitializer();
		reassociateProxy(li, proxy);
		return li.getImplementation(); //initialize + unwrap the object
	}
	else {
		return maybeProxy;
	}
}
 
Example 7
Project: jspresso-ce   File: JspressoMongoEventListener.java   Source Code and License Vote up 6 votes
/**
 * Gets entity.
 *
 * @param entityName
 *     the entity name
 * @param id
 *     the id
 * @return the entity
 */
@SuppressWarnings("unchecked")
protected IEntity getEntity(String entityName, Serializable id) {
  IEntity registeredEntity = null;
  try {
    if (getBackendController().isUnitOfWorkActive()) {
      registeredEntity = getBackendController().getUnitOfWorkEntity((Class<? extends IEntity>) Class.forName(
          entityName), id);
    } else {
      registeredEntity = getBackendController().getRegisteredEntity((Class<? extends IEntity>) Class.forName(
          entityName), id);
      if (registeredEntity instanceof HibernateProxy) {
        HibernateProxy proxy = (HibernateProxy) registeredEntity;
        LazyInitializer li = proxy.getHibernateLazyInitializer();
        registeredEntity = (IEntity) li.getImplementation();
      }
    }
  } catch (ClassNotFoundException ex) {
    LOG.error("Class for entity {} was not found", entityName, ex);
  }
  return registeredEntity;
}
 
Example 8
Project: breeze.server.java   File: HibernateProxyTypeAdapter.java   Source Code and License Vote up 6 votes
@SuppressWarnings({"rawtypes", "unchecked"})
  @Override
  public void write(JsonWriter out, HibernateProxy value) throws IOException {
      if (value == null) {
          out.nullValue();
          return;
      }

LazyInitializer initializer =  value.getHibernateLazyInitializer();
if (initializer.isUninitialized()) {
	out.nullValue();
	return;
} else {
       // Retrieve the original (not proxy) class
       Class<?> baseType = Hibernate.getClass(value);
       // Get the TypeAdapter of the original class, to delegate the serialization
       TypeAdapter delegate = context.getAdapter(TypeToken.get(baseType));
       // Get a filled instance of the original class
       Object unproxiedValue = initializer.getImplementation();
       // Serialize the value
       delegate.write(out, unproxiedValue);
}
  }
 
Example 9
Project: lams   File: StatefulPersistenceContext.java   Source Code and License Vote up 5 votes
@Override
public void reassociateProxy(Object value, Serializable id) throws MappingException {
	if ( value instanceof ElementWrapper ) {
		value = ( (ElementWrapper) value ).getElement();
	}

	if ( value instanceof HibernateProxy ) {
		LOG.debugf( "Setting proxy identifier: %s", id );
		final HibernateProxy proxy = (HibernateProxy) value;
		final LazyInitializer li = proxy.getHibernateLazyInitializer();
		li.setIdentifier( id );
		reassociateProxy( li, proxy );
	}
}
 
Example 10
Project: parabuild-ci   File: H3BeanConverter.java   Source Code and License Vote up 5 votes
/**
 * Hibernate makes {@link Class#getClass()} diffficult ...
 * @param example The class that we want to call {@link Class#getClass()} on
 * @return The type of the given object
 */
public Class getClass(Object example)
{
    if (example instanceof HibernateProxy)
    {
        HibernateProxy proxy = (HibernateProxy) example;
        LazyInitializer initializer = proxy.getHibernateLazyInitializer();
        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.getClass();
                }
            }
            catch (NoSuchMethodError ex)
            {
                // We must be using Hibernate 3.0/3.1 which doesn't have
                // this method
            }
        }

        return initializer.getImplementation().getClass();
    }
    else
    {
        return example.getClass();
    }
}
 
Example 11
Project: dwr   File: H3BeanConverter.java   Source Code and License Vote up 5 votes
/**
 * Hibernate makes {@link Class#getClass()} difficult ...
 * @param example The class that we want to call {@link Class#getClass()} on
 * @return The type of the given object
 */
public Class<?> getClass(Object example)
{
    if (example instanceof HibernateProxy)
    {
        HibernateProxy proxy = (HibernateProxy) example;
        LazyInitializer initializer = proxy.getHibernateLazyInitializer();
        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.getClass();
                }
            }
            catch (NoSuchMethodError ex)
            {
                // We must be using Hibernate 3.0/3.1 which doesn't have
                // this method
            }
        }

        return initializer.getImplementation().getClass();
    }
    else
    {
        return example.getClass();
    }
}
 
Example 12
Project: dwr   File: H4BeanConverter.java   Source Code and License Vote up 5 votes
/**
 * Hibernate makes {@link Class#getClass()} difficult ...
 * @param example The class that we want to call {@link Class#getClass()} on
 * @return The type of the given object
 */
public Class<?> getClass(Object example)
{
    if (example instanceof HibernateProxy)
    {
        HibernateProxy proxy = (HibernateProxy) example;
        LazyInitializer initializer = proxy.getHibernateLazyInitializer();
        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.getClass();
                }
            }
            catch (NoSuchMethodError ex)
            {
                // We must be using Hibernate 3.0/3.1 which doesn't have
                // this method
            }
        }

        return initializer.getImplementation().getClass();
    }
    else
    {
        return example.getClass();
    }
}
 
Example 13
Project: cacheonix-core   File: StatefulPersistenceContext.java   Source Code and License Vote up 5 votes
/**
 * If a deleted entity instance is re-saved, and it has a proxy, we need to
 * reset the identifier of the proxy 
 */
public void reassociateProxy(Object value, Serializable id) throws MappingException {
	if ( value instanceof ElementWrapper ) {
		value = ( (ElementWrapper) value ).getElement();
	}
	
	if ( value instanceof HibernateProxy ) {
		if ( log.isDebugEnabled() ) log.debug("setting proxy identifier: " + id);
		HibernateProxy proxy = (HibernateProxy) value;
		LazyInitializer li = proxy.getHibernateLazyInitializer();
		li.setIdentifier(id);
		reassociateProxy(li, proxy);
	}
}
 
Example 14
Project: cacheonix-core   File: EntityType.java   Source Code and License Vote up 5 votes
/**
 * Get the identifier value of an instance or proxy.
 * <p/>
 * Intended only for loggin purposes!!!
 *
 * @param object The object from which to extract the identifier.
 * @param persister The entity persister
 * @param entityMode The entity mode
 * @return The extracted identifier.
 */
private static Serializable getIdentifier(Object object, EntityPersister persister, EntityMode entityMode) {
	if (object instanceof HibernateProxy) {
		HibernateProxy proxy = (HibernateProxy) object;
		LazyInitializer li = proxy.getHibernateLazyInitializer();
		return li.getIdentifier();
	}
	else {
		return persister.getIdentifier( object, entityMode );
	}
}
 
Example 15
Project: eMonocot   File: HibernateProxySerializer.java   Source Code and License Vote up 5 votes
/**
 * Helper method for finding value being proxied, if it is available
 * or if it is to be forced to be loaded.
 */
protected Object findProxied(HibernateProxy proxy)
{
	LazyInitializer init = proxy.getHibernateLazyInitializer();
	if (!_forceLazyLoading && init.isUninitialized()) {
		return null;
	}
	return init.getImplementation();
}
 
Example 16
Project: blcdemo   File: HibernateUtils.java   Source Code and License Vote up 5 votes
/**
 * <p>Ensure a domain object is an actual persisted object and not a Hibernate proxy object by getting its real implementation.
 *
 * <p>This is primarily useful when retrieving a lazy loaded object that has been subclassed and you have the intention of casting it.
 *
 * @param t the domain object to deproxy
 * @return the actual persisted object or the passed in object if it is not a Hibernate proxy
 */
public static <T> T deproxy(T t) {
    if (t instanceof HibernateProxy) {
        HibernateProxy proxy = (HibernateProxy)t;
        LazyInitializer lazyInitializer = proxy.getHibernateLazyInitializer();
        return (T)lazyInitializer.getImplementation();
    }
    return t;
}
 
Example 17
Project: jspresso-ce   File: ControllerAwareEntityProxyInterceptor.java   Source Code and License Vote up 5 votes
/**
 * {@inheritDoc}
 */
@SuppressWarnings("unchecked")
@Override
public Object getEntity(String entityName, Serializable id) {
  IEntity registeredEntity = null;
  try {
    if (getBackendController().isUnitOfWorkActive()) {
      registeredEntity = getBackendController().getUnitOfWorkEntity((Class<? extends IEntity>) Class.forName(
          entityName), id);
    } else {
      registeredEntity = getBackendController().getRegisteredEntity((Class<? extends IEntity>) Class.forName(
          entityName), id);
      if (registeredEntity instanceof HibernateProxy) {
        HibernateProxy proxy = (HibernateProxy) registeredEntity;
        LazyInitializer li = proxy.getHibernateLazyInitializer();
        registeredEntity = (IEntity) li.getImplementation();
      }
    }
  } catch (ClassNotFoundException ex) {
    LOG.error("Class for entity {} was not found", entityName, ex);
  }
  // getEntity should never return transient instances, see #1244
  if (registeredEntity != null && !registeredEntity.isPersistent()) {
    registeredEntity = null;
  }
  ((HibernateBackendController) getBackendController()).detachFromHibernateInDepth(registeredEntity,
      ((HibernateBackendController) getBackendController()).getHibernateSession(), new HibernateEntityRegistry(
          "detachFromHibernateInDepth"));
  return registeredEntity;
}
 
Example 18
Project: beanlib   File: UnEnhancer.java   Source Code and License Vote up 5 votes
public static <T> T unenhanceObject(T object) {
    if (object instanceof HibernateProxy) {
        HibernateProxy hibernateProxy = (HibernateProxy) object;
        LazyInitializer lazyInitializer = hibernateProxy.getHibernateLazyInitializer();

        @SuppressWarnings("unchecked")
        T ret = (T) lazyInitializer.getImplementation();
        return ret;
    }
    return object;
}
 
Example 19
Project: parabuild-ci   File: H3PropertyDescriptorProperty.java   Source Code and License Vote up 4 votes
public Object getValue(Object bean) throws MarshallException
{
    if (!(bean instanceof HibernateProxy))
    {
        // This is not a hibernate dynamic proxy, just use it
        return super.getValue(bean);
    }
    else
    {
        // If the property is already initialized, use it
        boolean initialized = Hibernate.isPropertyInitialized(bean, descriptor.getName());
        if (initialized)
        {
            // This might be a lazy-collection so we need to double check
            Object reply = super.getValue(bean);
            initialized = Hibernate.isInitialized(reply);
        }

        if (initialized)
        {
            return super.getValue(bean);
        }
        else
        {
            // If the session bound to the property is live, use it
            HibernateProxy proxy = (HibernateProxy) bean;
            LazyInitializer initializer = proxy.getHibernateLazyInitializer();
            SessionImplementor implementor = initializer.getSession();
            if (implementor.isOpen())
            {
                return super.getValue(bean);
            }

            // So the property needs database access, and the session is closed
            // We'll need to try get another session
            ServletContext context = WebContextFactory.get().getServletContext();
            Session session = H3SessionAjaxFilter.getCurrentSession(context);

            if (session != null)
            {
                session.update(bean);
                return super.getValue(bean);
            }

            return null;
        }
    }
}
 
Example 20
Project: dwr   File: H3PropertyDescriptorProperty.java   Source Code and License Vote up 4 votes
@Override
public Object getValue(Object bean) throws ConversionException
{
    if (!(bean instanceof HibernateProxy))
    {
        // This is not a hibernate dynamic proxy, just use it
        return super.getValue(bean);
    }
    else
    {
        // If the property is already initialized, use it
        boolean initialized = Hibernate.isPropertyInitialized(bean, descriptor.getName());
        if (initialized)
        {
            // This might be a lazy-collection so we need to double check
            Object reply = super.getValue(bean);
            initialized = Hibernate.isInitialized(reply);
        }

        if (initialized)
        {
            return super.getValue(bean);
        }
        else
        {
            // If the session bound to the property is live, use it
            HibernateProxy proxy = (HibernateProxy) bean;
            LazyInitializer initializer = proxy.getHibernateLazyInitializer();
            SessionImplementor implementor = initializer.getSession();
            if (implementor.isOpen())
            {
                return super.getValue(bean);
            }

            // So the property needs database access, and the session is closed
            // We'll need to try get another session
            ServletContext context = WebContextFactory.get().getServletContext();
            Session session = H3SessionAjaxFilter.getCurrentSession(context);

            if (session != null)
            {
                session.update(bean);
                return super.getValue(bean);
            }

            return null;
        }
    }
}
 
Example 21
Project: dwr   File: H4PropertyDescriptorProperty.java   Source Code and License Vote up 4 votes
@Override
public Object getValue(Object bean) throws ConversionException
{
    if (!(bean instanceof HibernateProxy))
    {
        // This is not a hibernate dynamic proxy, just use it
        return super.getValue(bean);
    }
    else
    {
        // If the property is already initialized, use it
        boolean initialized = Hibernate.isPropertyInitialized(bean, descriptor.getName());
        if (initialized)
        {
            // This might be a lazy-collection so we need to double check
            Object reply = super.getValue(bean);
            initialized = Hibernate.isInitialized(reply);
        }

        if (initialized)
        {
            return super.getValue(bean);
        }
        else
        {
            // If the session bound to the property is live, use it
            HibernateProxy proxy = (HibernateProxy) bean;
            LazyInitializer initializer = proxy.getHibernateLazyInitializer();
            SessionImplementor implementor = initializer.getSession();
            if (implementor.isOpen())
            {
                return super.getValue(bean);
            }

            // So the property needs database access, and the session is closed
            // We'll need to try get another session
            ServletContext context = WebContextFactory.get().getServletContext();
            Session session = H4SessionAjaxFilter.getCurrentSession(context);

            if (session != null)
            {
                session.update(bean);
                return super.getValue(bean);
            }

            return null;
        }
    }
}