Search in sources :

Example 16 with Joinable

use of org.hibernate.persister.entity.Joinable in project hibernate-orm by hibernate.

the class JoinWalker method selectString.

/**
 * Generate a select list of columns containing all properties of the entity classes
 */
protected final String selectString(List associations) throws MappingException {
    if (associations.size() == 0) {
        return "";
    } else {
        StringBuilder buf = new StringBuilder(associations.size() * 100);
        int entityAliasCount = 0;
        int collectionAliasCount = 0;
        for (int i = 0; i < associations.size(); i++) {
            OuterJoinableAssociation join = (OuterJoinableAssociation) associations.get(i);
            OuterJoinableAssociation next = (i == associations.size() - 1) ? null : (OuterJoinableAssociation) associations.get(i + 1);
            final Joinable joinable = join.getJoinable();
            final String entitySuffix = (suffixes == null || entityAliasCount >= suffixes.length) ? null : suffixes[entityAliasCount];
            final String collectionSuffix = (collectionSuffixes == null || collectionAliasCount >= collectionSuffixes.length) ? null : collectionSuffixes[collectionAliasCount];
            final String selectFragment = joinable.selectFragment(next == null ? null : next.getJoinable(), next == null ? null : next.getRHSAlias(), join.getRHSAlias(), entitySuffix, collectionSuffix, join.getJoinType() == JoinType.LEFT_OUTER_JOIN);
            if (selectFragment.trim().length() > 0) {
                buf.append(", ").append(selectFragment);
            }
            if (joinable.consumesEntityAlias()) {
                entityAliasCount++;
            }
            if (joinable.consumesCollectionAlias() && join.getJoinType() == JoinType.LEFT_OUTER_JOIN && !join.hasRestriction()) {
                collectionAliasCount++;
            }
        }
        return buf.toString();
    }
}
Also used : Joinable(org.hibernate.persister.entity.Joinable)

Example 17 with Joinable

use of org.hibernate.persister.entity.Joinable in project hibernate-orm by hibernate.

the class JoinHelper method getRHSColumnNames.

/**
 * Get the columns of the associated table which are to be used in the join
 *
 * @param type The type
 * @param factory The SessionFactory
 *
 * @return The columns for the right-hand-side of the join
 */
public static String[] getRHSColumnNames(AssociationType type, SessionFactoryImplementor factory) {
    final String uniqueKeyPropertyName = type.getRHSUniqueKeyPropertyName();
    final Joinable joinable = type.getAssociatedJoinable(factory);
    if (uniqueKeyPropertyName == null) {
        return joinable.getKeyColumnNames();
    } else {
        return ((OuterJoinLoadable) joinable).getPropertyColumnNames(uniqueKeyPropertyName);
    }
}
Also used : OuterJoinLoadable(org.hibernate.persister.entity.OuterJoinLoadable) Joinable(org.hibernate.persister.entity.Joinable)

Example 18 with Joinable

use of org.hibernate.persister.entity.Joinable in project hibernate-orm by hibernate.

the class AbstractCompositionAttribute method getAttributes.

