Search in sources :

Example 16 with AssociationType

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

the class BatchFetchStrategyHelperTest method testManyToOneDefaultFetch.

@Test
public void testManyToOneDefaultFetch() {
    final AssociationType associationType = determineAssociationType(AnEntity.class, "otherEntityDefault");
    final org.hibernate.FetchMode fetchMode = determineFetchMode(AnEntity.class, "otherEntityDefault");
    assertSame(org.hibernate.FetchMode.JOIN, fetchMode);
    final FetchStyle fetchStyle = FetchStrategyHelper.determineFetchStyleByMetadata(fetchMode, associationType, sessionFactory());
    // batch size is ignored with org.hibernate.FetchMode.JOIN
    assertSame(FetchStyle.JOIN, fetchStyle);
    final FetchTiming fetchTiming = FetchStrategyHelper.determineFetchTiming(fetchStyle, associationType, sessionFactory());
    assertSame(FetchTiming.IMMEDIATE, fetchTiming);
}
Also used : FetchStyle(org.hibernate.engine.FetchStyle) AssociationType(org.hibernate.type.AssociationType) FetchTiming(org.hibernate.engine.FetchTiming) Test(org.junit.Test)

Example 17 with AssociationType

use of org.hibernate.type.AssociationType in project yyl_example by Relucent.

the class HibernateTest method test3.

private static void test3(SessionFactory sessionFactory) {
    System.out.println("--------------------------------------------------");
    ClassMetadata metadata = sessionFactory.getClassMetadata(Table1.class);
    System.out.println("EntityName: " + metadata.getMappedClass());
    System.out.println("IdProperty: " + metadata.getIdentifierPropertyName());
    for (String propertyName : metadata.getPropertyNames()) {
        Type type = metadata.getPropertyType(propertyName);
        System.out.println("Property: " + propertyName);
        System.out.println(" isComponentType: " + type.isComponentType());
        System.out.println(" isAnyType: " + type.isAnyType());
        System.out.println(" isCollection: " + type.isCollectionType());
        System.out.println(" isAssociationType: " + type.isAssociationType());
        if (type instanceof AssociationType) {
            AssociationType associationType = (AssociationType) type;
            String associatedEntityName = associationType.getAssociatedEntityName((SessionFactoryImpl) sessionFactory);
            ClassMetadata associatedClassMetadata = sessionFactory.getClassMetadata(associatedEntityName);
            Class<?> mappedClass = associatedClassMetadata.getMappedClass();
            System.out.println(" associatedClass: " + mappedClass);
        } else {
            System.out.println(" propertyClass: " + type.getReturnedClass());
        }
        System.out.println();
    }
}
Also used : ClassMetadata(org.hibernate.metadata.ClassMetadata) AssociationType(org.hibernate.type.AssociationType) Type(org.hibernate.type.Type) AssociationType(org.hibernate.type.AssociationType)

Example 18 with AssociationType

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

the class AttributeNodeImpl method internalMakeSubgraph.

