Example usage for org.hibernate.persister.entity EntityPersister createProxy

List of usage examples for org.hibernate.persister.entity EntityPersister createProxy

Introduction

In this page you can find the example usage for org.hibernate.persister.entity EntityPersister createProxy.

Prototype

Object createProxy(Serializable id, SharedSessionContractImplementor session) throws HibernateException;

Source Link

Document

Create a new proxy instance

Usage

From source file:com.autobizlogic.abl.data.hibernate.HibPersistentBeanCopy.java

License:Open Source License

/**
 * Create from a state array./*from w  ww .j  a  va2 s .  co  m*/
 * @param The state (typically from a Hibernate event)
 * @param pk The primary key
 * @param persister The persister for the object
 */
@SuppressWarnings("unchecked")
protected HibPersistentBeanCopy(Object[] state, Object entity, Serializable pk, EntityPersister persister,
        Session session) {
    if (entity instanceof Map)
        map = (Map<String, Object>) entity;
    else {
        bean = entity;
        beanMap = new BeanMap(entity);
    }
    this.pk = pk;
    this.metaEntity = MetaModelFactory.getHibernateMetaModel(persister.getFactory())
            .getMetaEntity(persister.getEntityName());

    ClassMetadata metadata = persister.getClassMetadata();
    String[] propNames = metadata.getPropertyNames();
    for (int i = 0; i < propNames.length; i++) {
        String propName = propNames[i];
        if (metaEntity.getMetaProperty(propName).isCollection())
            continue;

        MetaRole metaRole = metaEntity.getMetaRole(propName);
        if (metaRole == null) { // Not a relationship -- must be an attribute
            if (state[i] != null)
                values.put(propName, state[i]);
        } else if (!metaRole.isCollection()) {
            // In the case of old values, when we are handed the state, it contains the pk for associations,
            // and not (as you'd expect) the object itself. So we check whether the value is a real object,
            // and if it's not, we grab it from the object.
            if (state[i] == null || session.contains(state[i])) {
                values.put(propName, state[i]);
            } else {
                // We have a pk instead of a proxy -- ask Hibernate to create a proxy for it.
                String className = metadata.getPropertyType(propName).getReturnedClass().getName();
                PersistenceContext persContext = HibernateSessionUtil.getPersistenceContextForSession(session);
                EntityPersister entityPersister = HibernateSessionUtil.getEntityPersister(session, className,
                        bean);
                EntityKey entityKey = new EntityKey((Serializable) state[i], entityPersister,
                        session.getEntityMode());

                // Has a proxy already been created for this session?
                Object proxy = persContext.getProxy(entityKey);
                if (proxy == null) {
                    // There is no proxy anywhere for this object, so ask Hibernate to create one for us
                    proxy = entityPersister.createProxy((Serializable) state[i], (SessionImplementor) session);
                    persContext.getBatchFetchQueue().addBatchLoadableEntityKey(entityKey);
                    persContext.addProxy(entityKey, proxy);
                }
                values.put(propName, proxy);
            }
        }
    }
}