From 2b694d7a86de7db604b668264e1c7b6f55fef2fe Mon Sep 17 00:00:00 2001 From: Hardy Ferentschik Date: Wed, 18 May 2011 14:21:23 +0200 Subject: [PATCH] HHH-6171 Switching to Map as type parameters in HibernateTypeDescriptor (needs maybe to change to Map). Properties is the wrong class to use. Also fixed several variable and method typos, eg updatable instead of updateable. And we are talking about a discriminator not descriminator. --- .../binding/AbstractAttributeBinding.java | 30 +++--- .../binding/HibernateTypeDescriptor.java | 8 +- .../metamodel/binding/KeyValueBinding.java | 4 +- .../metamodel/binding/MappingDefaults.java | 12 ++- .../binding/SimpleAttributeBinding.java | 27 +++--- .../binding/state/AttributeBindingState.java | 3 +- .../state/SimpleAttributeBindingState.java | 2 +- .../source/annotations/AnnotationBinder.java | 36 ++++--- .../annotations/entity/ConfiguredClass.java | 2 +- .../entity/ConfiguredClassHierarchy.java | 2 +- .../annotations/entity/EntityBinder.java | 2 + .../annotations/entity/MappedAttribute.java | 53 +++++++--- .../binding/AttributeBindingStateImpl.java | 12 +-- .../AttributeColumnRelationalState.java | 8 +- .../source/hbm/HibernateMappingBinder.java | 2 +- .../AbstractHbmAttributeBindingState.java | 7 +- .../binding/HbmDiscriminatorBindingState.java | 6 +- .../HbmManyToOneAttributeBindingState.java | 2 +- .../HbmSimpleAttributeBindingState.java | 26 ++--- .../source/internal/MetadataImpl.java | 4 +- .../util/GenericTypeDiscoveryTest.java | 8 +- .../annotations/util/TypeDiscoveryTest.java | 96 +++++++++++++++++++ 22 files changed, 238 insertions(+), 114 deletions(-) create mode 100644 hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/util/TypeDiscoveryTest.java diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/binding/AbstractAttributeBinding.java b/hibernate-core/src/main/java/org/hibernate/metamodel/binding/AbstractAttributeBinding.java index 7b06986e94..5cfdb2f9eb 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/binding/AbstractAttributeBinding.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/binding/AbstractAttributeBinding.java @@ -31,6 +31,7 @@ import java.util.Map; import java.util.Set; import org.hibernate.MappingException; +import org.hibernate.metamodel.binding.state.AttributeBindingState; import org.hibernate.metamodel.domain.Attribute; import org.hibernate.metamodel.domain.MetaAttribute; import org.hibernate.metamodel.relational.Column; @@ -39,11 +40,10 @@ import org.hibernate.metamodel.relational.SimpleValue; import org.hibernate.metamodel.relational.TableSpecification; import org.hibernate.metamodel.relational.Tuple; import org.hibernate.metamodel.relational.Value; -import org.hibernate.metamodel.binding.state.AttributeBindingState; -import org.hibernate.metamodel.relational.state.ValueCreator; import org.hibernate.metamodel.relational.state.SimpleValueRelationalState; -import org.hibernate.metamodel.relational.state.ValueRelationalState; import org.hibernate.metamodel.relational.state.TupleRelationalState; +import org.hibernate.metamodel.relational.state.ValueCreator; +import org.hibernate.metamodel.relational.state.ValueRelationalState; /** * TODO : javadoc @@ -51,11 +51,9 @@ import org.hibernate.metamodel.relational.state.TupleRelationalState; * @author Steve Ebersole */ public abstract class AbstractAttributeBinding implements AttributeBinding { - private final HibernateTypeDescriptor hibernateTypeDescriptor = new HibernateTypeDescriptor(); private final EntityBinding entityBinding; - private final Set entityReferencingAttributeBindings = - new HashSet(); + private final Set entityReferencingAttributeBindings = new HashSet(); private Attribute attribute; private Value value; @@ -83,7 +81,7 @@ public abstract class AbstractAttributeBinding implements AttributeBinding { isAlternateUniqueKey = state.isAlternateUniqueKey(); cascade = state.getCascade(); optimisticLockable = state.isOptimisticLockable(); - nodeName = state.getNodeName() ; + nodeName = state.getNodeName(); metaAttributes = state.getMetaAttributes(); } @@ -125,14 +123,15 @@ public abstract class AbstractAttributeBinding implements AttributeBinding { // TODO: not sure I like this here... if ( isPrimaryKey() ) { if ( SimpleValue.class.isInstance( value ) ) { - if ( ! Column.class.isInstance( value ) ) { - // this should never ever happen.. + if ( !Column.class.isInstance( value ) ) { + // this should never ever happen.. throw new MappingException( "Simple ID is not a column." ); } entityBinding.getBaseTable().getPrimaryKey().addColumn( Column.class.cast( value ) ); } else { - for ( SimpleValueRelationalState val : TupleRelationalState.class.cast( state ).getRelationalStates() ) { + for ( SimpleValueRelationalState val : TupleRelationalState.class.cast( state ) + .getRelationalStates() ) { if ( Column.class.isInstance( val ) ) { entityBinding.getBaseTable().getPrimaryKey().addColumn( Column.class.cast( val ) ); } @@ -169,8 +168,8 @@ public abstract class AbstractAttributeBinding implements AttributeBinding { return value == null ? Collections.emptyList() : value instanceof Tuple - ? ( (Tuple) value ).values() - : Collections.singletonList( (SimpleValue) value ); + ? ( (Tuple) value ).values() + : Collections.singletonList( (SimpleValue) value ); } @Override @@ -220,9 +219,9 @@ public abstract class AbstractAttributeBinding implements AttributeBinding { public boolean[] getColumnInsertability() { List tmp = new ArrayList(); for ( SimpleValue simpleValue : getValues() ) { - tmp.add( ! ( simpleValue instanceof DerivedValue ) ); + tmp.add( !( simpleValue instanceof DerivedValue ) ); } - boolean[] rtn = new boolean[ tmp.size() ]; + boolean[] rtn = new boolean[tmp.size()]; int i = 0; for ( Boolean insertable : tmp ) { rtn[i++] = insertable.booleanValue(); @@ -253,11 +252,10 @@ public abstract class AbstractAttributeBinding implements AttributeBinding { } public void validate() { - if ( ! entityReferencingAttributeBindings.isEmpty() ) { + if ( !entityReferencingAttributeBindings.isEmpty() ) { // TODO; validate that this AttributeBinding can be a target of an entity reference // (e.g., this attribute is the primary key or there is a unique-key) // can a unique attribute be used as a target? if so, does it need to be non-null? } } - } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/binding/HibernateTypeDescriptor.java b/hibernate-core/src/main/java/org/hibernate/metamodel/binding/HibernateTypeDescriptor.java index 428ced9f0d..d65925f1b6 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/binding/HibernateTypeDescriptor.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/binding/HibernateTypeDescriptor.java @@ -23,7 +23,7 @@ */ package org.hibernate.metamodel.binding; -import java.util.Properties; +import java.util.Map; import org.hibernate.type.Type; @@ -35,7 +35,7 @@ import org.hibernate.type.Type; public class HibernateTypeDescriptor { private String typeName; private Type explicitType; - private Properties typeParameters; + private Map typeParameters; public String getTypeName() { return typeName; @@ -55,12 +55,12 @@ public class HibernateTypeDescriptor { this.explicitType = explicitType; } - public Properties getTypeParameters() { + public Map getTypeParameters() { return typeParameters; } /* package-protected */ - void setTypeParameters(Properties typeParameters) { + void setTypeParameters(Map typeParameters) { this.typeParameters = typeParameters; } } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/binding/KeyValueBinding.java b/hibernate-core/src/main/java/org/hibernate/metamodel/binding/KeyValueBinding.java index 50cd12250a..e1d2489769 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/binding/KeyValueBinding.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/binding/KeyValueBinding.java @@ -29,9 +29,9 @@ package org.hibernate.metamodel.binding; * @author Steve Ebersole */ public interface KeyValueBinding extends AttributeBinding { - public boolean isKeyCasadeDeleteEnabled(); + public boolean isKeyCascadeDeleteEnabled(); public String getUnsavedValue(); - public boolean isUpdateable(); + public boolean isUpdatable(); } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/binding/MappingDefaults.java b/hibernate-core/src/main/java/org/hibernate/metamodel/binding/MappingDefaults.java index d080f5da3e..1c0ae529db 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/binding/MappingDefaults.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/binding/MappingDefaults.java @@ -34,14 +34,24 @@ import org.hibernate.service.ServiceRegistry; */ public interface MappingDefaults { Map getMappingMetas(); + String getPackageName(); + String getDefaultSchemaName(); + String getDefaultCatalogName(); + String getDefaultIdColumnName(); - String getDefaultDescriminatorColumnName(); + + String getDefaultDiscriminatorColumnName(); + String getDefaultCascade(); + String getDefaultAccess(); + boolean isDefaultLazy(); + ServiceRegistry getServiceRegistry(); + NamingStrategy getNamingStrategy(); } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/binding/SimpleAttributeBinding.java b/hibernate-core/src/main/java/org/hibernate/metamodel/binding/SimpleAttributeBinding.java index d601715b37..f458f986ec 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/binding/SimpleAttributeBinding.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/binding/SimpleAttributeBinding.java @@ -25,8 +25,8 @@ package org.hibernate.metamodel.binding; import org.hibernate.mapping.PropertyGeneration; import org.hibernate.metamodel.binding.state.SimpleAttributeBindingState; -import org.hibernate.metamodel.relational.state.ValueRelationalState; import org.hibernate.metamodel.relational.state.ColumnRelationalState; +import org.hibernate.metamodel.relational.state.ValueRelationalState; /** * TODO : javadoc @@ -37,8 +37,8 @@ public class SimpleAttributeBinding extends AbstractAttributeBinding implements private final boolean forceNonNullable; private final boolean forceUnique; private boolean insertable; - private boolean updateable; - private boolean keyCasadeDeleteEnabled; + private boolean updatable; + private boolean keyCascadeDeleteEnabled; private String unsavedValue; private PropertyGeneration generation; @@ -51,8 +51,8 @@ public class SimpleAttributeBinding extends AbstractAttributeBinding implements public final SimpleAttributeBinding initialize(SimpleAttributeBindingState state) { super.initialize( state ); insertable = state.isInsertable(); - updateable = state.isUpdateable(); - keyCasadeDeleteEnabled = state.isKeyCascadeDeleteEnabled(); + updatable = state.isUpdatable(); + keyCascadeDeleteEnabled = state.isKeyCascadeDeleteEnabled(); unsavedValue = state.getUnsavedValue(); generation = state.getPropertyGeneration() == null ? PropertyGeneration.NEVER : state.getPropertyGeneration(); return this; @@ -80,21 +80,21 @@ public class SimpleAttributeBinding extends AbstractAttributeBinding implements this.insertable = insertable; } - public boolean isUpdateable() { - return updateable; + public boolean isUpdatable() { + return updatable; } - protected void setUpdateable(boolean updateable) { - this.updateable = updateable; + protected void setUpdatable(boolean updatable) { + this.updatable = updatable; } @Override - public boolean isKeyCasadeDeleteEnabled() { - return keyCasadeDeleteEnabled; + public boolean isKeyCascadeDeleteEnabled() { + return keyCascadeDeleteEnabled; } - public void setKeyCasadeDeleteEnabled(boolean keyCasadeDeleteEnabled) { - this.keyCasadeDeleteEnabled = keyCasadeDeleteEnabled; + public void setKeyCascadeDeleteEnabled(boolean keyCascadeDeleteEnabled) { + this.keyCascadeDeleteEnabled = keyCascadeDeleteEnabled; } @Override @@ -117,5 +117,4 @@ public class SimpleAttributeBinding extends AbstractAttributeBinding implements public PropertyGeneration getGeneration() { return generation; } - } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/binding/state/AttributeBindingState.java b/hibernate-core/src/main/java/org/hibernate/metamodel/binding/state/AttributeBindingState.java index 3a9bd19782..845e895f50 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/binding/state/AttributeBindingState.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/binding/state/AttributeBindingState.java @@ -24,7 +24,6 @@ package org.hibernate.metamodel.binding.state; import java.util.Map; -import java.util.Properties; import org.hibernate.metamodel.domain.MetaAttribute; @@ -36,7 +35,7 @@ public interface AttributeBindingState { String getTypeName(); - Properties getTypeParameters(); + Map getTypeParameters(); boolean isLazy(); diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/binding/state/SimpleAttributeBindingState.java b/hibernate-core/src/main/java/org/hibernate/metamodel/binding/state/SimpleAttributeBindingState.java index 21a8a4253d..fadcadd2f6 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/binding/state/SimpleAttributeBindingState.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/binding/state/SimpleAttributeBindingState.java @@ -31,7 +31,7 @@ import org.hibernate.mapping.PropertyGeneration; public interface SimpleAttributeBindingState extends AttributeBindingState { boolean isInsertable(); - boolean isUpdateable(); + boolean isUpdatable(); boolean isKeyCascadeDeleteEnabled(); diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/AnnotationBinder.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/AnnotationBinder.java index 98ab6e4a37..56ecb82edd 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/AnnotationBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/AnnotationBinder.java @@ -23,7 +23,6 @@ */ package org.hibernate.metamodel.source.annotations; -import java.util.Iterator; import java.util.Set; import org.jboss.jandex.Index; @@ -44,36 +43,37 @@ import org.hibernate.metamodel.source.internal.MetadataImpl; * are added to the annotation index. * * @author Hardy Ferentschik - * @todo On top of the index we probably needs to pass some sort of XMLContext for global configuration data - * @todo The annotation index should really be passed at construction time */ public class AnnotationBinder { private static final Logger log = LoggerFactory.getLogger( AnnotationBinder.class ); private final MetadataImpl metadata; + private final Index index; - public AnnotationBinder(MetadataImpl metadata) { + public AnnotationBinder(MetadataImpl metadata, Index index) { this.metadata = metadata; + this.index = index; } - public void bind(Index annotationIndex) { - preEntityBindings( annotationIndex ); - bindMappedClasses( annotationIndex ); - postEntityBindings( annotationIndex ); + public void bind() { + preEntityBindings(); + bindMappedClasses(); + postEntityBindings(); } /** - * Binds global configuration data prior to entity binding. This includes generators and type definitions - * - * @param annotationIndex the annotation repository/index + * Binds global configuration data prior to entity binding. This includes generators and type definitions. */ - private void preEntityBindings(Index annotationIndex) { - FetchProfileBinder.bind( metadata, annotationIndex ); + private void preEntityBindings() { + FetchProfileBinder.bind( metadata, index ); } - private void bindMappedClasses(Index annotationIndex) { + /** + * Does the actual entity binding (see {@link org.hibernate.metamodel.binding.EntityBinding}. + */ + private void bindMappedClasses() { // need to order our annotated entities into an order we can process Set hierarchies = ConfiguredClassHierarchyBuilder.createEntityHierarchies( - annotationIndex, metadata.getServiceRegistry() + index, metadata.getServiceRegistry() ); // now we process each hierarchy one at the time @@ -89,11 +89,9 @@ public class AnnotationBinder { /** * Binds global configuration data post entity binding. This includes mappings which live outside of the configuration for a single * entity or entity hierarchy, for example sequence generators, fetch profiles, etc - * - * @param annotationIndex the annotation repository/index */ - private void postEntityBindings(Index annotationIndex) { - TableBinder.bind( metadata, annotationIndex ); + private void postEntityBindings() { + TableBinder.bind( metadata, index ); } } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/entity/ConfiguredClass.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/entity/ConfiguredClass.java index 0cca032de6..f032c4533c 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/entity/ConfiguredClass.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/entity/ConfiguredClass.java @@ -380,7 +380,7 @@ public class ConfiguredClass { final Map> annotations = JandexHelper.getMemberAnnotations( classInfo, member.getName() ); - return MappedAttribute.createMappedAttribute( name, (Class) type, annotations ); + return MappedAttribute.createMappedAttribute( name, ( (Class) type ).getName(), annotations ); } private Type findResolvedType(String name, ResolvedMember[] resolvedMembers) { diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/entity/ConfiguredClassHierarchy.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/entity/ConfiguredClassHierarchy.java index e4b1723620..b5fadf47a9 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/entity/ConfiguredClassHierarchy.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/entity/ConfiguredClassHierarchy.java @@ -196,7 +196,7 @@ public class ConfiguredClassHierarchy implements Iterable { private AccessType throwIdNotFoundAnnotationException(List classes) { StringBuilder builder = new StringBuilder(); - builder.append( "Unable to determine identifier attribute for class hierarchy " ); + builder.append( "Unable to determine identifier attribute for class hierarchy consisting of the classe(s) " ); builder.append( hierarchyListString( classes ) ); throw new AnnotationException( builder.toString() ); } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/entity/EntityBinder.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/entity/EntityBinder.java index 79a0ec9ffa..61d9f7a880 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/entity/EntityBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/entity/EntityBinder.java @@ -85,11 +85,13 @@ public class EntityBinder { bindJpaCaching( entityBinding ); bindHibernateCaching( entityBinding ); + // take care of the id, attributes and relations if ( configuredClass.isRoot() ) { bindId( entityBinding ); } bindAttributes( entityBinding ); + // last, but not least we register the new EntityBinding with the metadata meta.addEntity( entityBinding ); } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/entity/MappedAttribute.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/entity/MappedAttribute.java index 57552efcda..8aae9c0869 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/entity/MappedAttribute.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/entity/MappedAttribute.java @@ -23,6 +23,7 @@ */ package org.hibernate.metamodel.source.annotations.entity; +import java.util.HashMap; import java.util.List; import java.util.Map; import javax.persistence.DiscriminatorType; @@ -43,15 +44,20 @@ import org.hibernate.metamodel.source.annotations.util.JandexHelper; * @author Hardy Ferentschik */ public class MappedAttribute implements Comparable { - private final String name; - private final Class type; private final Map> annotations; + + private final String name; + + private final String type; + private final Map typeParameters; + private final ColumnValues columnValues; + private final boolean isId; private final boolean isVersioned; private final boolean isDiscriminator; - static MappedAttribute createMappedAttribute(String name, Class type, Map> annotations) { + static MappedAttribute createMappedAttribute(String name, String type, Map> annotations) { return new MappedAttribute( name, type, annotations, false ); } @@ -68,7 +74,7 @@ public class MappedAttribute implements Comparable { annotations, JPADotNames.DISCRIMINATOR_COLUMN ); String name = DiscriminatorColumnValues.DEFAULT_DISCRIMINATOR_COLUMN_NAME; - Class type = String.class; // string is the discriminator default + String type = String.class.toString(); // string is the discriminator default if ( discriminatorOptionsAnnotation != null ) { name = discriminatorOptionsAnnotation.value( "name" ).asString(); @@ -77,15 +83,15 @@ public class MappedAttribute implements Comparable { ); switch ( discriminatorType ) { case STRING: { - type = String.class; + type = String.class.toString(); break; } case CHAR: { - type = Character.class; + type = Character.class.toString(); break; } case INTEGER: { - type = Integer.class; + type = Integer.class.toString(); break; } default: { @@ -96,12 +102,14 @@ public class MappedAttribute implements Comparable { return new MappedAttribute( name, type, discriminatorAnnotations, true ); } - private MappedAttribute(String name, Class type, Map> annotations, boolean isDiscriminator) { + private MappedAttribute(String name, String type, Map> annotations, boolean isDiscriminator) { this.name = name; - this.type = type; this.annotations = annotations; this.isDiscriminator = isDiscriminator; + this.typeParameters = new HashMap(); + this.type = determineType( type, typeParameters ); + AnnotationInstance idAnnotation = JandexHelper.getSingleAnnotation( annotations, JPADotNames.ID ); isId = idAnnotation != null; @@ -127,7 +135,7 @@ public class MappedAttribute implements Comparable { return name; } - public final Class getType() { + public final String getType() { return type; } @@ -135,6 +143,10 @@ public class MappedAttribute implements Comparable { return columnValues; } + public Map getTypeParameters() { + return typeParameters; + } + public boolean isId() { return isId; } @@ -155,7 +167,7 @@ public class MappedAttribute implements Comparable { * @return Returns the annotation with the specified name or {@code null}. Note, since these are the * annotations defined on a single attribute there can never be more than one. */ - public final AnnotationInstance annotations(DotName annotationDotName) { + public final AnnotationInstance getIfExists(DotName annotationDotName) { if ( annotations.containsKey( annotationDotName ) ) { List instanceList = annotations.get( annotationDotName ); if ( instanceList.size() > 1 ) { @@ -176,12 +188,27 @@ public class MappedAttribute implements Comparable { @Override public String toString() { final StringBuilder sb = new StringBuilder(); - sb.append( "MappedProperty" ); + sb.append( "MappedAttribute" ); sb.append( "{name='" ).append( name ).append( '\'' ); - sb.append( ", type=" ).append( type ); + sb.append( ", type='" ).append( type ).append( '\'' ); + sb.append( ", isId=" ).append( isId ); + sb.append( ", isVersioned=" ).append( isVersioned ); + sb.append( ", isDiscriminator=" ).append( isDiscriminator ); sb.append( '}' ); return sb.toString(); } + + /** + * We need to check whether the is an explicit type specified via {@link org.hibernate.annotations.Type}. + * + * @param type the type specified via the constructor + * @param typeParameters map for type parameters in case there are any + * + * @return the final type for this mapped attribute + */ + private String determineType(String type, Map typeParameters) { + return type; + } } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/entity/state/binding/AttributeBindingStateImpl.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/entity/state/binding/AttributeBindingStateImpl.java index b58dd2b59b..bfc67180dc 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/entity/state/binding/AttributeBindingStateImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/entity/state/binding/AttributeBindingStateImpl.java @@ -24,7 +24,6 @@ package org.hibernate.metamodel.source.annotations.entity.state.binding; import java.util.Map; -import java.util.Properties; import org.hibernate.mapping.PropertyGeneration; import org.hibernate.metamodel.binding.state.SimpleAttributeBindingState; @@ -40,13 +39,12 @@ public class AttributeBindingStateImpl implements SimpleAttributeBindingState { private final MappedAttribute mappedAttribute; private final PropertyGeneration propertyGeneration = null; private final String typeName; - private final Properties typeParameters; + private final Map typeParameters; public AttributeBindingStateImpl(MappedAttribute mappedAttribute) { this.mappedAttribute = mappedAttribute; - typeName = mappedAttribute.getType().getName(); - // TODO: implement.... - typeParameters = null; + typeName = mappedAttribute.getType(); + typeParameters = mappedAttribute.getTypeParameters(); } @Override @@ -73,7 +71,7 @@ public class AttributeBindingStateImpl implements SimpleAttributeBindingState { } @Override - public boolean isUpdateable() { + public boolean isUpdatable() { return false; //To change body of implemented methods use File | Settings | File Templates. } @@ -93,7 +91,7 @@ public class AttributeBindingStateImpl implements SimpleAttributeBindingState { } @Override - public Properties getTypeParameters() { + public Map getTypeParameters() { return typeParameters; } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/entity/state/relational/AttributeColumnRelationalState.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/entity/state/relational/AttributeColumnRelationalState.java index c513142c9a..d11edd6753 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/entity/state/relational/AttributeColumnRelationalState.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/entity/state/relational/AttributeColumnRelationalState.java @@ -155,13 +155,13 @@ public class AttributeColumnRelationalState implements ColumnRelationalState { List allColumnTransformerAnnotations = new ArrayList(); // not quite sure about the usefulness of @ColumnTransformers (HF) - AnnotationInstance columnTransformersAnnotations = attribute.annotations( HibernateDotNames.COLUMN_TRANSFORMERS ); + AnnotationInstance columnTransformersAnnotations = attribute.getIfExists( HibernateDotNames.COLUMN_TRANSFORMERS ); if ( columnTransformersAnnotations != null ) { AnnotationInstance[] annotationInstances = allColumnTransformerAnnotations.get( 0 ).value().asNestedArray(); allColumnTransformerAnnotations.addAll( Arrays.asList( annotationInstances ) ); } - AnnotationInstance columnTransformerAnnotation = attribute.annotations( HibernateDotNames.COLUMN_TRANSFORMER ); + AnnotationInstance columnTransformerAnnotation = attribute.getIfExists( HibernateDotNames.COLUMN_TRANSFORMER ); if ( columnTransformerAnnotation != null ) { allColumnTransformerAnnotations.add( columnTransformerAnnotation ); } @@ -196,7 +196,7 @@ public class AttributeColumnRelationalState implements ColumnRelationalState { private String parseCheckAnnotation(MappedAttribute attribute) { String checkCondition = null; - AnnotationInstance checkAnnotation = attribute.annotations( HibernateDotNames.CHECK ); + AnnotationInstance checkAnnotation = attribute.getIfExists( HibernateDotNames.CHECK ); if ( checkAnnotation != null ) { checkCondition = checkAnnotation.value( "constraints" ).toString(); } @@ -205,7 +205,7 @@ public class AttributeColumnRelationalState implements ColumnRelationalState { private Set parseIndexAnnotation(MappedAttribute attribute) { Set indexNames = new HashSet(); - AnnotationInstance indexAnnotation = attribute.annotations( HibernateDotNames.INDEX ); + AnnotationInstance indexAnnotation = attribute.getIfExists( HibernateDotNames.INDEX ); if ( indexAnnotation != null ) { String indexName = indexAnnotation.value( "name" ).toString(); indexNames.add( indexName ); diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/HibernateMappingBinder.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/HibernateMappingBinder.java index 7ba07a08b5..5221233eb8 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/HibernateMappingBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/HibernateMappingBinder.java @@ -112,7 +112,7 @@ public class HibernateMappingBinder implements MappingDefaults { } - public String getDefaultDescriminatorColumnName() { + public String getDefaultDiscriminatorColumnName() { return DEFAULT_DISCRIMINATOR_COLUMN_NAME; } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/state/binding/AbstractHbmAttributeBindingState.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/state/binding/AbstractHbmAttributeBindingState.java index 7dcabd8c7f..96e74b6426 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/state/binding/AbstractHbmAttributeBindingState.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/state/binding/AbstractHbmAttributeBindingState.java @@ -24,15 +24,14 @@ package org.hibernate.metamodel.source.hbm.state.binding; import java.util.Map; -import java.util.Properties; import org.hibernate.MappingException; import org.hibernate.internal.util.ReflectHelper; import org.hibernate.mapping.PropertyGeneration; import org.hibernate.metamodel.binding.MappingDefaults; +import org.hibernate.metamodel.binding.state.AttributeBindingState; import org.hibernate.metamodel.domain.MetaAttribute; import org.hibernate.metamodel.source.util.MappingHelper; -import org.hibernate.metamodel.binding.state.AttributeBindingState; /** * @author Gail Badner @@ -63,7 +62,7 @@ public abstract class AbstractHbmAttributeBindingState implements AttributeBindi this.ownerClassName = ownerClassName; this.attributeName = attributeName; this.defaults = defaults; - this.nodeName = nodeName; + this.nodeName = nodeName; this.metaAttributes = metaAttributes; this.accessorName = accessorName; this.isOptimisticLockable = isOptimisticLockable; @@ -121,7 +120,7 @@ public abstract class AbstractHbmAttributeBindingState implements AttributeBindi return null; } - public Properties getTypeParameters() { + public Map getTypeParameters() { return null; } } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/state/binding/HbmDiscriminatorBindingState.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/state/binding/HbmDiscriminatorBindingState.java index ee53cc8850..c15ffa1bc1 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/state/binding/HbmDiscriminatorBindingState.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/state/binding/HbmDiscriminatorBindingState.java @@ -24,8 +24,8 @@ package org.hibernate.metamodel.source.hbm.state.binding; import org.hibernate.metamodel.binding.MappingDefaults; -import org.hibernate.metamodel.source.hbm.xml.mapping.XMLHibernateMapping.XMLClass.XMLDiscriminator; import org.hibernate.metamodel.binding.state.DiscriminatorBindingState; +import org.hibernate.metamodel.source.hbm.xml.mapping.XMLHibernateMapping.XMLClass.XMLDiscriminator; /** * @author Gail Badner @@ -41,7 +41,7 @@ public class HbmDiscriminatorBindingState extends AbstractHbmAttributeBindingSta // Discriminator.getName() is not defined, so the attribute will always be // defaults.getDefaultDescriminatorColumnName() super( - ownerClassName, defaults.getDefaultDescriminatorColumnName(), defaults, null, null, null, true + ownerClassName, defaults.getDefaultDiscriminatorColumnName(), defaults, null, null, null, true ); this.discriminator = discriminator; } @@ -66,7 +66,7 @@ public class HbmDiscriminatorBindingState extends AbstractHbmAttributeBindingSta return discriminator.isInsert(); } - public boolean isUpdateable() { + public boolean isUpdatable() { return false; } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/state/binding/HbmManyToOneAttributeBindingState.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/state/binding/HbmManyToOneAttributeBindingState.java index a596849d46..934465c769 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/state/binding/HbmManyToOneAttributeBindingState.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/state/binding/HbmManyToOneAttributeBindingState.java @@ -160,7 +160,7 @@ public class HbmManyToOneAttributeBindingState return isInsertable; } - public boolean isUpdateable() { + public boolean isUpdatable() { return isUpdateable; } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/state/binding/HbmSimpleAttributeBindingState.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/state/binding/HbmSimpleAttributeBindingState.java index 0559f7f66b..10563ade7a 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/state/binding/HbmSimpleAttributeBindingState.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/state/binding/HbmSimpleAttributeBindingState.java @@ -23,12 +23,13 @@ */ package org.hibernate.metamodel.source.hbm.state.binding; +import java.util.HashMap; import java.util.Map; -import java.util.Properties; import org.hibernate.MappingException; import org.hibernate.mapping.PropertyGeneration; import org.hibernate.metamodel.binding.MappingDefaults; +import org.hibernate.metamodel.binding.state.SimpleAttributeBindingState; import org.hibernate.metamodel.domain.MetaAttribute; import org.hibernate.metamodel.source.hbm.HbmHelper; import org.hibernate.metamodel.source.hbm.xml.mapping.XMLHibernateMapping.XMLClass.XMLId; @@ -37,7 +38,6 @@ import org.hibernate.metamodel.source.hbm.xml.mapping.XMLHibernateMapping.XMLCla import org.hibernate.metamodel.source.hbm.xml.mapping.XMLParamElement; import org.hibernate.metamodel.source.hbm.xml.mapping.XMLPropertyElement; import org.hibernate.metamodel.source.util.MappingHelper; -import org.hibernate.metamodel.binding.state.SimpleAttributeBindingState; /** * @author Gail Badner @@ -45,12 +45,12 @@ import org.hibernate.metamodel.binding.state.SimpleAttributeBindingState; public class HbmSimpleAttributeBindingState extends AbstractHbmAttributeBindingState implements SimpleAttributeBindingState { private final String typeName; - private final Properties typeParameters = new Properties(); + private final Map typeParameters = new HashMap(); private final boolean isLazy; private final PropertyGeneration propertyGeneration; private final boolean isInsertable; - private final boolean isUpdateable; + private final boolean isUpdatable; public HbmSimpleAttributeBindingState( String ownerClassName, @@ -69,7 +69,7 @@ public class HbmSimpleAttributeBindingState extends AbstractHbmAttributeBindingS this.isLazy = false; if ( id.getTypeAttribute() != null ) { - typeName = maybeConvertToTypeDefName( id.getTypeAttribute(), defaults ); + typeName = maybeConvertToTypeDefName( id.getTypeAttribute(), defaults ); } else if ( id.getType() != null ) { typeName = maybeConvertToTypeDefName( id.getType().getName(), defaults ); @@ -82,7 +82,7 @@ public class HbmSimpleAttributeBindingState extends AbstractHbmAttributeBindingS this.propertyGeneration = PropertyGeneration.parse( null ); this.isInsertable = true; - this.isUpdateable = false; + this.isUpdatable = false; } private static String maybeConvertToTypeDefName(String typeName, MappingDefaults defaults) { @@ -121,7 +121,7 @@ public class HbmSimpleAttributeBindingState extends AbstractHbmAttributeBindingS throw new MappingException( "'generated' attribute cannot be 'insert' for versioning property" ); } this.isInsertable = MappingHelper.getBooleanValue( version.isInsert(), true ); - this.isUpdateable = true; + this.isUpdatable = true; } public HbmSimpleAttributeBindingState( @@ -152,7 +152,7 @@ public class HbmSimpleAttributeBindingState extends AbstractHbmAttributeBindingS throw new MappingException( "'generated' attribute cannot be 'insert' for versioning property" ); } this.isInsertable = true; //TODO: is this right???? - this.isUpdateable = true; + this.isUpdatable = true; } public HbmSimpleAttributeBindingState( @@ -197,10 +197,10 @@ public class HbmSimpleAttributeBindingState extends AbstractHbmAttributeBindingS property.getName() ); } - isUpdateable = false; + isUpdatable = false; } else { - isUpdateable = MappingHelper.getBooleanValue( property.isUpdate(), true ); + isUpdatable = MappingHelper.getBooleanValue( property.isUpdate(), true ); } if ( property.getTypeAttribute() != null ) { @@ -241,7 +241,7 @@ public class HbmSimpleAttributeBindingState extends AbstractHbmAttributeBindingS return typeName; } - public Properties getTypeParameters() { + public Map getTypeParameters() { return typeParameters; } @@ -257,8 +257,8 @@ public class HbmSimpleAttributeBindingState extends AbstractHbmAttributeBindingS return isInsertable; } - public boolean isUpdateable() { - return isUpdateable; + public boolean isUpdatable() { + return isUpdatable; } public String getCascade() { diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/internal/MetadataImpl.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/internal/MetadataImpl.java index 2ce2faa78d..a024b44ad3 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/internal/MetadataImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/internal/MetadataImpl.java @@ -134,8 +134,8 @@ public class MetadataImpl implements Metadata, MetadataImplementor, Serializable index = ormParser.parseAndUpdateIndex( mappings, index ); // create the annotation binder and pass it the final annotation index - final AnnotationBinder annotationBinder = new AnnotationBinder( this ); - annotationBinder.bind( index ); + final AnnotationBinder annotationBinder = new AnnotationBinder( this, index ); + annotationBinder.bind(); } /** diff --git a/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/util/GenericTypeDiscoveryTest.java b/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/util/GenericTypeDiscoveryTest.java index d54180674e..886c90c763 100644 --- a/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/util/GenericTypeDiscoveryTest.java +++ b/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/util/GenericTypeDiscoveryTest.java @@ -41,8 +41,6 @@ import org.junit.Test; import org.hibernate.metamodel.source.annotations.entity.ConfiguredClass; import org.hibernate.metamodel.source.annotations.entity.ConfiguredClassHierarchy; import org.hibernate.metamodel.source.annotations.entity.MappedAttribute; -import org.hibernate.metamodel.source.annotations.entity.ConfiguredClass; -import org.hibernate.metamodel.source.annotations.entity.ConfiguredClassHierarchy; import org.hibernate.service.ServiceRegistryBuilder; import org.hibernate.service.classloading.spi.ClassLoaderService; import org.hibernate.service.internal.BasicServiceRegistryImpl; @@ -83,7 +81,7 @@ public class GenericTypeDiscoveryTest extends BaseUnitTestCase { ClassInfo info = configuredClass.getClassInfo(); assertEquals( "wrong class", DotName.createSimple( Stuff.class.getName() ), info.name() ); MappedAttribute property = configuredClass.getMappedProperty( "value" ); - assertEquals( Price.class, property.getType() ); + assertEquals( Price.class.getName(), property.getType() ); assertTrue( iter.hasNext() ); configuredClass = iter.next(); @@ -99,9 +97,9 @@ public class GenericTypeDiscoveryTest extends BaseUnitTestCase { assertEquals( "wrong class", DotName.createSimple( Item.class.getName() ), info.name() ); // properties are alphabetically ordered! property = configuredClass.getMappedProperty( "owner" ); - assertEquals( SomeGuy.class, property.getType() ); + assertEquals( SomeGuy.class.getName(), property.getType() ); property = configuredClass.getMappedProperty( "type" ); - assertEquals( PaperType.class, property.getType() ); + assertEquals( PaperType.class.getName(), property.getType() ); assertTrue( iter.hasNext() ); configuredClass = iter.next(); diff --git a/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/util/TypeDiscoveryTest.java b/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/util/TypeDiscoveryTest.java new file mode 100644 index 0000000000..fd9ed767b5 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/util/TypeDiscoveryTest.java @@ -0,0 +1,96 @@ +/* + * 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.source.annotations.util; + +import java.util.Iterator; +import java.util.Set; +import javax.persistence.Id; + +import org.jboss.jandex.ClassInfo; +import org.jboss.jandex.DotName; +import org.jboss.jandex.Index; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import org.hibernate.metamodel.source.annotations.entity.ConfiguredClass; +import org.hibernate.metamodel.source.annotations.entity.ConfiguredClassHierarchy; +import org.hibernate.metamodel.source.annotations.entity.MappedAttribute; +import org.hibernate.service.ServiceRegistryBuilder; +import org.hibernate.service.classloading.spi.ClassLoaderService; +import org.hibernate.service.internal.BasicServiceRegistryImpl; +import org.hibernate.testing.junit4.BaseUnitTestCase; + +import static junit.framework.Assert.assertEquals; + +/** + * @author Hardy Ferentschik + */ +public class TypeDiscoveryTest extends BaseUnitTestCase { + private BasicServiceRegistryImpl serviceRegistry; + private ClassLoaderService service; + + @Before + public void setUp() { + serviceRegistry = (BasicServiceRegistryImpl) new ServiceRegistryBuilder().buildServiceRegistry(); + service = serviceRegistry.getService( ClassLoaderService.class ); + } + + @After + public void tearDown() { + serviceRegistry.destroy(); + } + + @Test + public void testImplicitAndExplicitType() { + Index index = JandexHelper.indexForClass( service, Entity.class ); + Set hierarchies = ConfiguredClassHierarchyBuilder.createEntityHierarchies( + index, serviceRegistry + ); + assertEquals( "There should be only one hierarchy", 1, hierarchies.size() ); + + Iterator iter = hierarchies.iterator().next().iterator(); + ConfiguredClass configuredClass = iter.next(); + ClassInfo info = configuredClass.getClassInfo(); + assertEquals( "wrong class", DotName.createSimple( Entity.class.getName() ), info.name() ); + + MappedAttribute property = configuredClass.getMappedProperty( "id" ); + assertEquals( "Unexpected property type", "int", property.getType() ); + + property = configuredClass.getMappedProperty( "string" ); + assertEquals( "Unexpected property type", String.class.getName(), property.getType() ); + + property = configuredClass.getMappedProperty( "customString" ); + assertEquals( "Unexpected property type", String.class.getName(), property.getType() ); + + } + + @javax.persistence.Entity + class Entity { + @Id + private int id; + private String string; + private String customString; + } +} \ No newline at end of file