@SuppressWarnings("unchecked")
private <X> SubgraphImpl<X> internalMakeSubgraph(Class<X> type) {
    if (attribute.getPersistentAttributeType() == Attribute.PersistentAttributeType.BASIC || attribute.getPersistentAttributeType() == Attribute.PersistentAttributeType.EMBEDDED) {
        throw new IllegalArgumentException(String.format("Attribute [%s] is not of managed type", getAttributeName()));
    }
    if (attribute.getPersistentAttributeType() == Attribute.PersistentAttributeType.ELEMENT_COLLECTION) {
        throw new IllegalArgumentException(String.format("Collection elements [%s] is not of managed type", getAttributeName()));
    }
    if (subgraphMap == null) {
        subgraphMap = new HashMap<>();
    }
    final Helper.AttributeSource attributeSource = Helper.resolveAttributeSource(sessionFactory(), managedType);
    final AssociationType attributeType = (AssociationType) attributeSource.findType(attribute.getName());
    final Joinable joinable = attributeType.getAssociatedJoinable(sessionFactory());
    if (joinable.isCollection()) {
        final EntityPersister elementEntityPersister = ((QueryableCollection) joinable).getElementPersister();
        if (type == null) {
            type = elementEntityPersister.getMappedClass();
        } else {
            if (!isTreatableAs(elementEntityPersister, type)) {
                throw new IllegalArgumentException(String.format("Collection elements [%s] cannot be treated as requested type [%s] : %s", getAttributeName(), type.getName(), elementEntityPersister.getMappedClass().getName()));
            }
        }
    } else {
        final EntityPersister entityPersister = (EntityPersister) joinable;
        if (type == null) {
            type = entityPersister.getMappedClass();
        } else {
            if (!isTreatableAs(entityPersister, type)) {
                throw new IllegalArgumentException(String.format("Attribute [%s] cannot be treated as requested type [%s] : %s", getAttributeName(), type.getName(), entityPersister.getMappedClass().getName()));
            }
        }
    }
    ManagedType managedType = null;
    try {
        managedType = sessionFactory.getMetamodel().entity(type.getName());
    } catch (IllegalArgumentException e) {
    // do nothing
    }
    if (managedType == null) {
        managedType = attribute.getDeclaringType();
    }
    final SubgraphImpl<X> subgraph = new SubgraphImpl<>(this.sessionFactory, managedType, type);
    subgraphMap.put(type, subgraph);
    return subgraph;
}
Also used : CollectionHelper(org.hibernate.internal.util.collections.CollectionHelper) Helper(org.hibernate.metamodel.internal.Helper) EntityPersister(org.hibernate.persister.entity.EntityPersister) ManagedType(javax.persistence.metamodel.ManagedType) AssociationType(org.hibernate.type.AssociationType) Joinable(org.hibernate.persister.entity.Joinable) QueryableCollection(org.hibernate.persister.collection.QueryableCollection)

Example 19 with AssociationType

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

the class Loader method loadFromResultSet.

/**
	 * Hydrate the state an object from the SQL <tt>ResultSet</tt>, into
	 * an array or "hydrated" values (do not resolve associations yet),
	 * and pass the hydrates state to the session.
	 */
private void loadFromResultSet(final ResultSet rs, final int i, final Object object, final String instanceEntityName, final EntityKey key, final String rowIdAlias, final LockMode lockMode, final Loadable rootPersister, final SharedSessionContractImplementor session) throws SQLException, HibernateException {
    final Serializable id = key.getIdentifier();
    // Get the persister for the _subclass_
    final Loadable persister = (Loadable) getFactory().getEntityPersister(instanceEntityName);
    if (LOG.isTraceEnabled()) {
        LOG.tracef("Initializing object from ResultSet: %s", MessageHelper.infoString(persister, id, getFactory()));
    }
    boolean fetchAllPropertiesRequested = isEagerPropertyFetchEnabled(i);
    // add temp entry so that the next step is circular-reference
    // safe - only needed because some types don't take proper
    // advantage of two-phase-load (esp. components)
    TwoPhaseLoad.addUninitializedEntity(key, object, persister, lockMode, session);
    //This is not very nice (and quite slow):
    final String[][] cols = persister == rootPersister ? getEntityAliases()[i].getSuffixedPropertyAliases() : getEntityAliases()[i].getSuffixedPropertyAliases(persister);
    final Object[] values = persister.hydrate(rs, id, object, rootPersister, cols, fetchAllPropertiesRequested, session);
    final Object rowId = persister.hasRowId() ? rs.getObject(rowIdAlias) : null;
    final AssociationType[] ownerAssociationTypes = getOwnerAssociationTypes();
    if (ownerAssociationTypes != null && ownerAssociationTypes[i] != null) {
        String ukName = ownerAssociationTypes[i].getRHSUniqueKeyPropertyName();
        if (ukName != null) {
            final int index = ((UniqueKeyLoadable) persister).getPropertyIndex(ukName);
            final Type type = persister.getPropertyTypes()[index];
            // polymorphism not really handled completely correctly,
            // perhaps...well, actually its ok, assuming that the
            // entity name used in the lookup is the same as the
            // the one used here, which it will be
            EntityUniqueKey euk = new EntityUniqueKey(//polymorphism comment above
            rootPersister.getEntityName(), ukName, type.semiResolve(values[index], session, object), type, persister.getEntityMode(), session.getFactory());
            session.getPersistenceContext().addEntity(euk, object);
        }
    }
    TwoPhaseLoad.postHydrate(persister, id, values, rowId, object, lockMode, session);
}
Also used : UniqueKeyLoadable(org.hibernate.persister.entity.UniqueKeyLoadable) Loadable(org.hibernate.persister.entity.Loadable) Serializable(java.io.Serializable) UniqueKeyLoadable(org.hibernate.persister.entity.UniqueKeyLoadable) EntityType(org.hibernate.type.EntityType) VersionType(org.hibernate.type.VersionType) AssociationType(org.hibernate.type.AssociationType) Type(org.hibernate.type.Type) AssociationType(org.hibernate.type.AssociationType) EntityUniqueKey(org.hibernate.engine.spi.EntityUniqueKey)

