Example usage for org.hibernate.collection.spi PersistentCollection forceInitialization

List of usage examples for org.hibernate.collection.spi PersistentCollection forceInitialization

Introduction

In this page you can find the example usage for org.hibernate.collection.spi PersistentCollection forceInitialization.

Prototype

void forceInitialization();

Source Link

Document

To be called internally by the session, forcing immediate initialization.

Usage

From source file:ch.algotrader.dao.GenericDaoImpl.java

License:Open Source License

@Override
public Object getInitializedCollection(final String role, final long id) {

    return this.txTemplate.execute(txStatus -> {

        SessionFactoryImpl sessionFactoryImpl = (SessionFactoryImpl) this.sessionFactory;
        CollectionPersister persister = sessionFactoryImpl.getCollectionPersister(role);

        // load the owner entity
        ClassMetadata ownerMetadata = persister.getOwnerEntityPersister().getClassMetadata();
        Session session = this.sessionFactory.getCurrentSession();
        Object owner = session.get(ownerMetadata.getEntityName(), id);

        // owner does not exist anymore so no point in loading the collection
        if (owner == null) {
            return null;
        }/*ww w.j  a v a2  s  .c  om*/

        // get the collection by it's property name
        Object col = ownerMetadata.getPropertyValue(owner, persister.getNodeName());

        // if it is a PersistentCollection make sure it is initialized
        if (col instanceof PersistentCollection) {

            PersistentCollection collection = (PersistentCollection) col;
            if (!collection.wasInitialized()) {
                collection.forceInitialization();
            }
        }

        return col;
    });
}

From source file:de.micromata.genome.db.jpa.xmldump.impl.HibernateCollectionConverter.java

License:Apache License

/**
 * @see com.thoughtworks.xstream.converters.Converter#marshal(java.lang.Object,
 *      com.thoughtworks.xstream.io.HierarchicalStreamWriter, com.thoughtworks.xstream.converters.MarshallingContext)
 *///from www  .  j a v a  2 s . c o  m
@Override
public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) {
    Object collection = source;

    if (source instanceof PersistentCollection) {
        PersistentCollection col = (PersistentCollection) source;
        col.forceInitialization();
        // ToDo ES: collection = col.getCollectionSnapshot().getSnapshot();
        collection = col.getStoredSnapshot();
    }

    // the set is returned as a map by Hibernate (unclear why exactly)
    if (source instanceof PersistentSortedSet) {
        collection = new TreeSet(((HashMap) collection).values());
    } else if (source instanceof PersistentSet) {
        // collection = new HashSet(((HashMap)collection).entrySet());
        collection = new HashSet(((HashMap) collection).values());
    }

    // delegate the collection to the approapriate converter
    if (listSetConverter.canConvert(collection.getClass())) {
        listSetConverter.marshal(collection, writer, context);
        return;
    }
    if (mapConverter.canConvert(collection.getClass())) {
        mapConverter.marshal(collection, writer, context);
        return;
    }
    if (treeMapConverter.canConvert(collection.getClass())) {
        treeMapConverter.marshal(collection, writer, context);
        return;
    }
    if (treeSetConverter.canConvert(collection.getClass())) {
        treeSetConverter.marshal(collection, writer, context);
        return;
    }

    defaultConverter.marshal(collection, writer, context);
}

From source file:org.grails.orm.hibernate.proxy.HibernateProxyHandler.java

License:Apache License

public void initialize(Object o) {
    if (o instanceof PersistentCollection) {
        final PersistentCollection col = (PersistentCollection) o;
        if (!col.wasInitialized()) {
            col.forceInitialization();
        }//from   w  ww.  j  a va  2  s  .  c o m
    }
    super.initialize(o);
}