Search in sources :

Example 31 with AssociationType

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

the class NoProxyFetchStrategyHelperTest 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());
    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 32 with AssociationType

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

the class NoProxyFetchStrategyHelperTest method determineAssociationType.

private AssociationType determineAssociationType(Class<?> entityClass, String path) {
    OuterJoinLoadable entityPersister = (OuterJoinLoadable) sessionFactory().getEntityPersister(entityClass.getName());
    int index = ((UniqueKeyLoadable) entityPersister).getPropertyIndex(path);
    return (AssociationType) entityPersister.getSubclassPropertyType(index);
}
Also used : OuterJoinLoadable(org.hibernate.persister.entity.OuterJoinLoadable) AssociationType(org.hibernate.type.AssociationType) UniqueKeyLoadable(org.hibernate.persister.entity.UniqueKeyLoadable)

Example 33 with AssociationType

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

the class Cascade method cascadeProperty.

/**
	 * Cascade an action to the child or children
	 */
private static void cascadeProperty(final CascadingAction action, final CascadePoint cascadePoint, final EventSource eventSource, final int componentPathStackDepth, final Object parent, final Object child, final Type type, final CascadeStyle style, final String propertyName, final Object anything, final boolean isCascadeDeleteEnabled) throws HibernateException {
    if (child != null) {
        if (type.isAssociationType()) {
            final AssociationType associationType = (AssociationType) type;
            if (cascadeAssociationNow(cascadePoint, associationType)) {
                cascadeAssociation(action, cascadePoint, eventSource, componentPathStackDepth, parent, child, type, style, anything, isCascadeDeleteEnabled);
            }
        } else if (type.isComponentType()) {
            cascadeComponent(action, cascadePoint, eventSource, componentPathStackDepth, parent, child, (CompositeType) type, anything);
        }
    }
    // potentially we need to handle orphan deletes for one-to-ones here...
    if (isLogicalOneToOne(type)) {
        // orphan checking
        if (style.hasOrphanDelete() && action.deleteOrphans()) {
            // value is orphaned if loaded state for this property shows not null
            // because it is currently null.
            final EntityEntry entry = eventSource.getPersistenceContext().getEntry(parent);
            if (entry != null && entry.getStatus() != Status.SAVING) {
                final Object loadedValue;
                if (componentPathStackDepth == 0) {
                    // association defined on entity
                    loadedValue = entry.getLoadedValue(propertyName);
                } else {
                    // association defined on component
                    // 		todo : this is currently unsupported because of the fact that
                    //		we do not know the loaded state of this value properly
                    //		and doing so would be very difficult given how components and
                    //		entities are loaded (and how 'loaded state' is put into the
                    //		EntityEntry).  Solutions here are to either:
                    //			1) properly account for components as a 2-phase load construct
                    //			2) just assume the association was just now orphaned and
                    // 				issue the orphan delete.  This would require a special
                    //				set of SQL statements though since we do not know the
                    //				orphaned value, something a delete with a subquery to
                    // 				match the owner.
                    //							final EntityType entityType = (EntityType) type;
                    //							final String getPropertyPath = composePropertyPath( entityType.getPropertyName() );
                    loadedValue = null;
                }
                // entity is managed (without first nulling and manually flushing).
                if (child == null || (loadedValue != null && child != loadedValue)) {
                    final EntityEntry valueEntry = eventSource.getPersistenceContext().getEntry(loadedValue);
                    // already been flushed.  See HHH-7829.
                    if (valueEntry != null) {
                        final String entityName = valueEntry.getPersister().getEntityName();
                        if (LOG.isTraceEnabled()) {
                            final Serializable id = valueEntry.getPersister().getIdentifier(loadedValue, eventSource);
                            final String description = MessageHelper.infoString(entityName, id);
                            LOG.tracev("Deleting orphaned entity instance: {0}", description);
                        }
                        if (type.isAssociationType() && ((AssociationType) type).getForeignKeyDirection().equals(ForeignKeyDirection.TO_PARENT)) {
                            // If FK direction is to-parent, we must remove the orphan *beforeQuery* the queued update(s)
                            // occur.  Otherwise, replacing the association on a managed entity, without manually
                            // nulling and flushing, causes FK constraint violations.
                            eventSource.removeOrphanBeforeUpdates(entityName, loadedValue);
                        } else {
                            // Else, we must delete afterQuery the updates.
                            eventSource.delete(entityName, loadedValue, isCascadeDeleteEnabled, new HashSet());
                        }
                    }
                }
            }
        }
    }
}
Also used : EntityEntry(org.hibernate.engine.spi.EntityEntry) Serializable(java.io.Serializable) AssociationType(org.hibernate.type.AssociationType) CompositeType(org.hibernate.type.CompositeType) HashSet(java.util.HashSet)

