Example usage for org.hibernate.proxy HibernateProxy getHibernateLazyInitializer

List of usage examples for org.hibernate.proxy HibernateProxy getHibernateLazyInitializer

Introduction

In this page you can find the example usage for org.hibernate.proxy HibernateProxy getHibernateLazyInitializer.

Prototype

public LazyInitializer getHibernateLazyInitializer();

Source Link

Document

Get the underlying lazy initialization handler.

Usage

From source file:org.jspresso.framework.application.backend.persistence.mongo.JspressoMongoEventListener.java

License:Open Source License

/**
 * Gets entity./*from   ww  w .  jav a  2 s.c o m*/
 *
 * @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;
}

From source file:org.libreplan.business.hibernate.notification.HibernateDatabaseModificationsListener.java

License:Open Source License

private static Class<?> inferEntityClass(Object entity) {
    if (entity instanceof HibernateProxy) {
        HibernateProxy proxy = (HibernateProxy) entity;

        return proxy.getHibernateLazyInitializer().getPersistentClass();
    }//from w w  w.ja va  2s .  co  m

    return entity.getClass();
}

From source file:org.libreplan.business.util.deepcopy.DeepCopy.java

License:Open Source License

private <T> T desproxify(T value) {
    if (value instanceof HibernateProxy) {
        HibernateProxy proxy = (HibernateProxy) value;
        return (T) proxy.getHibernateLazyInitializer().getImplementation();
    }//from  ww w .  ja v a 2  s . c  om
    return value;
}

From source file:org.malaguna.cmdit.service.reflection.HibernateProxyUtils.java

License:Open Source License

