Example usage for org.hibernate.internal SessionFactoryImpl getCollectionPersister

List of usage examples for org.hibernate.internal SessionFactoryImpl getCollectionPersister

Introduction

In this page you can find the example usage for org.hibernate.internal SessionFactoryImpl getCollectionPersister.

Prototype

@Deprecated
default CollectionPersister getCollectionPersister(String role) throws MappingException 

Source Link

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;
        }//from ww  w  .j a v  a 2  s  .co m

        // 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;
    });
}