Example 20 with AssociationType

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

the class JoinWalker method walkCollectionTree.

/**
	 * For a collection role, return a list of associations to be fetched by outerjoin
	 */
private void walkCollectionTree(final QueryableCollection persister, final String alias, final PropertyPath path, final int currentDepth) throws MappingException {
    if (persister.isOneToMany()) {
        walkEntityTree((OuterJoinLoadable) persister.getElementPersister(), alias, path, currentDepth);
    } else {
        Type type = persister.getElementType();
        if (type.isAssociationType()) {
            // a many-to-many;
            // decrement currentDepth here to allow join across the association table
            // without exceeding MAX_FETCH_DEPTH (i.e. the "currentDepth - 1" bit)
            AssociationType associationType = (AssociationType) type;
            String[] aliasedLhsColumns = persister.getElementColumnNames(alias);
            String[] lhsColumns = persister.getElementColumnNames();
            // if the current depth is 0, the root thing being loaded is the
            // many-to-many collection itself.  Here, it is alright to use
            // an inner join...
            boolean useInnerJoin = currentDepth == 0;
            final JoinType joinType = getJoinType(associationType, persister.getFetchMode(), path, persister.getTableName(), lhsColumns, !useInnerJoin, currentDepth - 1, //operations which cascade as far as the collection also cascade to collection elements
            null);
            addAssociationToJoinTreeIfNecessary(associationType, aliasedLhsColumns, alias, path, currentDepth - 1, joinType);
        } else if (type.isComponentType()) {
            walkCompositeElementTree((CompositeType) type, persister.getElementColumnNames(), persister, alias, path, currentDepth);
        }
    }
}
Also used : EntityType(org.hibernate.type.EntityType) CompositeType(org.hibernate.type.CompositeType) JoinType(org.hibernate.sql.JoinType) AssociationType(org.hibernate.type.AssociationType) Type(org.hibernate.type.Type) AssociationType(org.hibernate.type.AssociationType) JoinType(org.hibernate.sql.JoinType) CompositeType(org.hibernate.type.CompositeType)

Aggregations

AssociationType (org.hibernate.type.AssociationType)43 Type (org.hibernate.type.Type)18 FetchStyle (org.hibernate.engine.FetchStyle)17 FetchTiming (org.hibernate.engine.FetchTiming)17 Test (org.junit.Test)17 CompositeType (org.hibernate.type.CompositeType)12 EntityType (org.hibernate.type.EntityType)10 JoinType (org.hibernate.sql.JoinType)9 QueryableCollection (org.hibernate.persister.collection.QueryableCollection)6 EntityPersister (org.hibernate.persister.entity.EntityPersister)5 OuterJoinLoadable (org.hibernate.persister.entity.OuterJoinLoadable)5 UniqueKeyLoadable (org.hibernate.persister.entity.UniqueKeyLoadable)4 CollectionType (org.hibernate.type.CollectionType)4 VersionType (org.hibernate.type.VersionType)4 Iterator (java.util.Iterator)3 ManagedType (javax.persistence.metamodel.ManagedType)3 QueryException (org.hibernate.QueryException)3 Joinable (org.hibernate.persister.entity.Joinable)3 AssociationKey (org.hibernate.persister.walking.spi.AssociationKey)3 Serializable (java.io.Serializable)2