Search in sources :

Example 21 with CollectionType

use of org.hibernate.type.CollectionType in project hibernate-orm by hibernate.

the class HibernateTraversableResolver method addAssociationsToTheSetForOneProperty.

private void addAssociationsToTheSetForOneProperty(String name, Type type, String prefix, SessionFactoryImplementor factory) {
    if (type.isCollectionType()) {
        CollectionType collType = (CollectionType) type;
        Type assocType = collType.getElementType(factory);
        addAssociationsToTheSetForOneProperty(name, assocType, prefix, factory);
    } else //ToOne association
    if (type.isEntityType() || type.isAnyType()) {
        associations.add(prefix + name);
    } else if (type.isComponentType()) {
        CompositeType componentType = (CompositeType) type;
        addAssociationsToTheSetForAllProperties(componentType.getPropertyNames(), componentType.getSubtypes(), (prefix.equals("") ? name : prefix + name) + ".", factory);
    }
}
Also used : CollectionType(org.hibernate.type.CollectionType) ElementType(java.lang.annotation.ElementType) CompositeType(org.hibernate.type.CompositeType) Type(org.hibernate.type.Type) CollectionType(org.hibernate.type.CollectionType) CompositeType(org.hibernate.type.CompositeType)

Example 22 with CollectionType

use of org.hibernate.type.CollectionType in project hibernate-orm by hibernate.

the class AbstractEntityPersister method initializeLazyProperty.