Example 34 with AssociationType

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

the class AbstractPropertyMapping method initPropertyPaths.

/*protected void initPropertyPaths(
			final String path,
			final Type type,
			final String[] columns,
			final String[] formulaTemplates,
			final Mapping factory)
	throws MappingException {
		//addFormulaPropertyPath(path, type, formulaTemplates);
		initPropertyPaths(path, type, columns, formulaTemplates, factory);
	}*/
protected void initPropertyPaths(final String path, final Type type, String[] columns, String[] columnReaders, String[] columnReaderTemplates, final String[] formulaTemplates, final Mapping factory) throws MappingException {
    assert columns != null : "Incoming columns should not be null : " + path;
    assert type != null : "Incoming type should not be null : " + path;
    if (columns.length != type.getColumnSpan(factory)) {
        throw new MappingException("broken column mapping for: " + path + " of: " + getEntityName());
    }
    if (type.isAssociationType()) {
        AssociationType actype = (AssociationType) type;
        if (actype.useLHSPrimaryKey()) {
            columns = getIdentifierColumnNames();
            columnReaders = getIdentifierColumnReaders();
            columnReaderTemplates = getIdentifierColumnReaderTemplates();
        } else {
            String foreignKeyProperty = actype.getLHSPropertyName();
            if (foreignKeyProperty != null && !path.equals(foreignKeyProperty)) {
                //TODO: this requires that the collection is defined afterQuery the
                //      referenced property in the mapping file (ok?)
                columns = columnsByPropertyPath.get(foreignKeyProperty);
                if (columns == null) {
                    //get em on the second pass!
                    return;
                }
                columnReaders = columnReadersByPropertyPath.get(foreignKeyProperty);
                columnReaderTemplates = columnReaderTemplatesByPropertyPath.get(foreignKeyProperty);
            }
        }
    }
    if (path != null) {
        addPropertyPath(path, type, columns, columnReaders, columnReaderTemplates, formulaTemplates);
    }
    if (type.isComponentType()) {
        CompositeType actype = (CompositeType) type;
        initComponentPropertyPaths(path, actype, columns, columnReaders, columnReaderTemplates, formulaTemplates, factory);
        if (actype.isEmbedded()) {
            initComponentPropertyPaths(path == null ? null : StringHelper.qualifier(path), actype, columns, columnReaders, columnReaderTemplates, formulaTemplates, factory);
        }
    } else if (type.isEntityType()) {
        initIdentifierPropertyPaths(path, (EntityType) type, columns, columnReaders, columnReaderTemplates, factory);
    }
}
Also used : EntityType(org.hibernate.type.EntityType) AssociationType(org.hibernate.type.AssociationType) MappingException(org.hibernate.MappingException) CompositeType(org.hibernate.type.CompositeType)

Example 35 with AssociationType

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

the class SingleTableEntityPersister method getSubclassPropertyTableNumber.

private int getSubclassPropertyTableNumber(String propertyName, String entityName) {
    // When there are duplicated property names in the subclasses
    // then propertyMapping.toType( propertyName ) may return an
    // incorrect Type. To ensure correct results, lookup the property type
    // using the concrete EntityPersister with the specified entityName
    // (since the concrete EntityPersister cannot have duplicated property names).
    final EntityPersister concreteEntityPersister;
    if (getEntityName().equals(entityName)) {
        concreteEntityPersister = this;
    } else {
        concreteEntityPersister = getFactory().getMetamodel().entityPersister(entityName);
    }
    Type type = concreteEntityPersister.getPropertyType(propertyName);
    if (type.isAssociationType() && ((AssociationType) type).useLHSPrimaryKey()) {
        return 0;
    }
    final Integer tabnum = propertyTableNumbersByNameAndSubclass.get(entityName + '.' + propertyName);
    return tabnum == null ? 0 : tabnum;
}
Also used : DiscriminatorType(org.hibernate.type.DiscriminatorType) AssociationType(org.hibernate.type.AssociationType) Type(org.hibernate.type.Type)

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