Search in sources :

Example 6 with CollectionPersister

use of org.hibernate.persister.collection.CollectionPersister in project hibernate-orm by hibernate.

the class WrapVisitor method processArrayOrNewCollection.

final Object processArrayOrNewCollection(Object collection, CollectionType collectionType) throws HibernateException {
    final SessionImplementor session = getSession();
    if (collection == null) {
        //do nothing
        return null;
    } else {
        CollectionPersister persister = session.getFactory().getCollectionPersister(collectionType.getRole());
        final PersistenceContext persistenceContext = session.getPersistenceContext();
        //TODO: move into collection type, so we can use polymorphism!
        if (collectionType.hasHolder()) {
            if (collection == CollectionType.UNFETCHED_COLLECTION) {
                return null;
            }
            PersistentCollection ah = persistenceContext.getCollectionHolder(collection);
            if (ah == null) {
                ah = collectionType.wrap(session, collection);
                persistenceContext.addNewCollection(persister, ah);
                persistenceContext.addCollectionHolder(ah);
            }
            return null;
        } else {
            PersistentCollection persistentCollection = collectionType.wrap(session, collection);
            persistenceContext.addNewCollection(persister, persistentCollection);
            if (LOG.isTraceEnabled()) {
                LOG.tracev("Wrapped collection in role: {0}", collectionType.getRole());
            }
            //Force a substitution!
            return persistentCollection;
        }
    }
}
Also used : PersistentCollection(org.hibernate.collection.spi.PersistentCollection) CollectionPersister(org.hibernate.persister.collection.CollectionPersister) SessionImplementor(org.hibernate.engine.spi.SessionImplementor) PersistenceContext(org.hibernate.engine.spi.PersistenceContext)

Example 7 with CollectionPersister

use of org.hibernate.persister.collection.CollectionPersister in project hibernate-orm by hibernate.

the class OnLockVisitor method processCollection.

@Override
public Object processCollection(Object collection, CollectionType type) throws HibernateException {
    if (collection == null) {
        return null;
    }
    final SessionImplementor session = getSession();
    final CollectionPersister persister = session.getFactory().getCollectionPersister(type.getRole());
    if (collection instanceof PersistentCollection) {
        final PersistentCollection persistentCollection = (PersistentCollection) collection;
        if (persistentCollection.setCurrentSession(session)) {
            if (isOwnerUnchanged(persistentCollection, persister, extractCollectionKeyFromOwner(persister))) {
                // a "detached" collection that originally belonged to the same entity
                if (persistentCollection.isDirty()) {
                    throw new HibernateException("reassociated object has dirty collection");
                }
                reattachCollection(persistentCollection, type);
            } else {
                // a "detached" collection that belonged to a different entity
                throw new HibernateException("reassociated object has dirty collection reference");
            }
        } else {
            // to the entity passed to update()
            throw new HibernateException("reassociated object has dirty collection reference");
        }
    } else {
        //TODO: or an array!! we can't lock objects with arrays now??
        throw new HibernateException("reassociated object has dirty collection reference (or an array)");
    }
    return null;
}
Also used : PersistentCollection(org.hibernate.collection.spi.PersistentCollection) CollectionPersister(org.hibernate.persister.collection.CollectionPersister) HibernateException(org.hibernate.HibernateException) SessionImplementor(org.hibernate.engine.spi.SessionImplementor)

Example 8 with CollectionPersister

use of org.hibernate.persister.collection.CollectionPersister in project hibernate-orm by hibernate.

the class QueryTranslatorImpl method renderSQL.

