Skip to content

Commit

Permalink
HHH-6502: Added support for lists to new metamodel up to Binder proce…
Browse files Browse the repository at this point in the history
…ssing. Still need to modify persisters.
  • Loading branch information
jpav committed Apr 9, 2012
1 parent edcc34a commit 1ced278
Show file tree
Hide file tree
Showing 14 changed files with 640 additions and 24 deletions.
Expand Up @@ -45,12 +45,14 @@
import org.hibernate.internal.util.ReflectHelper;
import org.hibernate.internal.util.StringHelper;
import org.hibernate.metamodel.internal.HibernateTypeHelper.ReflectedCollectionJavaTypes;
import org.hibernate.metamodel.internal.source.hbm.ListAttributeSourceImpl;
import org.hibernate.metamodel.spi.binding.AbstractPluralAttributeBinding;
import org.hibernate.metamodel.spi.binding.AttributeBinding;
import org.hibernate.metamodel.spi.binding.AttributeBindingContainer;
import org.hibernate.metamodel.spi.binding.BagBinding;
import org.hibernate.metamodel.spi.binding.BasicAttributeBinding;
import org.hibernate.metamodel.spi.binding.BasicPluralAttributeElementBinding;
import org.hibernate.metamodel.spi.binding.BasicPluralAttributeIndexBinding;
import org.hibernate.metamodel.spi.binding.CompositeAttributeBinding;
import org.hibernate.metamodel.spi.binding.EntityBinding;
import org.hibernate.metamodel.spi.binding.EntityDiscriminator;
Expand All @@ -59,10 +61,12 @@
import org.hibernate.metamodel.spi.binding.IdGenerator;
import org.hibernate.metamodel.spi.binding.IndexedPluralAttributeBinding;
import org.hibernate.metamodel.spi.binding.InheritanceType;
import org.hibernate.metamodel.spi.binding.ListBinding;
import org.hibernate.metamodel.spi.binding.ManyToOneAttributeBinding;
import org.hibernate.metamodel.spi.binding.MetaAttribute;
import org.hibernate.metamodel.spi.binding.PluralAttributeBinding;
import org.hibernate.metamodel.spi.binding.PluralAttributeElementNature;
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.binding.SecondaryTable;
Expand Down Expand Up @@ -107,6 +111,7 @@
import org.hibernate.metamodel.spi.source.MetaAttributeSource;
import org.hibernate.metamodel.spi.source.MetadataImplementor;
import org.hibernate.metamodel.spi.source.Orderable;
import org.hibernate.metamodel.spi.source.PluralAttributeIndexSource;
import org.hibernate.metamodel.spi.source.PluralAttributeKeySource;
import org.hibernate.metamodel.spi.source.PluralAttributeNature;
import org.hibernate.metamodel.spi.source.PluralAttributeSource;
Expand Down Expand Up @@ -292,7 +297,7 @@ private void bindBasicCollectionElement(
bindJdbcDataType( resolvedElementType, ( AbstractValue ) elementBinding.getRelationalValueBindings().get( 0 ).getValue() );
}