public Object initializeLazyProperty(String fieldName, Object entity, SharedSessionContractImplementor session) {
    final EntityEntry entry = session.getPersistenceContext().getEntry(entity);
    final InterceptorImplementor interceptor = ((PersistentAttributeInterceptable) entity).$$_hibernate_getInterceptor();
    assert interceptor != null : "Expecting bytecode interceptor to be non-null";
    if (hasCollections()) {
        final Type type = getPropertyType(fieldName);
        if (type.isCollectionType()) {
            // we have a condition where a collection attribute is being access via enhancement:
            // 		we can circumvent all the rest and just return the PersistentCollection
            final CollectionType collectionType = (CollectionType) type;
            final CollectionPersister persister = factory.getMetamodel().collectionPersister(collectionType.getRole());
            // Get/create the collection, and make sure it is initialized!  This initialized part is
            // different from proxy-based scenarios where we have to create the PersistentCollection
            // reference "ahead of time" to add as a reference to the proxy.  For bytecode solutions
            // we are not creating the PersistentCollection ahead of time, but instead we are creating
            // it on first request through the enhanced entity.
            // see if there is already a collection instance associated with the session
            // 		NOTE : can this ever happen?
            final Serializable key = getCollectionKey(persister, entity, entry, session);
            PersistentCollection collection = session.getPersistenceContext().getCollection(new CollectionKey(persister, key));
            if (collection == null) {
                collection = collectionType.instantiate(session, persister, key);
                collection.setOwner(entity);
                session.getPersistenceContext().addUninitializedCollection(persister, collection, key);
            }
            // HHH-11161 Initialize, if the collection is not extra lazy
            if (!persister.isExtraLazy()) {
                session.initializeCollection(collection, false);
            }
            interceptor.attributeInitialized(fieldName);
            if (collectionType.isArrayType()) {
                session.getPersistenceContext().addCollectionHolder(collection);
            }
            // update the "state" of the entity's EntityEntry to over-write UNFETCHED_PROPERTY reference
            // for the collection to the just loaded collection
            final EntityEntry ownerEntry = session.getPersistenceContext().getEntry(entity);
            if (ownerEntry == null) {
                // not good
                throw new AssertionFailure("Could not locate EntityEntry for the collection owner in the PersistenceContext");
            }
            ownerEntry.overwriteLoadedStateCollectionValue(fieldName, collection);
            // EARLY EXIT!!!
            return collection;
        }
    }
    final Serializable id = session.getContextEntityIdentifier(entity);
    if (entry == null) {
        throw new HibernateException("entity is not associated with the session: " + id);
    }
    if (LOG.isTraceEnabled()) {
        LOG.tracev("Initializing lazy properties of: {0}, field access: {1}", MessageHelper.infoString(this, id, getFactory()), fieldName);
    }
    if (session.getCacheMode().isGetEnabled() && hasCache() && isLazyPropertiesCacheable()) {
        final EntityRegionAccessStrategy cache = getCacheAccessStrategy();
        final Object cacheKey = cache.generateCacheKey(id, this, session.getFactory(), session.getTenantIdentifier());
        final Object ce = CacheHelper.fromSharedCache(session, cacheKey, cache);
        if (ce != null) {
            final CacheEntry cacheEntry = (CacheEntry) getCacheEntryStructure().destructure(ce, factory);
            final Object initializedValue = initializeLazyPropertiesFromCache(fieldName, entity, session, entry, cacheEntry);
            interceptor.attributeInitialized(fieldName);
            // NOTE EARLY EXIT!!!
            return initializedValue;
        }
    }
    return initializeLazyPropertiesFromDatastore(fieldName, entity, session, id, entry);
}
Also used : Serializable(java.io.Serializable) AssertionFailure(org.hibernate.AssertionFailure) HibernateException(org.hibernate.HibernateException) PersistentAttributeInterceptable(org.hibernate.engine.spi.PersistentAttributeInterceptable) EntityRegionAccessStrategy(org.hibernate.cache.spi.access.EntityRegionAccessStrategy) CollectionKey(org.hibernate.engine.spi.CollectionKey) StructuredCacheEntry(org.hibernate.cache.spi.entry.StructuredCacheEntry) UnstructuredCacheEntry(org.hibernate.cache.spi.entry.UnstructuredCacheEntry) CacheEntry(org.hibernate.cache.spi.entry.CacheEntry) PersistentCollection(org.hibernate.collection.spi.PersistentCollection) EntityEntry(org.hibernate.engine.spi.EntityEntry) AssociationType(org.hibernate.type.AssociationType) JoinType(org.hibernate.sql.JoinType) CollectionType(org.hibernate.type.CollectionType) EntityType(org.hibernate.type.EntityType) ComponentType(org.hibernate.type.ComponentType) CompositeType(org.hibernate.type.CompositeType) VersionType(org.hibernate.type.VersionType) Type(org.hibernate.type.Type) CollectionPersister(org.hibernate.persister.collection.CollectionPersister) CollectionType(org.hibernate.type.CollectionType)

Example 23 with CollectionType

use of org.hibernate.type.CollectionType in project hibernate-orm by hibernate.

the class ValidityAuditStrategy method isNonIdentifierWhereConditionsRequired.

private boolean isNonIdentifierWhereConditionsRequired(String entityName, String propertyName, SessionImplementor session) {
    final Type propertyType = session.getSessionFactory().getMetamodel().entityPersister(entityName).getPropertyType(propertyName);
    if (propertyType.isCollectionType()) {
        final CollectionType collectionType = (CollectionType) propertyType;
        final Type collectionElementType = collectionType.getElementType(session.getSessionFactory());
        if (collectionElementType instanceof ComponentType) {
            // required for Embeddables
            return true;
        } else if (collectionElementType instanceof MaterializedClobType || collectionElementType instanceof MaterializedNClobType) {
            // for Map<> using @Lob annotations
            return collectionType instanceof MapType;
        }
    }
    return false;
}
Also used : CollectionType(org.hibernate.type.CollectionType) ComponentType(org.hibernate.type.ComponentType) MaterializedNClobType(org.hibernate.type.MaterializedNClobType) MaterializedClobType(org.hibernate.type.MaterializedClobType) MapType(org.hibernate.type.MapType) RevisionType(org.hibernate.envers.RevisionType) Type(org.hibernate.type.Type) ComponentType(org.hibernate.type.ComponentType) CollectionType(org.hibernate.type.CollectionType) MaterializedNClobType(org.hibernate.type.MaterializedNClobType) MaterializedClobType(org.hibernate.type.MaterializedClobType) MapType(org.hibernate.type.MapType)

