diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/hbm/ListAttributeSourceImpl.java b/hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/hbm/ListAttributeSourceImpl.java index ff42af2d01..d29c1f97db 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/hbm/ListAttributeSourceImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/hbm/ListAttributeSourceImpl.java @@ -24,6 +24,7 @@ package org.hibernate.metamodel.internal.source.hbm; import org.hibernate.internal.jaxb.mapping.hbm.JaxbListElement; +import org.hibernate.internal.jaxb.mapping.hbm.JaxbListIndexElement; import org.hibernate.metamodel.spi.source.AttributeSourceContainer; import org.hibernate.metamodel.spi.source.PluralAttributeIndexSource; import org.hibernate.metamodel.spi.source.PluralAttributeNature; @@ -45,7 +46,12 @@ public class ListAttributeSourceImpl extends AbstractPluralAttributeSourceImpl { JaxbListElement listElement, AttributeSourceContainer container ) { super( sourceMappingDocument, listElement, container ); - this.indexSource = new PluralAttributeIndexSourceImpl( sourceMappingDocument(), listElement.getListIndex(), container ); + JaxbListIndexElement listIndexElement = listElement.getListIndex(); + if ( listIndexElement == null ) { + this.indexSource = new PluralAttributeIndexSourceImpl( sourceMappingDocument(), listElement.getIndex(), container ); + } else { + this.indexSource = new PluralAttributeIndexSourceImpl( sourceMappingDocument(), listIndexElement, container ); + } } public PluralAttributeIndexSource getIndexSource() { diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/hbm/PluralAttributeIndexSourceImpl.java b/hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/hbm/PluralAttributeIndexSourceImpl.java index 81c9cdc95f..9a9f730cfc 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/hbm/PluralAttributeIndexSourceImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/hbm/PluralAttributeIndexSourceImpl.java @@ -28,6 +28,7 @@ import java.util.List; import java.util.Map; import org.hibernate.internal.jaxb.mapping.hbm.JaxbColumnElement; +import org.hibernate.internal.jaxb.mapping.hbm.JaxbIndexElement; import org.hibernate.internal.jaxb.mapping.hbm.JaxbListIndexElement; import org.hibernate.metamodel.spi.source.AttributeSourceContainer; import org.hibernate.metamodel.spi.source.ExplicitHibernateTypeSource; @@ -101,6 +102,64 @@ public class PluralAttributeIndexSourceImpl extends AbstractHbmSourceNode implem base = Integer.parseInt( indexElement.getBase() ); } + public PluralAttributeIndexSourceImpl( + MappingDocument mappingDocument, + final JaxbIndexElement indexElement, + final AttributeSourceContainer container ) { + super( mappingDocument ); + valueSources = Helper.buildValueSources( sourceMappingDocument(), new Helper.ValueSourcesAdapter() { + + List< JaxbColumnElement > columnElements = indexElement.getColumn() == null + ? Collections.EMPTY_LIST + : Collections.singletonList( indexElement.getColumn() ); + + @Override + public String getColumnAttribute() { + return indexElement.getColumnAttribute(); + } + + @Override + public List getColumnOrFormulaElements() { + return columnElements; + } + + @Override + public String getContainingTableName() { + return null; + } + + @Override + public String getFormulaAttribute() { + return null; + } + + @Override + public boolean isIncludedInInsertByDefault() { + return areValuesIncludedInInsertByDefault(); + } + + @Override + public boolean isIncludedInUpdateByDefault() { + return areValuesIncludedInUpdateByDefault(); + } + } ); + + typeSource = new ExplicitHibernateTypeSource() { + + @Override + public String getName() { + return "integer"; + } + + @Override + public Map< String, String > getParameters() { + return java.util.Collections.< String, String >emptyMap(); + } + }; + + base = 0; + } + /** * {@inheritDoc} * diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/spi/binding/BasicPluralAttributeIndexBinding.java b/hibernate-core/src/main/java/org/hibernate/metamodel/spi/binding/BasicPluralAttributeIndexBinding.java index 00de6968e7..4c8ff5ef33 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/spi/binding/BasicPluralAttributeIndexBinding.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/spi/binding/BasicPluralAttributeIndexBinding.java @@ -33,25 +33,12 @@ public class BasicPluralAttributeIndexBinding implements PluralAttributeIndexBin private final AbstractPluralAttributeBinding pluralAttributeBinding; private final HibernateTypeDescriptor hibernateTypeDescriptor = new HibernateTypeDescriptor(); private Value value; - private int base; /** * @param pluralAttributeBinding - * @param base */ - public BasicPluralAttributeIndexBinding( final AbstractPluralAttributeBinding pluralAttributeBinding, int base ) { + public BasicPluralAttributeIndexBinding( final AbstractPluralAttributeBinding pluralAttributeBinding ) { this.pluralAttributeBinding = pluralAttributeBinding; - this.base = base; - } - - /** - * {@inheritDoc} - * - * @see org.hibernate.metamodel.spi.binding.PluralAttributeIndexBinding#base() - */ - @Override - public int base() { - return base; } /** diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/spi/binding/ListBinding.java b/hibernate-core/src/main/java/org/hibernate/metamodel/spi/binding/ListBinding.java index d563761938..c7395b42a2 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/spi/binding/ListBinding.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/spi/binding/ListBinding.java @@ -32,6 +32,7 @@ import org.hibernate.metamodel.spi.source.MetaAttributeContext; public class ListBinding extends AbstractPluralAttributeBinding implements IndexedPluralAttributeBinding { private PluralAttributeIndexBinding pluralAttributeIndexBinding; + private int base; public ListBinding( AttributeBindingContainer container, @@ -52,7 +53,12 @@ public class ListBinding extends AbstractPluralAttributeBinding implements Index includedInOptimisticLocking, isLazy, metaAttributeContext ); - this.pluralAttributeIndexBinding = new BasicPluralAttributeIndexBinding( this, base ); + this.pluralAttributeIndexBinding = new BasicPluralAttributeIndexBinding( this ); + this.base = base; + } + + public int base() { + return base; } /** diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/spi/binding/PluralAttributeIndexBinding.java b/hibernate-core/src/main/java/org/hibernate/metamodel/spi/binding/PluralAttributeIndexBinding.java index 5a2e1a8b06..54afb67d40 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/spi/binding/PluralAttributeIndexBinding.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/spi/binding/PluralAttributeIndexBinding.java @@ -30,8 +30,6 @@ import org.hibernate.metamodel.spi.relational.Value; */ public interface PluralAttributeIndexBinding { - int base(); - PluralAttributeBinding getPluralAttributeBinding(); Value getIndexRelationalValue(); diff --git a/hibernate-core/src/main/java/org/hibernate/persister/collection/AbstractCollectionPersister.java b/hibernate-core/src/main/java/org/hibernate/persister/collection/AbstractCollectionPersister.java index 614c1ed1dc..12ac7d22d8 100644 --- a/hibernate-core/src/main/java/org/hibernate/persister/collection/AbstractCollectionPersister.java +++ b/hibernate-core/src/main/java/org/hibernate/persister/collection/AbstractCollectionPersister.java @@ -80,8 +80,10 @@ import org.hibernate.mapping.Table; import org.hibernate.metadata.CollectionMetadata; import org.hibernate.metamodel.spi.binding.AbstractPluralAttributeBinding; import org.hibernate.metamodel.spi.binding.CustomSQL; +import org.hibernate.metamodel.spi.binding.IndexedPluralAttributeBinding; +import org.hibernate.metamodel.spi.binding.ListBinding; import org.hibernate.metamodel.spi.binding.PluralAttributeAssociationElementBinding; -import org.hibernate.metamodel.spi.binding.PluralAttributeElementBinding; +import org.hibernate.metamodel.spi.binding.PluralAttributeIndexBinding; import org.hibernate.metamodel.spi.binding.PluralAttributeKeyBinding; import org.hibernate.metamodel.spi.binding.RelationalValueBinding; import org.hibernate.metamodel.spi.domain.PluralAttributeNature; @@ -105,7 +107,7 @@ import org.hibernate.type.Type; /** * Base implementation of the QueryableCollection interface. - * + * * @author Gavin King * @see BasicCollectionPersister * @see OneToManyPersister @@ -636,14 +638,14 @@ public abstract class AbstractCollectionPersister dialect = factory.getDialect(); sqlExceptionHelper = factory.getSQLExceptionHelper(); if ( !collection.getHibernateTypeDescriptor().getResolvedTypeMapping().isCollectionType() ) { - throw new MappingException( - String.format( + throw new MappingException( + String.format( "Unexpected resolved type for %s; expected a CollectionType; instead it is %s", collection.getAttribute().getRole(), - collection.getHibernateTypeDescriptor().getResolvedTypeMapping() ) + collection.getHibernateTypeDescriptor().getResolvedTypeMapping() ) ); } - + collectionType = (CollectionType) collection.getHibernateTypeDescriptor().getResolvedTypeMapping(); role = collection.getAttribute().getRole(); entityName = collection.getContainer().seekEntityBinding().getEntity().getName(); @@ -660,8 +662,8 @@ public abstract class AbstractCollectionPersister // isSet = collection.isSet(); // isSorted = collection.isSorted(); isArray = collectionType.isArrayType(); - isPrimitiveArray = - collectionType.isArrayType() && + isPrimitiveArray = + collectionType.isArrayType() && PrimitiveType.class.isInstance( collection.getPluralAttributeElementBinding() .getHibernateTypeDescriptor() @@ -720,7 +722,7 @@ public abstract class AbstractCollectionPersister // ELEMENT - PluralAttributeElementBinding elementBinding = collection.getPluralAttributeElementBinding(); + // PluralAttributeElementBinding elementBinding = collection.getPluralAttributeElementBinding(); //TODO: is elemNode needed? String elemNode = null; //String elemNode = collection.getElementNodeName(); @@ -792,14 +794,22 @@ public abstract class AbstractCollectionPersister // INDEX AND ROW SELECT hasIndex = collection.getAttribute().getNature().isIndexed(); - // TODO: Fix this when index relational binding is created - //if ( hasIndex ) { - // // NativeSQL: collect index column and auto-aliases - // PluralAttributeIndexBinding indexBinding = ( (IndexedPluralAttributeBinding) collection ).getPluralAttributeIndexBinding(); - // indexType = indexBinding.getHibernateTypeDescriptor().getResolvedTypeMapping(); - //} - //else { - indexContainsFormula = false; + indexContainsFormula = false; + indexNodeName = null; + if ( hasIndex ) { + // NativeSQL: collect index column and auto-aliases + IndexedPluralAttributeBinding indexedBinding = (IndexedPluralAttributeBinding) collection; + // TODO: indexNodeName = indexedBinding.getIndexNodeName(); + PluralAttributeIndexBinding indexBinding = indexedBinding.getPluralAttributeIndexBinding(); + indexType = indexBinding.getHibernateTypeDescriptor().getResolvedTypeMapping(); + baseIndex = indexBinding instanceof ListBinding ? ( ( ListBinding ) indexBinding ).base() : 0; + Column column = (Column) indexBinding.getIndexRelationalValue(); + indexColumnNames = new String[] { column.getQuotedName( dialect ) }; + indexColumnAliases = new String[] { column.getAlias( dialect ) }; + indexFormulaTemplates = new String[1]; + indexFormulas = new String[1]; + indexColumnIsSettable = new boolean[] { true }; + } else { indexColumnIsSettable = null; indexFormulaTemplates = null; indexFormulas = null; @@ -807,8 +817,7 @@ public abstract class AbstractCollectionPersister indexColumnNames = null; indexColumnAliases = null; baseIndex = 0; - indexNodeName = null; - //} + } hasIdentifier = collection.getAttribute().getNature() == PluralAttributeNature.IDBAG; // TODO: fix this when IdBags are supported. @@ -986,7 +995,7 @@ public abstract class AbstractCollectionPersister } return templateString; } - + public void postInstantiate() throws MappingException { initializer = queryLoaderName == null ? createCollectionInitializer( LoadQueryInfluencers.NONE ) : @@ -1777,12 +1786,12 @@ public abstract class AbstractCollectionPersister expectation ); } - if ( st == null ) { +// if ( st == null ) { st = session.getTransactionCoordinator() .getJdbcCoordinator() .getBatch( insertBatchKey ) .getBatchStatement( sql, callable ); - } +// } } else { st = session.getTransactionCoordinator() @@ -2235,7 +2244,7 @@ public abstract class AbstractCollectionPersister /** * Intended for internal use only. In fact really only currently used from * test suite for assertion purposes. - * + * * @return The default collection initializer for this persister/collection. */ public CollectionInitializer getInitializer() {