@Override
public Iterable<AttributeDefinition> getAttributes() {
    return new Iterable<AttributeDefinition>() {

        @Override
        public Iterator<AttributeDefinition> iterator() {
            return new Iterator<AttributeDefinition>() {

                private final int numberOfAttributes = getType().getSubtypes().length;

                private int currentSubAttributeNumber;

                private int currentColumnPosition = columnStartPosition;

                @Override
                public boolean hasNext() {
                    return currentSubAttributeNumber < numberOfAttributes;
                }

                @Override
                public AttributeDefinition next() {
                    final int subAttributeNumber = currentSubAttributeNumber;
                    currentSubAttributeNumber++;
                    final String name = getType().getPropertyNames()[subAttributeNumber];
                    final Type type = getType().getSubtypes()[subAttributeNumber];
                    int columnPosition = currentColumnPosition;
                    currentColumnPosition += type.getColumnSpan(sessionFactory());
                    final CompositeType cType = getType();
                    final boolean nullable = cType.getPropertyNullability() == null || cType.getPropertyNullability()[subAttributeNumber];
                    if (type.isAssociationType()) {
                        // we build the association-key here because of the "goofiness" with 'currentColumnPosition'
                        final AssociationKey associationKey;
                        final AssociationType aType = (AssociationType) type;
                        final Joinable joinable = aType.getAssociatedJoinable(sessionFactory());
                        if (aType.isAnyType()) {
                            associationKey = new AssociationKey(JoinHelper.getLHSTableName(aType, attributeNumber(), (OuterJoinLoadable) locateOwningPersister()), JoinHelper.getLHSColumnNames(aType, attributeNumber(), columnPosition, (OuterJoinLoadable) locateOwningPersister(), sessionFactory()));
                        } else if (aType.getForeignKeyDirection() == ForeignKeyDirection.FROM_PARENT) {
                            final String lhsTableName;
                            final String[] lhsColumnNames;
                            if (joinable.isCollection()) {
                                final QueryableCollection collectionPersister = (QueryableCollection) joinable;
                                lhsTableName = collectionPersister.getTableName();
                                lhsColumnNames = collectionPersister.getElementColumnNames();
                            } else {
                                final OuterJoinLoadable entityPersister = (OuterJoinLoadable) locateOwningPersister();
                                lhsTableName = getLHSTableName(aType, attributeNumber(), entityPersister);
                                lhsColumnNames = getLHSColumnNames(aType, attributeNumber(), columnPosition, entityPersister, sessionFactory());
                            }
                            associationKey = new AssociationKey(lhsTableName, lhsColumnNames);
                        } else {
                            associationKey = new AssociationKey(joinable.getTableName(), getRHSColumnNames(aType, sessionFactory()));
                        }
                        return new CompositeBasedAssociationAttribute(AbstractCompositionAttribute.this, sessionFactory(), attributeNumber(), name, (AssociationType) type, new BaselineAttributeInformation.Builder().setInsertable(AbstractCompositionAttribute.this.isInsertable()).setUpdateable(AbstractCompositionAttribute.this.isUpdateable()).setNullable(nullable).setDirtyCheckable(true).setVersionable(AbstractCompositionAttribute.this.isVersionable()).setCascadeStyle(getType().getCascadeStyle(subAttributeNumber)).setFetchMode(getType().getFetchMode(subAttributeNumber)).createInformation(), subAttributeNumber, associationKey);
                    } else if (type.isComponentType()) {
                        return new CompositionBasedCompositionAttribute(AbstractCompositionAttribute.this, sessionFactory(), attributeNumber(), name, (CompositeType) type, columnPosition, new BaselineAttributeInformation.Builder().setInsertable(AbstractCompositionAttribute.this.isInsertable()).setUpdateable(AbstractCompositionAttribute.this.isUpdateable()).setNullable(nullable).setDirtyCheckable(true).setVersionable(AbstractCompositionAttribute.this.isVersionable()).setCascadeStyle(getType().getCascadeStyle(subAttributeNumber)).setFetchMode(getType().getFetchMode(subAttributeNumber)).createInformation());
                    } else {
                        return new CompositeBasedBasicAttribute(AbstractCompositionAttribute.this, sessionFactory(), subAttributeNumber, name, type, new BaselineAttributeInformation.Builder().setInsertable(AbstractCompositionAttribute.this.isInsertable()).setUpdateable(AbstractCompositionAttribute.this.isUpdateable()).setNullable(nullable).setDirtyCheckable(true).setVersionable(AbstractCompositionAttribute.this.isVersionable()).setCascadeStyle(getType().getCascadeStyle(subAttributeNumber)).setFetchMode(getType().getFetchMode(subAttributeNumber)).createInformation());
                    }
                }

                @Override
                public void remove() {
                    throw new UnsupportedOperationException("Remove operation not supported here");
                }
            };
        }
    };
}
Also used : AssociationKey(org.hibernate.persister.walking.spi.AssociationKey) OuterJoinLoadable(org.hibernate.persister.entity.OuterJoinLoadable) AttributeDefinition(org.hibernate.persister.walking.spi.AttributeDefinition) QueryableCollection(org.hibernate.persister.collection.QueryableCollection) CompositeType(org.hibernate.type.CompositeType) AssociationType(org.hibernate.type.AssociationType) Type(org.hibernate.type.Type) AssociationType(org.hibernate.type.AssociationType) Joinable(org.hibernate.persister.entity.Joinable) Iterator(java.util.Iterator) BaselineAttributeInformation(org.hibernate.tuple.BaselineAttributeInformation) CompositeType(org.hibernate.type.CompositeType)

Example 19 with Joinable

use of org.hibernate.persister.entity.Joinable in project midpoint by Evolveum.

the class RUtil method getTableName.

public static String getTableName(Class<?> hqlType, Session session) {
    SessionFactory factory = session.getSessionFactory();
    MetamodelImpl model = (MetamodelImpl) factory.getMetamodel();
    EntityPersister ep = model.entityPersister(hqlType);
    if (ep instanceof Joinable) {
        Joinable joinable = (Joinable) ep;
        return joinable.getTableName();
    }
    throw new SystemException("Couldn't get table name for class " + hqlType.getName());
}
Also used : SessionFactory(org.hibernate.SessionFactory) EntityPersister(org.hibernate.persister.entity.EntityPersister) AbstractEntityPersister(org.hibernate.persister.entity.AbstractEntityPersister) MetamodelImpl(org.hibernate.metamodel.internal.MetamodelImpl) SystemException(com.evolveum.midpoint.util.exception.SystemException) Joinable(org.hibernate.persister.entity.Joinable)

Aggregations

Joinable (org.hibernate.persister.entity.Joinable)19 QueryableCollection (org.hibernate.persister.collection.QueryableCollection)6 OuterJoinLoadable (org.hibernate.persister.entity.OuterJoinLoadable)6 AssociationKey (org.hibernate.persister.walking.spi.AssociationKey)5 AssociationType (org.hibernate.type.AssociationType)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 HibernateException (org.hibernate.HibernateException)2 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)2 EntityReferenceInitializerImpl (org.hibernate.loader.plan.exec.process.internal.EntityReferenceInitializerImpl)2 EntityReferenceAliases (org.hibernate.loader.plan.exec.spi.EntityReferenceAliases)2 FetchSource (org.hibernate.loader.plan.spi.FetchSource)2 CollectionPersister (org.hibernate.persister.collection.CollectionPersister)2 EntityPersister (org.hibernate.persister.entity.EntityPersister)2 CollectionType (org.hibernate.type.CollectionType)2 SetType (org.hibernate.type.SetType)2 AST (antlr.collections.AST)1 SystemException (com.evolveum.midpoint.util.exception.SystemException)1