Example 24 with CollectionType

use of org.hibernate.type.CollectionType in project uPortal by Jasig.

the class PortalRawEventsAggregatorImpl method getCollectionRoles.

private List<String> getCollectionRoles(final SessionFactory sessionFactory, final Class<?> entityClass) {
    List<String> collectionRoles = entityCollectionRoles.get(entityClass);
    if (collectionRoles != null) {
        return collectionRoles;
    }
    final com.google.common.collect.ImmutableList.Builder<String> collectionRolesBuilder = ImmutableList.builder();
    final ClassMetadata classMetadata = sessionFactory.getClassMetadata(entityClass);
    for (final Type type : classMetadata.getPropertyTypes()) {
        if (type.isCollectionType()) {
            collectionRolesBuilder.add(((CollectionType) type).getRole());
        }
    }
    collectionRoles = collectionRolesBuilder.build();
    entityCollectionRoles.put(entityClass, collectionRoles);
    return collectionRoles;
}
Also used : ClassMetadata(org.hibernate.metadata.ClassMetadata) CollectionType(org.hibernate.type.CollectionType) FlushModeType(javax.persistence.FlushModeType) Type(org.hibernate.type.Type) ImmutableList(com.google.common.collect.ImmutableList)

Example 25 with CollectionType

use of org.hibernate.type.CollectionType in project dhis2-core by dhis2.

the class AbstractPropertyIntrospectorService method getPropertiesFromHibernate.