public Object unproxy(Object object) {
    Object result = object;//from   w  w  w  . ja v a  2s. c om

    if (object != null && isProxy(object)) {
        try {
            Hibernate.initialize(object);
            HibernateProxy proxy = (HibernateProxy) object;
            result = proxy.getHibernateLazyInitializer().getImplementation();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    return result;
}

From source file:org.mifos.framework.components.audit.util.helpers.InterceptHelper.java

License:Open Source License

private Class getClazz(Object obj) {
    try {//  w  w  w . j a v  a 2s  .com
        HibernateProxy hibernateProxy = (HibernateProxy) obj;
        LazyInitializer lazyInitializer = hibernateProxy.getHibernateLazyInitializer();
        return lazyInitializer.getPersistentClass();
    } catch (ClassCastException e) {
        return obj.getClass();
    }
}

From source file:org.openmrs.module.sync.api.db.hibernate.HibernateSyncInterceptor.java

License:Open Source License

/**
 * Retrieves uuid of OpenmrsObject instance from the storage based on identity value (i.e. PK).
 * <p>//from  w ww . j a  va  2 s  .c  om
 * Remarks: It is important for the implementation to avoid loading obj into session while
 * trying to determine its uuid. As a result, the implementation uses the combination of
 * reflection to determine the object's identifier value and Hibernate criteria in order to
 * build select statement for getting the uuid. The reason to avoid fetching the obj is because
 * doing it causes an error in hibernate when processing disconnected proxies. Specifically,
 * during obs edit, several properties are are disconnected as the form controller uses Criteria
 * object to construct select queury session.clear() and then session.merge(). Finally,
 * implementation suspends any state flushing to avoid any weird auto-flush events being
 * triggered while select is being executed.
 *
 * @param obj Instance of OpenmrsObject for which to retrieve uuid for.
 * @return uuid from storage if obj identity value is set, else null.
 * @see ForeignKeys
 */
protected String fetchUuid(OpenmrsObject obj) {

    if (obj == null) {
        return null;
    }

    if (log.isDebugEnabled()) {
        log.debug("Attempting to fetch uuid for from OpenmrsObject");
    }

    try {
        return obj.getUuid();
    } catch (Exception e) {
        log.debug("Unable to get uuid from OpenmrsObject directly", e);
    }

    try {
        if (obj instanceof HibernateProxy) {
            log.debug("Attempting to retrieve via the Hibernate Proxy class and identifier");
            HibernateProxy proxy = (HibernateProxy) obj;
            Class persistentClass = proxy.getHibernateLazyInitializer().getPersistentClass();
            Object identifier = proxy.getHibernateLazyInitializer().getIdentifier();
            String uuid = fetchUuid(persistentClass, identifier);
            log.debug("Successfully retrieved uuid " + uuid);
            return uuid;
        }
    } catch (Exception e) {
        log.debug("Unable to fetch uuid from Hibernate Proxy: ", e);
    }

    try {
        log.debug("Attempting to load from the database given class and id");
        String uuid = fetchUuid(obj.getClass(), obj.getId());
        log.debug("Successfully retrieved uuid " + uuid);
        return uuid;
    } catch (Exception e) {
        log.debug("Unable to fetch uuid from class and id", e);
    }

    try {
        log.debug(
                "Attempting to load from the database given class only, using hibernate mapping to determine identifier");
        ClassMetadata data = getSessionFactory().getClassMetadata(obj.getClass());
        if (data != null) {
            String idPropertyName = data.getIdentifierPropertyName();
            if (idPropertyName != null) {
                Method m = SyncUtil.getGetterMethod(obj.getClass(), idPropertyName);
                if (m != null) {
                    Object idPropertyValue = m.invoke(obj);
                    String uuid = fetchUuid(obj.getClass(), idPropertyValue);
                    log.debug("Successfully retrieved uuid " + uuid);
                    return uuid;
                }
            }
        }
    } catch (Exception e) {
        log.debug("Unable to fetch uuid from reflection via hibernate metadata", e);
    }

    log.warn("*** All attempts failed to fetch the uuid for an OpenmrsObject ***");

    return null;
}

From source file:org.openmrs.module.sync.SyncUtil.java

License:Open Source License

public static String formatObject(Object object) {
    if (object != null) {
        try {//from   w  ww  .  java  2 s .  com
            if (object instanceof HibernateProxy) {
                HibernateProxy proxy = (HibernateProxy) object;
                Class persistentClass = proxy.getHibernateLazyInitializer().getPersistentClass();
                Object identifier = proxy.getHibernateLazyInitializer().getIdentifier();
                return persistentClass.getSimpleName() + "#" + identifier;
            }
            if (object instanceof OpenmrsObject) {
                OpenmrsObject o = (OpenmrsObject) object;
                return object.getClass().getSimpleName() + (o.getId() == null ? "" : "#" + o.getId());
            }
            if (object instanceof Collection) {
                Collection c = (Collection) object;
                StringBuilder sb = new StringBuilder();
                for (Object o : c) {
                    sb.append(sb.length() == 0 ? "" : ",").append(formatObject(o));
                }
                return c.getClass().getSimpleName() + "[" + sb + "]";
            }
        } catch (Exception e) {
        }
        return object.getClass().getSimpleName();
    }
    return "";
}

From source file:org.opensingular.lib.support.persistence.entity.BaseEntity.java

License:Apache License

public static final <T> T getOriginal(T obj) {
    if (obj instanceof HibernateProxy) {
        HibernateProxy proxy = (HibernateProxy) obj;
        LazyInitializer li = proxy.getHibernateLazyInitializer();
        return (T) li.getImplementation();
    }// ww  w.  j av  a 2  s .c o  m
    return obj;
}

From source file:org.openvpms.component.business.dao.hibernate.im.common.DOState.java

License:Open Source License

/**
 * Returns a key for an object, to be used in sets and maps, to avoid
 * loading objects from the database./* w ww  .j av  a  2s  .  c om*/
 * <p/>
 * If the object is loaded, its link identifier will be used, otherwise
 * the concatenation of its persistent class name and id will be used.
 *
 * @param object the object
 * @return a unique identifier for the object
 */
private String getKey(IMObjectDO object) {
    String id = null;
    if (object instanceof HibernateProxy) {
        HibernateProxy proxy = (HibernateProxy) object;
        LazyInitializer init = proxy.getHibernateLazyInitializer();
        if (init.isUninitialized()) {
            id = init.getPersistentClass().getName() + "#" + init.getIdentifier().toString();
        }
    }
    if (id == null) {
        id = object.getLinkId();
    }
    return id;
}

From source file:org.openvpms.component.business.dao.hibernate.im.common.HibernateHelper.java

License:Open Source License

/**
 * Helper to deproxy an object if required.
 *
 * @param object the potentially proxied object
 * @return the deproxied object, or <tt>object</tt> if it wasn't proxied
 *//* w  w w.j  av a2s.c  o m*/
public static Object deproxy(Object object) {
    if (object instanceof HibernateProxy) {
        HibernateProxy proxy = ((HibernateProxy) object);
        LazyInitializer init = proxy.getHibernateLazyInitializer();
        object = init.getImplementation();
    }
    return object;
}