private void renderSQL() throws QueryException, MappingException {
    final int rtsize;
    if (returnedTypes.size() == 0 && scalarTypes.size() == 0) {
        //ie no select clause in HQL
        returnedTypes = fromTypes;
        rtsize = returnedTypes.size();
    } else {
        rtsize = returnedTypes.size();
        Iterator iter = entitiesToFetch.iterator();
        while (iter.hasNext()) {
            returnedTypes.add(iter.next());
        }
    }
    int size = returnedTypes.size();
    persisters = new Queryable[size];
    names = new String[size];
    owners = new int[size];
    ownerAssociationTypes = new EntityType[size];
    suffixes = new String[size];
    includeInSelect = new boolean[size];
    for (int i = 0; i < size; i++) {
        String name = (String) returnedTypes.get(i);
        //if ( !isName(name) ) throw new QueryException("unknown type: " + name);
        persisters[i] = getEntityPersisterForName(name);
        // TODO: cannot use generateSuffixes() - it handles the initial suffix differently.
        suffixes[i] = (size == 1) ? "" : Integer.toString(i) + '_';
        names[i] = name;
        includeInSelect[i] = !entitiesToFetch.contains(name);
        if (includeInSelect[i]) {
            selectLength++;
        }
        if (name.equals(collectionOwnerName)) {
            collectionOwnerColumn = i;
        }
        String oneToOneOwner = (String) oneToOneOwnerNames.get(name);
        owners[i] = (oneToOneOwner == null) ? -1 : returnedTypes.indexOf(oneToOneOwner);
        ownerAssociationTypes[i] = (EntityType) uniqueKeyOwnerReferences.get(name);
    }
    if (ArrayHelper.isAllNegative(owners)) {
        owners = null;
    }
    //Must be done here because of side-effect! yuck...
    String scalarSelect = renderScalarSelect();
    int scalarSize = scalarTypes.size();
    hasScalars = scalarTypes.size() != rtsize;
    returnTypes = new Type[scalarSize];
    for (int i = 0; i < scalarSize; i++) {
        returnTypes[i] = (Type) scalarTypes.get(i);
    }
    QuerySelect sql = new QuerySelect(getFactory().getDialect());
    sql.setDistinct(distinct);
    if (!shallowQuery) {
        renderIdentifierSelect(sql);
        renderPropertiesSelect(sql);
    }
    if (collectionPersister != null) {
        sql.addSelectFragmentString(collectionPersister.selectFragment(fetchName, "__"));
    }
    if (hasScalars || shallowQuery) {
        sql.addSelectFragmentString(scalarSelect);
    }
    //TODO: for some dialects it would be appropriate to add the renderOrderByPropertiesSelect() to other select strings
    mergeJoins(sql.getJoinFragment());
    sql.setWhereTokens(whereTokens.iterator());
    sql.setGroupByTokens(groupByTokens.iterator());
    sql.setHavingTokens(havingTokens.iterator());
    sql.setOrderByTokens(orderByTokens.iterator());
    if (collectionPersister != null && collectionPersister.hasOrdering()) {
        sql.addOrderBy(collectionPersister.getSQLOrderByString(fetchName));
    }
    scalarColumnNames = NameGenerator.generateColumnNames(returnTypes, getFactory());
    // initialize the Set of queried identifier spaces (ie. tables)
    Iterator iter = collections.values().iterator();
    while (iter.hasNext()) {
        CollectionPersister p = getCollectionPersister((String) iter.next());
        addQuerySpaces(p.getCollectionSpaces());
    }
    iter = typeMap.keySet().iterator();
    while (iter.hasNext()) {
        Queryable p = getEntityPersisterForName((String) iter.next());
        addQuerySpaces(p.getQuerySpaces());
    }
    sqlString = sql.toQueryString();
    if (holderClass != null) {
        holderConstructor = ReflectHelper.getConstructor(holderClass, returnTypes);
    }
    if (hasScalars) {
        actualReturnTypes = returnTypes;
    } else {
        actualReturnTypes = new Type[selectLength];
        int j = 0;
        for (int i = 0; i < persisters.length; i++) {
            if (includeInSelect[i]) {
                actualReturnTypes[j++] = getFactory().getTypeResolver().getTypeFactory().manyToOne(persisters[i].getEntityName(), shallowQuery);
            }
        }
    }
}
Also used : CollectionPersister(org.hibernate.persister.collection.CollectionPersister) Queryable(org.hibernate.persister.entity.Queryable) Iterator(java.util.Iterator) QuerySelect(org.hibernate.sql.QuerySelect)

Example 9 with CollectionPersister

use of org.hibernate.persister.collection.CollectionPersister in project hibernate-orm by hibernate.

the class CacheImpl method containsCollection.

@Override
public boolean containsCollection(String role, Serializable ownerIdentifier) {
    CollectionPersister p = sessionFactory.getMetamodel().collectionPersister(role);
    if (p.hasCache()) {
        CollectionRegionAccessStrategy cache = p.getCacheAccessStrategy();
        // have to assume non tenancy
        Object key = cache.generateCacheKey(ownerIdentifier, p, sessionFactory, null);
        return cache.getRegion().contains(key);
    } else {
        return false;
    }
}
Also used : CollectionPersister(org.hibernate.persister.collection.CollectionPersister) CollectionRegionAccessStrategy(org.hibernate.cache.spi.access.CollectionRegionAccessStrategy)

Example 10 with CollectionPersister

use of org.hibernate.persister.collection.CollectionPersister in project hibernate-orm by hibernate.

the class CacheImpl method evictCollection.

@Override
public void evictCollection(String role, Serializable ownerIdentifier) {
    CollectionPersister p = sessionFactory.getMetamodel().collectionPersister(role);
    if (p.hasCache()) {
        if (LOG.isDebugEnabled()) {
            LOG.debugf("Evicting second-level cache: %s", MessageHelper.collectionInfoString(p, ownerIdentifier, sessionFactory));
        }
        CollectionRegionAccessStrategy cache = p.getCacheAccessStrategy();
        // have to assume non tenancy
        Object key = cache.generateCacheKey(ownerIdentifier, p, sessionFactory, null);
        cache.evict(key);
    }
}
Also used : CollectionPersister(org.hibernate.persister.collection.CollectionPersister) CollectionRegionAccessStrategy(org.hibernate.cache.spi.access.CollectionRegionAccessStrategy)

Aggregations

CollectionPersister (org.hibernate.persister.collection.CollectionPersister)44 HibernateException (org.hibernate.HibernateException)11 Type (org.hibernate.type.Type)9 PersistentCollection (org.hibernate.collection.spi.PersistentCollection)8 Serializable (java.io.Serializable)7 CollectionRegionAccessStrategy (org.hibernate.cache.spi.access.CollectionRegionAccessStrategy)7 CollectionType (org.hibernate.type.CollectionType)7 HashMap (java.util.HashMap)6 Session (org.hibernate.Session)6 SessionImplementor (org.hibernate.engine.spi.SessionImplementor)6 EntityPersister (org.hibernate.persister.entity.EntityPersister)6 Test (org.junit.Test)6 Map (java.util.Map)5 CollectionEntry (org.hibernate.engine.spi.CollectionEntry)5 EntityEntry (org.hibernate.engine.spi.EntityEntry)5 CompositeType (org.hibernate.type.CompositeType)5 EntityType (org.hibernate.type.EntityType)5 ResultSet (java.sql.ResultSet)4 AssertionFailure (org.hibernate.AssertionFailure)4 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)4