From 95802a0e9392b7e171233b5723b7ce551c0afe04 Mon Sep 17 00:00:00 2001 From: Hardy Ferentschik Date: Tue, 3 May 2011 10:07:29 +0200 Subject: [PATCH] HHH-6174 Creating schema and (primary) table --- .../source/annotations/ConfiguredClass.java | 47 ++++++----- .../annotations/ConfiguredClassHierarchy.java | 2 +- .../source/annotations/EntityBinder.java | 81 +++++++++++++------ ...ppedProperty.java => MappedAttribute.java} | 32 ++++++-- .../source/annotations/util/JandexHelper.java | 44 +++++++++- .../relational/HbmColumnRelationalState.java | 45 +++++++---- .../binding/BasicAnnotationBindingTests.java | 2 +- .../metamodel/binding/SimpleEntity.java | 3 + .../ConfiguredClassHierarchyBuilderTest.java | 28 +++---- .../util/GenericTypeDiscoveryTest.java | 8 +- .../annotations/util/JandexHelperTest.java | 70 ++++++++++++++++ 11 files changed, 273 insertions(+), 89 deletions(-) rename hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/{MappedProperty.java => MappedAttribute.java} (64%) create mode 100644 hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/util/JandexHelperTest.java diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/ConfiguredClass.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/ConfiguredClass.java index d4a4179e23..1c2b9c1cfa 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/ConfiguredClass.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/ConfiguredClass.java @@ -44,6 +44,7 @@ import org.jboss.jandex.AnnotationInstance; import org.jboss.jandex.AnnotationTarget; import org.jboss.jandex.AnnotationValue; import org.jboss.jandex.ClassInfo; +import org.jboss.jandex.DotName; import org.jboss.jandex.FieldInfo; import org.jboss.jandex.MethodInfo; @@ -74,12 +75,12 @@ public class ConfiguredClass { private final InheritanceType inheritanceType; private final boolean hasOwnTable; private final String primaryTableName; - private final AnnotationInstance tableAnnotation; + //private final AnnotationInstance tableAnnotation; private final boolean isMappedSuperClass; private final boolean isEmbeddable; - private final Map mappedProperties; + private final Map mappedAttributes; public ConfiguredClass(ClassInfo info, ConfiguredClass parent, @@ -104,10 +105,6 @@ public class ConfiguredClass { ); isEmbeddable = embeddableAnnotation != null; - tableAnnotation = JandexHelper.getSingleAnnotation( - classInfo, JPADotNames.TABLE - ); - // todo think about how exactly to handle embeddables regarding access type etc classAccessType = determineClassAccessType(); @@ -115,14 +112,14 @@ public class ConfiguredClass { hasOwnTable = definesItsOwnTable(); primaryTableName = determinePrimaryTableName(); - List properties = collectMappedProperties( resolvedType ); + List properties = collectMappedProperties( resolvedType ); // make sure the properties are ordered by property name Collections.sort( properties ); - Map tmpMap = new LinkedHashMap(); - for ( MappedProperty property : properties ) { + Map tmpMap = new LinkedHashMap(); + for ( MappedAttribute property : properties ) { tmpMap.put( property.getName(), property ); } - mappedProperties = Collections.unmodifiableMap( tmpMap ); + mappedAttributes = Collections.unmodifiableMap( tmpMap ); } public String getName() { @@ -161,12 +158,12 @@ public class ConfiguredClass { return primaryTableName; } - public Iterable getMappedProperties() { - return mappedProperties.values(); + public Iterable getMappedAttributes() { + return mappedAttributes.values(); } - public MappedProperty getMappedProperty(String propertyName) { - return mappedProperties.get( propertyName ); + public MappedAttribute getMappedProperty(String propertyName) { + return mappedAttributes.get( propertyName ); } @Override @@ -174,7 +171,7 @@ public class ConfiguredClass { final StringBuilder sb = new StringBuilder(); sb.append( "ConfiguredClass" ); sb.append( "{clazz=" ).append( clazz ); - sb.append( ", mappedProperties=" ).append( mappedProperties ); + sb.append( ", mappedAttributes=" ).append( mappedAttributes ); sb.append( ", classAccessType=" ).append( classAccessType ); sb.append( ", isRoot=" ).append( isRoot ); sb.append( ", inheritanceType=" ).append( inheritanceType ); @@ -197,7 +194,7 @@ public class ConfiguredClass { /** * @return A list of the persistent properties of this configured class */ - private List collectMappedProperties(ResolvedTypeWithMembers resolvedTypes) { + private List collectMappedProperties(ResolvedTypeWithMembers resolvedTypes) { // create sets of transient field and method names Set transientFieldNames = new HashSet(); Set transientMethodNames = new HashSet(); @@ -216,7 +213,7 @@ public class ConfiguredClass { throw new AssertionFailure( "Unable to resolve types for " + clazz.getName() ); } - List properties = new ArrayList(); + List properties = new ArrayList(); Set explicitlyConfiguredMemberNames = createExplicitlyConfiguredAccessProperties( properties, resolvedType ); @@ -265,7 +262,7 @@ public class ConfiguredClass { * * @return the property names of the explicitly configured class names in a set */ - private Set createExplicitlyConfiguredAccessProperties(List mappedProperties, ResolvedTypeWithMembers resolvedMembers) { + private Set createExplicitlyConfiguredAccessProperties(List mappedProperties, ResolvedTypeWithMembers resolvedMembers) { Set explicitAccessMembers = new HashSet(); List accessAnnotations = classInfo.annotations().get( JPADotNames.ACCESS ); @@ -350,8 +347,8 @@ public class ConfiguredClass { return explicitAccessMembers; } - private MappedProperty createMappedProperty(Member member, ResolvedTypeWithMembers resolvedType) { - String name = ReflectionHelper.getPropertyName( member ); + private MappedAttribute createMappedProperty(Member member, ResolvedTypeWithMembers resolvedType) { + final String name = ReflectionHelper.getPropertyName( member ); ResolvedMember[] resolvedMembers; if ( member instanceof Field ) { resolvedMembers = resolvedType.getMemberFields(); @@ -359,8 +356,11 @@ public class ConfiguredClass { else { resolvedMembers = resolvedType.getMemberMethods(); } - Type type = findResolvedType( member.getName(), resolvedMembers ); - return new MappedProperty( name, (Class) type ); + final Type type = findResolvedType( member.getName(), resolvedMembers ); + final Map> annotations = JandexHelper.getMemberAnnotations( + classInfo, member.getName() + ); + return new MappedAttribute( name, (Class) type, annotations ); } private Type findResolvedType(String name, ResolvedMember[] resolvedMembers) { @@ -417,6 +417,9 @@ public class ConfiguredClass { String tableName = null; if ( hasOwnTable() ) { tableName = clazz.getSimpleName(); + AnnotationInstance tableAnnotation = JandexHelper.getSingleAnnotation( + classInfo, JPADotNames.TABLE + ); if ( tableAnnotation != null ) { AnnotationValue value = tableAnnotation.value( "name" ); String tmp = value == null ? null : value.asString(); diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/ConfiguredClassHierarchy.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/ConfiguredClassHierarchy.java index 0e5b31cf7e..533a1188a8 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/ConfiguredClassHierarchy.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/ConfiguredClassHierarchy.java @@ -190,7 +190,7 @@ public class ConfiguredClassHierarchy implements Iterable { private AccessType throwIdNotFoundAnnotationException(List classes) { StringBuilder builder = new StringBuilder(); - builder.append( "Unable to find Id property for class hierarchy " ); + builder.append( "Unable to determine identifier attribute for class hierarchy " ); builder.append( hierarchyListString( classes ) ); throw new AnnotationException( builder.toString() ); } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/EntityBinder.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/EntityBinder.java index 219781b4cb..a30e0d2810 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/EntityBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/EntityBinder.java @@ -29,6 +29,7 @@ import java.util.Map; import java.util.Set; import org.jboss.jandex.AnnotationInstance; +import org.jboss.jandex.AnnotationValue; import org.hibernate.AssertionFailure; import org.hibernate.MappingException; @@ -43,6 +44,7 @@ import org.hibernate.metamodel.binding.SimpleAttributeBinding; import org.hibernate.metamodel.domain.Attribute; import org.hibernate.metamodel.domain.Entity; import org.hibernate.metamodel.domain.Hierarchical; +import org.hibernate.metamodel.relational.Identifier; import org.hibernate.metamodel.relational.Schema; import org.hibernate.metamodel.relational.Size; import org.hibernate.metamodel.source.annotations.util.JandexHelper; @@ -54,15 +56,17 @@ import org.hibernate.metamodel.source.internal.MetadataImpl; * @author Hardy Ferentschik */ public class EntityBinder { - private final ConfiguredClass entity; + private final ConfiguredClass configuredClass; private final MetadataImpl meta; + private final Schema.Name schemaName; public EntityBinder(MetadataImpl metadata, ConfiguredClass configuredClass) { - this.entity = configuredClass; + this.configuredClass = configuredClass; this.meta = metadata; EntityBinding entityBinding = new EntityBinding(); bindJpaEntityAnnotation( entityBinding ); bindHibernateEntityAnnotation( entityBinding ); // optional hibernate specific @org.hibernate.annotations.Entity + schemaName = createSchemaName(); bindTable( entityBinding ); if ( configuredClass.isRoot() ) { @@ -71,19 +75,48 @@ public class EntityBinder { meta.addEntity( entityBinding ); } + private Schema.Name createSchemaName() { + String schema = ""; + String catalog = ""; + + AnnotationInstance tableAnnotation = JandexHelper.getSingleAnnotation( + configuredClass.getClassInfo(), JPADotNames.TABLE + ); + if ( tableAnnotation != null ) { + AnnotationValue schemaValue = tableAnnotation.value( "schema" ); + AnnotationValue catalogValue = tableAnnotation.value( "catalog" ); + + schema = schemaValue != null ? schemaValue.asString() : ""; + catalog = catalogValue != null ? catalogValue.asString() : ""; + } + + return new Schema.Name( schema, catalog ); + } + private void bindTable(EntityBinding entityBinding) { + final Schema schema = meta.getDatabase().getSchema( schemaName ); + final Identifier tableName = Identifier.toIdentifier( configuredClass.getPrimaryTableName() ); + org.hibernate.metamodel.relational.Table table = schema.getTable( tableName ); + if ( table == null ) { + table = schema.createTable( tableName ); + } + entityBinding.setBaseTable( table ); -// this.schemaName = new Schema.Name( -// ( entityClazz.getSchema() == null ? -// hibernateMappingBinder.getDefaultSchemaName() : -// entityClazz.getSchema() ), -// ( entityClazz.getCatalog() == null ? -// hibernateMappingBinder.getDefaultCatalogName() : -// entityClazz.getCatalog() ) -// ); + AnnotationInstance hibernateTableAnnotation = JandexHelper.getSingleAnnotation( + configuredClass.getClassInfo(), HibernateDotNames.TABLE + ); + if ( hibernateTableAnnotation != null && hibernateTableAnnotation.value( "comment" ) != null ) { + table.addComment( hibernateTableAnnotation.value( "comment" ).asString().trim() ); + } + // todo map rest of Hibernate @Table attributes - final Schema schema = meta.getDatabase().getSchema( null ); + AnnotationInstance checkAnnotation = JandexHelper.getSingleAnnotation( + configuredClass.getClassInfo(), HibernateDotNames.CHECK + ); + if ( checkAnnotation != null ) { + table.addCheckConstraint( checkAnnotation.value( "constraints" ).asString() ); + } } private void bindId(EntityBinding entityBinding) { @@ -107,11 +140,11 @@ public class EntityBinder { private void bindJpaEntityAnnotation(EntityBinding entityBinding) { AnnotationInstance jpaEntityAnnotation = JandexHelper.getSingleAnnotation( - entity.getClassInfo(), JPADotNames.ENTITY + configuredClass.getClassInfo(), JPADotNames.ENTITY ); String name; if ( jpaEntityAnnotation.value( "name" ) == null ) { - name = StringHelper.unqualify( entity.getName() ); + name = StringHelper.unqualify( configuredClass.getName() ); } else { name = jpaEntityAnnotation.value( "name" ).asString(); @@ -121,26 +154,26 @@ public class EntityBinder { private void bindSingleIdAnnotation(EntityBinding entityBinding) { AnnotationInstance idAnnotation = JandexHelper.getSingleAnnotation( - entity.getClassInfo(), JPADotNames.ID + configuredClass.getClassInfo(), JPADotNames.ID ); String idName = JandexHelper.getPropertyName( idAnnotation.target() ); entityBinding.getEntity().getOrCreateSingularAttribute( idName ); SimpleAttributeBinding idBinding = entityBinding.makeSimplePrimaryKeyAttributeBinding( idName ); - MappedProperty idProperty = entity.getMappedProperty( idName ); + MappedAttribute idAttribute = configuredClass.getMappedProperty( idName ); AnnotationSimpleAttributeDomainState domainState = new AnnotationSimpleAttributeDomainState(); HibernateTypeDescriptor typeDescriptor = new HibernateTypeDescriptor(); - typeDescriptor.setTypeName( idProperty.getType().getName() ); + typeDescriptor.setTypeName( idAttribute.getType().getName() ); domainState.typeDescriptor = typeDescriptor; - domainState.attribute = entityBinding.getEntity().getOrCreateSingularAttribute( idProperty.getName() ); + domainState.attribute = entityBinding.getEntity().getOrCreateSingularAttribute( idAttribute.getName() ); idBinding.initialize( domainState ); AnnotationColumnRelationalState columnRelationsState = new AnnotationColumnRelationalState(); columnRelationsState.namingStrategy = meta.getNamingStrategy(); - columnRelationsState.columnName = idProperty.getColumnName(); + columnRelationsState.columnName = idAttribute.getColumnName(); columnRelationsState.unique = true; columnRelationsState.nullable = false; @@ -151,7 +184,7 @@ public class EntityBinder { private void bindHibernateEntityAnnotation(EntityBinding entityBinding) { AnnotationInstance hibernateEntityAnnotation = JandexHelper.getSingleAnnotation( - entity.getClassInfo(), HibernateDotNames.ENTITY + configuredClass.getClassInfo(), HibernateDotNames.ENTITY ); // if ( hibAnn != null ) { // dynamicInsert = hibAnn.dynamicInsert(); @@ -173,7 +206,7 @@ public class EntityBinder { } private Hierarchical getSuperType() { - ConfiguredClass parent = entity.getParent(); + ConfiguredClass parent = configuredClass.getParent(); if ( parent == null ) { return null; } @@ -181,7 +214,7 @@ public class EntityBinder { EntityBinding parentBinding = meta.getEntityBinding( parent.getName() ); if ( parentBinding == null ) { throw new AssertionFailure( - "Parent entity " + parent.getName() + " of entity " + entity.getName() + "not yet created!" + "Parent entity " + parent.getName() + " of entity " + configuredClass.getName() + "not yet created!" ); } @@ -189,14 +222,14 @@ public class EntityBinder { } private IdType determineIdType() { - List idAnnotations = entity.getClassInfo().annotations().get( JPADotNames.ENTITY ); - List embeddedIdAnnotations = entity.getClassInfo() + List idAnnotations = configuredClass.getClassInfo().annotations().get( JPADotNames.ENTITY ); + List embeddedIdAnnotations = configuredClass.getClassInfo() .annotations() .get( JPADotNames.EMBEDDED_ID ); if ( idAnnotations != null && embeddedIdAnnotations != null ) { throw new MappingException( - "@EmbeddedId and @Id cannot be used together. Check the configuration for " + entity.getName() + "." + "@EmbeddedId and @Id cannot be used together. Check the configuration for " + configuredClass.getName() + "." ); } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/MappedProperty.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/MappedAttribute.java similarity index 64% rename from hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/MappedProperty.java rename to hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/MappedAttribute.java index 68d6f144ce..b3a565579f 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/MappedProperty.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/MappedAttribute.java @@ -23,34 +23,52 @@ */ package org.hibernate.metamodel.source.annotations; +import java.util.Collections; +import java.util.List; +import java.util.Map; + +import org.jboss.jandex.AnnotationInstance; +import org.jboss.jandex.DotName; + /** - * Represent a mapped property (explicitly or implicitly mapped). + * Represent a mapped attribute (explicitly or implicitly mapped). * * @author Hardy Ferentschik */ -public class MappedProperty implements Comparable { +public class MappedAttribute implements Comparable { private final String name; private final Class type; + private final Map> annotations; - MappedProperty(String name, Class type) { + MappedAttribute(String name, Class type, Map> annotations) { this.name = name; this.type = type; + this.annotations = annotations; } - public String getName() { + final public String getName() { return name; } - public String getColumnName() { + final public String getColumnName() { return name; } - public Class getType() { + final public Class getType() { return type; } + public final List annotations(DotName annotationDotName) { + if ( annotations.containsKey( annotationDotName ) ) { + return annotations.get( annotationDotName ); + } + else { + return Collections.emptyList(); + } + } + @Override - public int compareTo(MappedProperty mappedProperty) { + public int compareTo(MappedAttribute mappedProperty) { return name.compareTo( mappedProperty.getName() ); } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/util/JandexHelper.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/util/JandexHelper.java index 32843bdda1..2fa3027099 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/util/JandexHelper.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/util/JandexHelper.java @@ -26,7 +26,10 @@ package org.hibernate.metamodel.source.annotations.util; import java.beans.Introspector; import java.io.IOException; import java.io.InputStream; +import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; import org.jboss.jandex.AnnotationInstance; import org.jboss.jandex.AnnotationTarget; @@ -37,7 +40,6 @@ import org.jboss.jandex.Index; import org.jboss.jandex.Indexer; import org.jboss.jandex.MethodInfo; - import org.hibernate.AssertionFailure; import org.hibernate.HibernateException; import org.hibernate.service.classloading.spi.ClassLoaderService; @@ -145,6 +147,46 @@ public class JandexHelper { } return indexer.complete(); } + + public static Map> getMemberAnnotations(ClassInfo classInfo, String name) { + if ( classInfo == null ) { + throw new IllegalArgumentException( "classInfo cannot be null" ); + } + + if ( name == null ) { + throw new IllegalArgumentException( "name cannot be null" ); + } + + Map> annotations = new HashMap>(); + for ( List annotationList : classInfo.annotations().values() ) { + for ( AnnotationInstance instance : annotationList ) { + String targetName = null; + if ( instance.target() instanceof FieldInfo ) { + targetName = ( (FieldInfo) instance.target() ).name(); + } + else if ( instance.target() instanceof MethodInfo ) { + targetName = ( (MethodInfo) instance.target() ).name(); + } + if ( targetName != null && name.equals( targetName ) ) { + addAnnotationToMap( instance, annotations ); + } + } + } + return annotations; + } + + private static void addAnnotationToMap(AnnotationInstance instance, Map> annotations) { + DotName dotName = instance.name(); + List list; + if ( annotations.containsKey( dotName ) ) { + list = annotations.get( dotName ); + } + else { + list = new ArrayList(); + annotations.put( dotName, list ); + } + list.add( instance ); + } } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/state/relational/HbmColumnRelationalState.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/state/relational/HbmColumnRelationalState.java index ccac43e7e1..f7ad3eb722 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/state/relational/HbmColumnRelationalState.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/state/relational/HbmColumnRelationalState.java @@ -60,7 +60,7 @@ public class HbmColumnRelationalState implements AbstractAttributeBinding.Column /* package-protected */ HbmColumnRelationalState(XMLColumnElement columnElement, - HbmSimpleValueRelationalStateContainer container) { + HbmSimpleValueRelationalStateContainer container) { this.container = container; this.explicitColumnName = columnElement.getName(); this.size = createSize( columnElement.getLength(), columnElement.getScale(), columnElement.getPrecision() ); @@ -70,8 +70,8 @@ public class HbmColumnRelationalState implements AbstractAttributeBinding.Column this.defaultColumnValue = columnElement.getDefault(); this.sqlType = columnElement.getSqlType(); this.customWrite = columnElement.getWrite(); - if ( customWrite != null && ! customWrite.matches("[^?]*\\?[^?]*") ) { - throw new MappingException("write expression must contain exactly one value placeholder ('?') character"); + if ( customWrite != null && !customWrite.matches( "[^?]*\\?[^?]*" ) ) { + throw new MappingException( "write expression must contain exactly one value placeholder ('?') character" ); } this.customRead = columnElement.getRead(); this.comment = columnElement.getComment() == null ? null : columnElement.getComment().trim(); @@ -82,7 +82,7 @@ public class HbmColumnRelationalState implements AbstractAttributeBinding.Column } HbmColumnRelationalState(XMLPropertyElement property, - HbmSimpleValueRelationalStateContainer container) { + HbmSimpleValueRelationalStateContainer container) { this.container = container; this.explicitColumnName = property.getName(); this.size = createSize( property.getLength(), property.getScale(), property.getPrecision() ); @@ -101,11 +101,11 @@ public class HbmColumnRelationalState implements AbstractAttributeBinding.Column } HbmColumnRelationalState(XMLManyToOneElement manyToOne, - HbmSimpleValueRelationalStateContainer container) { + HbmSimpleValueRelationalStateContainer container) { this.container = container; this.explicitColumnName = manyToOne.getName(); this.size = new Size(); - this.isNullable =! MappingHelper.getBooleanValue( manyToOne.isNotNull(), false ); + this.isNullable = !MappingHelper.getBooleanValue( manyToOne.isNotNull(), false ); this.isUnique = manyToOne.isUnique(); this.checkCondition = null; this.defaultColumnValue = null; @@ -120,8 +120,8 @@ public class HbmColumnRelationalState implements AbstractAttributeBinding.Column } HbmColumnRelationalState(XMLId id, - HbmSimpleValueRelationalStateContainer container) { - if ( id.getColumn() != null && ! id.getColumn().isEmpty() ) { + HbmSimpleValueRelationalStateContainer container) { + if ( id.getColumn() != null && !id.getColumn().isEmpty() ) { throw new IllegalArgumentException( "This method should not be called with non-empty id.getColumnElement()" ); } this.container = container; @@ -140,9 +140,11 @@ public class HbmColumnRelationalState implements AbstractAttributeBinding.Column } HbmColumnRelationalState(XMLDiscriminator discriminator, - HbmSimpleValueRelationalStateContainer container) { - if ( discriminator.getColumn() != null ) { - throw new IllegalArgumentException( "This method should not be called with null discriminator.getColumnElement()" ); + HbmSimpleValueRelationalStateContainer container) { + if ( discriminator.getColumn() != null ) { + throw new IllegalArgumentException( + "This method should not be called with null discriminator.getColumnElement()" + ); } this.container = container; this.explicitColumnName = null; @@ -160,11 +162,13 @@ public class HbmColumnRelationalState implements AbstractAttributeBinding.Column } HbmColumnRelationalState(XMLVersion version, - HbmSimpleValueRelationalStateContainer container) { + HbmSimpleValueRelationalStateContainer container) { this.container = container; this.explicitColumnName = version.getColumnAttribute(); - if ( version.getColumn() != null && ! version.getColumn().isEmpty() ) { - throw new IllegalArgumentException( "This method should not be called with non-empty version.getColumnElement()" ); + if ( version.getColumn() != null && !version.getColumn().isEmpty() ) { + throw new IllegalArgumentException( + "This method should not be called with non-empty version.getColumnElement()" + ); } // TODO: should set default this.size = new Size(); @@ -181,7 +185,7 @@ public class HbmColumnRelationalState implements AbstractAttributeBinding.Column } HbmColumnRelationalState(XMLTimestamp timestamp, - HbmSimpleValueRelationalStateContainer container) { + HbmSimpleValueRelationalStateContainer container) { this.container = container; this.explicitColumnName = timestamp.getColumn(); // TODO: should set default @@ -201,12 +205,15 @@ public class HbmColumnRelationalState implements AbstractAttributeBinding.Column public NamingStrategy getNamingStrategy() { return container.getNamingStrategy(); } + public String getExplicitColumnName() { return explicitColumnName; } + public Size getSize() { return size; } + protected static Size createSize(String length, String scale, String precision) { // TODO: should this set defaults if length, scale, precision is not specified? Size size = new Size(); @@ -222,6 +229,7 @@ public class HbmColumnRelationalState implements AbstractAttributeBinding.Column // TODO: is there an attribute for lobMultiplier? return size; } + public boolean isNullable() { return isNullable; } @@ -233,24 +241,31 @@ public class HbmColumnRelationalState implements AbstractAttributeBinding.Column public String getCheckCondition() { return checkCondition; } + public String getDefault() { return defaultColumnValue; } + public String getSqlType() { return sqlType; } + public String getCustomWriteFragment() { return customWrite; } + public String getCustomReadFragment() { return customRead; } + public String getComment() { return comment; } + public Set getUniqueKeys() { return uniqueKeys; } + public Set getIndexes() { return indexes; } diff --git a/hibernate-core/src/test/java/org/hibernate/metamodel/binding/BasicAnnotationBindingTests.java b/hibernate-core/src/test/java/org/hibernate/metamodel/binding/BasicAnnotationBindingTests.java index 7ac55a2b43..27cfd73814 100644 --- a/hibernate-core/src/test/java/org/hibernate/metamodel/binding/BasicAnnotationBindingTests.java +++ b/hibernate-core/src/test/java/org/hibernate/metamodel/binding/BasicAnnotationBindingTests.java @@ -38,7 +38,7 @@ import org.hibernate.testing.FailureExpected; */ public class BasicAnnotationBindingTests extends AbstractBasicBindingTests { - @FailureExpected(jiraKey = "HHH-5672", message = "Work in progress") + //@FailureExpected(jiraKey = "HHH-5672", message = "Work in progress") @Test public void testSimpleEntityMapping() { super.testSimpleEntityMapping(); diff --git a/hibernate-core/src/test/java/org/hibernate/metamodel/binding/SimpleEntity.java b/hibernate-core/src/test/java/org/hibernate/metamodel/binding/SimpleEntity.java index 227dc31cf4..a7370788b2 100644 --- a/hibernate-core/src/test/java/org/hibernate/metamodel/binding/SimpleEntity.java +++ b/hibernate-core/src/test/java/org/hibernate/metamodel/binding/SimpleEntity.java @@ -25,11 +25,14 @@ package org.hibernate.metamodel.binding; import javax.persistence.Entity; import javax.persistence.Id; +import javax.persistence.Table; + /** * @author Steve Ebersole */ @Entity +@Table(schema = "foo") public class SimpleEntity { @Id private Long id; diff --git a/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/util/ConfiguredClassHierarchyBuilderTest.java b/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/util/ConfiguredClassHierarchyBuilderTest.java index 5f4a46bb9e..a1b316b0af 100644 --- a/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/util/ConfiguredClassHierarchyBuilderTest.java +++ b/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/util/ConfiguredClassHierarchyBuilderTest.java @@ -59,12 +59,12 @@ import static org.junit.Assert.assertFalse; public class ConfiguredClassHierarchyBuilderTest extends BaseUnitTestCase { private BasicServiceRegistryImpl serviceRegistry; - private ClassLoaderService service; + private ClassLoaderService classLoaderService; @Before public void setUp() { serviceRegistry = (BasicServiceRegistryImpl) new ServiceRegistryBuilder().buildServiceRegistry(); - service = serviceRegistry.getService( ClassLoaderService.class ); + classLoaderService = serviceRegistry.getService( ClassLoaderService.class ); } @After @@ -74,7 +74,7 @@ public class ConfiguredClassHierarchyBuilderTest extends BaseUnitTestCase { @Test public void testSingleEntity() { - Index index = JandexHelper.indexForClass( service, Foo.class ); + Index index = JandexHelper.indexForClass( classLoaderService, Foo.class ); Set hierarchies = ConfiguredClassHierarchyBuilder.createEntityHierarchies( index, serviceRegistry ); @@ -88,7 +88,7 @@ public class ConfiguredClassHierarchyBuilderTest extends BaseUnitTestCase { @Test public void testSimpleInheritance() { - Index index = JandexHelper.indexForClass( service, B.class, A.class ); + Index index = JandexHelper.indexForClass( classLoaderService, B.class, A.class ); Set hierarchies = ConfiguredClassHierarchyBuilder.createEntityHierarchies( index, serviceRegistry ); @@ -104,7 +104,7 @@ public class ConfiguredClassHierarchyBuilderTest extends BaseUnitTestCase { @Test public void testMultipleHierarchies() { - Index index = JandexHelper.indexForClass( service, B.class, A.class, Foo.class ); + Index index = JandexHelper.indexForClass( classLoaderService, B.class, A.class, Foo.class ); Set hierarchies = ConfiguredClassHierarchyBuilder.createEntityHierarchies( index, serviceRegistry ); @@ -130,7 +130,7 @@ public class ConfiguredClassHierarchyBuilderTest extends BaseUnitTestCase { } Index index = JandexHelper.indexForClass( - service, MappedSubClass.class, MappedSuperClass.class, UnmappedSubClass.class + classLoaderService, MappedSubClass.class, MappedSuperClass.class, UnmappedSubClass.class ); Set hierarchies = ConfiguredClassHierarchyBuilder.createEntityHierarchies( index, serviceRegistry @@ -154,7 +154,7 @@ public class ConfiguredClassHierarchyBuilderTest extends BaseUnitTestCase { class EntityAndMappedSuperClass { } - Index index = JandexHelper.indexForClass( service, EntityAndMappedSuperClass.class ); + Index index = JandexHelper.indexForClass( classLoaderService, EntityAndMappedSuperClass.class ); ConfiguredClassHierarchyBuilder.createEntityHierarchies( index, serviceRegistry ); } @@ -165,7 +165,7 @@ public class ConfiguredClassHierarchyBuilderTest extends BaseUnitTestCase { class EntityAndEmbeddable { } - Index index = JandexHelper.indexForClass( service, EntityAndEmbeddable.class ); + Index index = JandexHelper.indexForClass( classLoaderService, EntityAndEmbeddable.class ); ConfiguredClassHierarchyBuilder.createEntityHierarchies( index, serviceRegistry ); } @@ -181,7 +181,7 @@ public class ConfiguredClassHierarchyBuilderTest extends BaseUnitTestCase { class B extends A { } - Index index = JandexHelper.indexForClass( service, B.class, A.class ); + Index index = JandexHelper.indexForClass( classLoaderService, B.class, A.class ); ConfiguredClassHierarchyBuilder.createEntityHierarchies( index, serviceRegistry ); } @@ -197,7 +197,7 @@ public class ConfiguredClassHierarchyBuilderTest extends BaseUnitTestCase { class B extends A { } - Index index = JandexHelper.indexForClass( service, B.class, A.class ); + Index index = JandexHelper.indexForClass( classLoaderService, B.class, A.class ); Set hierarchies = ConfiguredClassHierarchyBuilder.createEntityHierarchies( index, serviceRegistry ); @@ -226,7 +226,7 @@ public class ConfiguredClassHierarchyBuilderTest extends BaseUnitTestCase { class B extends A { } - Index index = JandexHelper.indexForClass( service, B.class, A.class ); + Index index = JandexHelper.indexForClass( classLoaderService, B.class, A.class ); Set hierarchies = ConfiguredClassHierarchyBuilder.createEntityHierarchies( index, serviceRegistry ); @@ -247,7 +247,7 @@ public class ConfiguredClassHierarchyBuilderTest extends BaseUnitTestCase { class B extends A { } - Index index = JandexHelper.indexForClass( service, B.class, A.class ); + Index index = JandexHelper.indexForClass( classLoaderService, B.class, A.class ); Set hierarchies = ConfiguredClassHierarchyBuilder.createEntityHierarchies( index, serviceRegistry ); @@ -275,7 +275,7 @@ public class ConfiguredClassHierarchyBuilderTest extends BaseUnitTestCase { class B extends A { } - Index index = JandexHelper.indexForClass( service, B.class, MappedSuperClass.class, A.class ); + Index index = JandexHelper.indexForClass( classLoaderService, B.class, MappedSuperClass.class, A.class ); Set hierarchies = ConfiguredClassHierarchyBuilder.createEntityHierarchies( index, serviceRegistry ); @@ -298,7 +298,7 @@ public class ConfiguredClassHierarchyBuilderTest extends BaseUnitTestCase { class B extends A { } - Index index = JandexHelper.indexForClass( service, B.class, A.class ); + Index index = JandexHelper.indexForClass( classLoaderService, B.class, A.class ); ConfiguredClassHierarchyBuilder.createEntityHierarchies( index, serviceRegistry ); } 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 bb506d59c6..2d7a25a1dc 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 @@ -40,7 +40,7 @@ import org.junit.Test; import org.hibernate.metamodel.source.annotations.ConfiguredClass; import org.hibernate.metamodel.source.annotations.ConfiguredClassHierarchy; -import org.hibernate.metamodel.source.annotations.MappedProperty; +import org.hibernate.metamodel.source.annotations.MappedAttribute; import org.hibernate.service.ServiceRegistryBuilder; import org.hibernate.service.classloading.spi.ClassLoaderService; import org.hibernate.service.internal.BasicServiceRegistryImpl; @@ -80,7 +80,7 @@ public class GenericTypeDiscoveryTest extends BaseUnitTestCase { ConfiguredClass configuredClass = iter.next(); ClassInfo info = configuredClass.getClassInfo(); assertEquals( "wrong class", DotName.createSimple( Stuff.class.getName() ), info.name() ); - MappedProperty property = configuredClass.getMappedProperty( "value" ); + MappedAttribute property = configuredClass.getMappedProperty( "value" ); assertEquals( Price.class, property.getType() ); assertTrue( iter.hasNext() ); @@ -88,7 +88,7 @@ public class GenericTypeDiscoveryTest extends BaseUnitTestCase { info = configuredClass.getClassInfo(); assertEquals( "wrong class", DotName.createSimple( PricedStuff.class.getName() ), info.name() ); assertFalse( - "PricedStuff should not mapped properties", configuredClass.getMappedProperties().iterator().hasNext() + "PricedStuff should not mapped properties", configuredClass.getMappedAttributes().iterator().hasNext() ); assertTrue( iter.hasNext() ); @@ -105,7 +105,7 @@ public class GenericTypeDiscoveryTest extends BaseUnitTestCase { configuredClass = iter.next(); info = configuredClass.getClassInfo(); assertEquals( "wrong class", DotName.createSimple( Paper.class.getName() ), info.name() ); - assertFalse( "Paper should not mapped properties", configuredClass.getMappedProperties().iterator().hasNext() ); + assertFalse( "Paper should not mapped properties", configuredClass.getMappedAttributes().iterator().hasNext() ); assertFalse( iter.hasNext() ); } diff --git a/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/util/JandexHelperTest.java b/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/util/JandexHelperTest.java new file mode 100644 index 0000000000..ec0c9f5106 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/util/JandexHelperTest.java @@ -0,0 +1,70 @@ +package org.hibernate.metamodel.source.annotations.util; + +import java.util.List; +import java.util.Map; +import javax.persistence.Basic; +import javax.persistence.Column; + +import org.jboss.jandex.AnnotationInstance; +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.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.assertTrue; + +/** + * @author Hardy Ferentschik + */ +public class JandexHelperTest extends BaseUnitTestCase { + private BasicServiceRegistryImpl serviceRegistry; + private ClassLoaderService classLoaderService; + + @Before + public void setUp() { + serviceRegistry = (BasicServiceRegistryImpl) new ServiceRegistryBuilder().buildServiceRegistry(); + classLoaderService = serviceRegistry.getService( ClassLoaderService.class ); + } + + @After + public void tearDown() { + serviceRegistry.destroy(); + } + + @Test + public void testGetMemberAnnotations() { + class Foo { + @Column + @Basic + private String bar; + + private String fubar; + } + Index index = JandexHelper.indexForClass( classLoaderService, Foo.class ); + + ClassInfo classInfo = index.getClassByName( DotName.createSimple( Foo.class.getName() ) ); + Map> memberAnnotations = JandexHelper.getMemberAnnotations( + classInfo, "bar" + ); + assertTrue( + "property bar should defines @Column annotation", + memberAnnotations.containsKey( DotName.createSimple( Column.class.getName() ) ) + ); + assertTrue( + "property bar should defines @Basic annotation", + memberAnnotations.containsKey( DotName.createSimple( Basic.class.getName() ) ) + ); + + memberAnnotations = JandexHelper.getMemberAnnotations( classInfo, "fubar" ); + assertTrue( "there should be no annotations in fubar", memberAnnotations.isEmpty() ); + } +} + +