private void bindBasicElementSetTablePrimaryKey( final SetBinding attributeBinding ) {
private void bindBasicElementTablePrimaryKey( final PluralAttributeBinding attributeBinding ) {
final PrimaryKey primaryKey = attributeBinding.getPluralAttributeKeyBinding().getCollectionTable().getPrimaryKey();
final ForeignKey foreignKey = attributeBinding.getPluralAttributeKeyBinding().getForeignKey();
final BasicPluralAttributeElementBinding elementBinding =
Expand Down Expand Up @@ -328,16 +333,24 @@ private void bindBasicPluralElementRelationalValues(
}

private void bindCollectionIndex(
final AbstractPluralAttributeBinding attributeBinding,
final PluralAttributeSource attributeSource,
final String defaultElementJavaTypeName ) {
if ( !IndexedPluralAttributeBinding.class.isInstance( attributeBinding ) ) {
return;
}
// final PluralAttributeIndexBinding indexBinding =
( ( IndexedPluralAttributeBinding ) attributeBinding ).getPluralAttributeIndexBinding();
// todo : implement
throw new NotYetImplementedException();
final IndexedPluralAttributeBinding attributeBinding,
final PluralAttributeIndexSource attributeSource,
final String defaultIndexJavaTypeName ) {
IndexedPluralAttributeBinding indexedAttributeBinding = attributeBinding;
final BasicPluralAttributeIndexBinding indexBinding =
( BasicPluralAttributeIndexBinding ) indexedAttributeBinding.getPluralAttributeIndexBinding();
indexBinding.setIndexRelationalValue( bindValues(
indexedAttributeBinding.getContainer(),
attributeSource,
indexedAttributeBinding.getAttribute(),
indexedAttributeBinding.getPluralAttributeKeyBinding().getCollectionTable() ).get( 0 ).getValue() );
bindHibernateTypeDescriptor(
indexBinding.getHibernateTypeDescriptor(),
attributeSource.explicitHibernateTypeSource(),
defaultIndexJavaTypeName );
Type resolvedElementType = heuristicType( indexBinding.getHibernateTypeDescriptor() );
bindHibernateResolvedType( indexBinding.getHibernateTypeDescriptor(), resolvedElementType );
bindJdbcDataType( resolvedElementType, ( AbstractValue ) indexBinding.getIndexRelationalValue() );
}

private void bindCollectionKey(
Expand Down Expand Up @@ -468,13 +481,16 @@ private void bindCollectionTablePrimaryKey(
final AbstractPluralAttributeBinding attributeBinding,
final PluralAttributeSource attributeSource,
final HibernateTypeHelper.ReflectedCollectionJavaTypes reflectedCollectionJavaTypes ) {
PluralAttributeNature pluralAttributeNature = attributeSource.getPluralAttributeNature();
if ( attributeSource.getElementSource().getNature() == org.hibernate.metamodel.spi.source.PluralAttributeElementNature.ONE_TO_MANY
|| attributeSource.getPluralAttributeNature() == PluralAttributeNature.BAG ) {
|| pluralAttributeNature == PluralAttributeNature.BAG ) {
return;
}
if ( attributeBinding.getPluralAttributeElementBinding().getPluralAttributeElementNature() == PluralAttributeElementNature.BASIC ) {
if ( attributeSource.getPluralAttributeNature() == PluralAttributeNature.SET ) {
bindBasicElementSetTablePrimaryKey( ( SetBinding ) attributeBinding );
if ( pluralAttributeNature == PluralAttributeNature.SET ) {
bindBasicElementTablePrimaryKey( attributeBinding );
} else if ( pluralAttributeNature == PluralAttributeNature.LIST ) {
bindIndexedTablePrimaryKey( ( IndexedPluralAttributeBinding ) attributeBinding );
} else {
throw new NotYetImplementedException( "Only Sets with basic elements are supported so far." );
}
Expand Down Expand Up @@ -747,6 +763,19 @@ private void bindIdentifier( final EntityBinding rootEntityBinding, final Identi
}
}

private void bindIndexedTablePrimaryKey( IndexedPluralAttributeBinding attributeBinding ) {
final PrimaryKey primaryKey = attributeBinding.getPluralAttributeKeyBinding().getCollectionTable().getPrimaryKey();
final ForeignKey foreignKey = attributeBinding.getPluralAttributeKeyBinding().getForeignKey();
final PluralAttributeIndexBinding indexBinding = attributeBinding.getPluralAttributeIndexBinding();
for ( final Column foreignKeyColumn : foreignKey.getSourceColumns() ) {
primaryKey.addColumn( foreignKeyColumn );
}
final Value value = indexBinding.getIndexRelationalValue();
if ( value instanceof Column ) {
primaryKey.addColumn( ( Column ) value );
}
}

LocalBindingContext bindingContext() {
return bindingContexts.peek();
}
Expand All @@ -764,6 +793,24 @@ private void bindJdbcDataType( final Type resolvedType, final AbstractValue valu
}
}

private AbstractPluralAttributeBinding bindListAttribute(
final AttributeBindingContainer attributeBindingContainer,
final PluralAttributeSource attributeSource,
PluralAttribute attribute ) {
if ( attribute == null ) {
attribute = attributeBindingContainer.getAttributeContainer().createList( attributeSource.getName() );
}
return attributeBindingContainer.makeListAttributeBinding(
attribute,
pluralAttributeElementNature( attributeSource ),
pluralAttributeKeyBinding( attributeBindingContainer, attributeSource ),
propertyAccessorName( attributeSource ),
attributeSource.isIncludedInOptimisticLocking(),
false,
createMetaAttributeContext( attributeBindingContainer, attributeSource ),
((ListAttributeSourceImpl)attributeSource).getIndexSource().base() );
}

private ManyToOneAttributeBinding bindManyToOneAttribute(
final AttributeBindingContainer attributeBindingContainer,
final ToOneAttributeSource attributeSource,
Expand Down Expand Up @@ -854,6 +901,9 @@ private AbstractPluralAttributeBinding bindPluralAttribute(
} else if ( nature == PluralAttributeNature.SET ) {
attributeBinding = bindSetAttribute( attributeBindingContainer, attributeSource, attribute );
resolvedType = resolveSetType( ( SetBinding ) attributeBinding );
} else if ( nature == PluralAttributeNature.LIST ) {
attributeBinding = bindListAttribute( attributeBindingContainer, attributeSource, attribute );
resolvedType = resolveListType( ( ListBinding ) attributeBinding );
} else {
throw new NotYetImplementedException( nature.toString() );
}
Expand Down Expand Up @@ -894,7 +944,12 @@ private AbstractPluralAttributeBinding bindPluralAttribute(
attributeSource.getElementSource().getNature() ) );
}

bindCollectionIndex( attributeBinding, attributeSource, defaultCollectionIndexJavaTypeName( reflectedCollectionJavaTypes ) );
if (attributeSource instanceof ListAttributeSourceImpl) {
bindCollectionIndex(
(IndexedPluralAttributeBinding) attributeBinding,
( (ListAttributeSourceImpl) attributeSource ).getIndexSource(),
defaultCollectionIndexJavaTypeName( reflectedCollectionJavaTypes ) );
}

bindCollectionTablePrimaryKey( attributeBinding, attributeSource, reflectedCollectionJavaTypes );
metadata.addCollection( attributeBinding );
Expand Down Expand Up @@ -1540,6 +1595,17 @@ private Type resolveCustomCollectionType( PluralAttributeBinding pluralAttribute
pluralAttributeBinding.getPluralAttributeElementBinding().getPluralAttributeElementNature() == PluralAttributeElementNature.COMPOSITE );
}

private Type resolveListType( ListBinding listBinding ) {
if ( listBinding.getHibernateTypeDescriptor().getExplicitTypeName() != null ) {
return resolveCustomCollectionType( listBinding );
} else {
return metadata.getTypeResolver().getTypeFactory().list(
listBinding.getAttribute().getRole(),
listBinding.getReferencedPropertyName(),
listBinding.getPluralAttributeElementBinding().getPluralAttributeElementNature() == PluralAttributeElementNature.COMPOSITE );
}
}

private Type resolveSetType( SetBinding setBinding ) {
if ( setBinding.getHibernateTypeDescriptor().getExplicitTypeName() != null ) {
return resolveCustomCollectionType( setBinding );
Expand Down
Expand Up @@ -180,7 +180,13 @@ else if ( JaxbSetElement.class.isInstance( attributeElement ) ) {
);
}
else if ( JaxbListElement.class.isInstance( attributeElement ) ) {
// todo : implement
results.add(
new ListAttributeSourceImpl(
sourceMappingDocument(),
JaxbListElement.class.cast( attributeElement ),
this
)
);
}
else if ( JaxbMapElement.class.isInstance( attributeElement ) ) {
// todo : implement
Expand Down
@@ -0,0 +1,69 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2011, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.metamodel.internal.source.hbm;

import org.hibernate.internal.jaxb.mapping.hbm.JaxbListElement;
import org.hibernate.metamodel.spi.source.AttributeSourceContainer;
import org.hibernate.metamodel.spi.source.PluralAttributeIndexSource;
import org.hibernate.metamodel.spi.source.PluralAttributeNature;

/**
*
*/
public class ListAttributeSourceImpl extends AbstractPluralAttributeSourceImpl {

private final PluralAttributeIndexSource indexSource;

/**
* @param sourceMappingDocument
* @param listElement
* @param container
*/
public ListAttributeSourceImpl(
MappingDocument sourceMappingDocument,
JaxbListElement listElement,
AttributeSourceContainer container ) {
super( sourceMappingDocument, listElement, container );
this.indexSource = new PluralAttributeIndexSourceImpl( sourceMappingDocument(), listElement.getListIndex(), container );
}

public PluralAttributeIndexSource getIndexSource() {
return indexSource;
}

@Override
public JaxbListElement getPluralAttributeElement() {
return ( JaxbListElement ) super.getPluralAttributeElement();
}

/**
* {@inheritDoc}
*
* @see org.hibernate.metamodel.spi.source.PluralAttributeSource#getPluralAttributeNature()
*/
@Override
public PluralAttributeNature getPluralAttributeNature() {
return PluralAttributeNature.LIST;
}
}

0 comments on commit 1ced278

Please sign in to comment.