protected Map<String, Property> getPropertiesFromHibernate(Class<?> klass) {
    updateJoinTables();
    ClassMetadata classMetadata = sessionFactory.getClassMetadata(klass);
    // is class persisted with hibernate
    if (classMetadata == null) {
        return new HashMap<>();
    }
    Map<String, Property> properties = new HashMap<>();
    SessionFactoryImplementor sessionFactoryImplementor = (SessionFactoryImplementor) sessionFactory;
    MetadataImplementor metadataImplementor = HibernateMetadata.getMetadataImplementor();
    if (metadataImplementor == null) {
        return new HashMap<>();
    }
    PersistentClass persistentClass = metadataImplementor.getEntityBinding(klass.getName());
    Iterator<?> propertyIterator = persistentClass.getPropertyClosureIterator();
    while (propertyIterator.hasNext()) {
        Property property = new Property(klass);
        property.setRequired(false);
        property.setPersisted(true);
        property.setOwner(true);
        org.hibernate.mapping.Property hibernateProperty = (org.hibernate.mapping.Property) propertyIterator.next();
        Type type = hibernateProperty.getType();
        property.setName(hibernateProperty.getName());
        property.setCascade(hibernateProperty.getCascade());
        property.setCollection(type.isCollectionType());
        property.setSetterMethod(hibernateProperty.getSetter(klass).getMethod());
        property.setGetterMethod(hibernateProperty.getGetter(klass).getMethod());
        if (property.isCollection()) {
            CollectionType collectionType = (CollectionType) type;
            CollectionPersister persister = sessionFactoryImplementor.getCollectionPersister(collectionType.getRole());
            property.setOwner(!persister.isInverse());
            property.setManyToMany(persister.isManyToMany());
            property.setMin(0d);
            property.setMax(Double.MAX_VALUE);
            if (property.isOwner()) {
                property.setOwningRole(collectionType.getRole());
                property.setInverseRole(roleToRole.get(collectionType.getRole()));
            } else {
                property.setOwningRole(roleToRole.get(collectionType.getRole()));
                property.setInverseRole(collectionType.getRole());
            }
        }
        if (SingleColumnType.class.isInstance(type) || CustomType.class.isInstance(type) || ManyToOneType.class.isInstance(type)) {
            Column column = (Column) hibernateProperty.getColumnIterator().next();
            property.setUnique(column.isUnique());
            property.setRequired(!column.isNullable());
            property.setMin(0d);
            property.setMax((double) column.getLength());
            property.setLength(column.getLength());
            if (TextType.class.isInstance(type)) {
                property.setMin(0d);
                property.setMax((double) Integer.MAX_VALUE);
                property.setLength(Integer.MAX_VALUE);
            } else if (IntegerType.class.isInstance(type)) {
                property.setMin(0d);
                property.setMax((double) Integer.MAX_VALUE);
                property.setLength(Integer.MAX_VALUE);
            } else if (LongType.class.isInstance(type)) {
                property.setMin(0d);
                property.setMax((double) Long.MAX_VALUE);
                property.setLength(Integer.MAX_VALUE);
            } else if (DoubleType.class.isInstance(type)) {
                property.setMin(0d);
                property.setMax(Double.MAX_VALUE);
                property.setLength(Integer.MAX_VALUE);
            }
        }
        if (ManyToOneType.class.isInstance(type)) {
            property.setManyToOne(true);
            property.setRequired(property.isRequired() && !property.isCollection());
            if (property.isOwner()) {
                property.setOwningRole(klass.getName() + "." + property.getName());
            } else {
                property.setInverseRole(klass.getName() + "." + property.getName());
            }
        } else if (OneToOneType.class.isInstance(type)) {
            property.setOneToOne(true);
        }
        properties.put(property.getName(), property);
    }
    return properties;
}
Also used : ClassMetadata(org.hibernate.metadata.ClassMetadata) HashMap(java.util.HashMap) ManyToOneType(org.hibernate.type.ManyToOneType) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) MetadataImplementor(org.hibernate.boot.spi.MetadataImplementor) IntegerType(org.hibernate.type.IntegerType) CustomType(org.hibernate.type.CustomType) CollectionType(org.hibernate.type.CollectionType) ManyToOneType(org.hibernate.type.ManyToOneType) IntegerType(org.hibernate.type.IntegerType) TextType(org.hibernate.type.TextType) LongType(org.hibernate.type.LongType) SingleColumnType(org.hibernate.type.SingleColumnType) OneToOneType(org.hibernate.type.OneToOneType) SetType(org.hibernate.type.SetType) DoubleType(org.hibernate.type.DoubleType) Type(org.hibernate.type.Type) CollectionPersister(org.hibernate.persister.collection.CollectionPersister) Column(org.hibernate.mapping.Column) DoubleType(org.hibernate.type.DoubleType) CollectionType(org.hibernate.type.CollectionType) PersistentClass(org.hibernate.mapping.PersistentClass) OneToOneType(org.hibernate.type.OneToOneType)

Aggregations

CollectionType (org.hibernate.type.CollectionType)26 Type (org.hibernate.type.Type)21 JoinType (org.hibernate.sql.JoinType)10 EntityType (org.hibernate.type.EntityType)10 CollectionPersister (org.hibernate.persister.collection.CollectionPersister)7 AssociationType (org.hibernate.type.AssociationType)7 CompositeType (org.hibernate.type.CompositeType)7 QueryException (org.hibernate.QueryException)5 JoinSequence (org.hibernate.engine.internal.JoinSequence)5 QueryableCollection (org.hibernate.persister.collection.QueryableCollection)5 HibernateException (org.hibernate.HibernateException)3 ComponentType (org.hibernate.type.ComponentType)3 SemanticException (antlr.SemanticException)2 Serializable (java.io.Serializable)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 MappingException (org.hibernate.MappingException)2 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)2 SessionFactoryHelper (org.hibernate.hql.internal.ast.util.SessionFactoryHelper)2 ClassMetadata (org.hibernate.metadata.ClassMetadata)2