From 3cbd2ae908ed8e00c0ff17f84a4445414b3e9b8f Mon Sep 17 00:00:00 2001 From: Hardy Ferentschik Date: Wed, 27 Jul 2011 13:40:25 +0200 Subject: [PATCH] HHH-6488 Implementing SimpleIdentifierSource#getIdentifierGeneratorDescriptor for annotations --- .../annotations/TypeEnumConversionHelper.java | 49 ++++++ .../attribute/AssociationAttribute.java | 11 +- .../annotations/attribute/BasicAttribute.java | 66 +++++++- .../attribute/MappedAttribute.java | 15 +- .../attribute/SimpleIdentifierSourceImpl.java | 11 +- .../annotations/entity/ConfiguredClass.java | 10 +- .../annotations/global/IdGeneratorBinder.java | 82 ++++------ .../annotations/xml/mocker/AccessHelper.java | 6 +- .../metamodel/source/binder/Binder.java | 2 +- .../entity/IdentifierGeneratorTest.java | 144 ++++++++++++++---- 10 files changed, 299 insertions(+), 97 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/TypeEnumConversionHelper.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/TypeEnumConversionHelper.java index c89857dc70..92a62e3b83 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/TypeEnumConversionHelper.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/TypeEnumConversionHelper.java @@ -1,11 +1,60 @@ +/* + * 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; +import javax.persistence.GenerationType; + +import org.hibernate.AssertionFailure; +import org.hibernate.id.MultipleHiLoPerTableGenerator; + /** + * Helper class which converts type enums from JPA annotations into the Hibernate specific form + * * @author Hardy Ferentschik */ public class TypeEnumConversionHelper { private TypeEnumConversionHelper() { + } + public static String generationTypeToGeneratorStrategyName(GenerationType generatorEnum, boolean useNewGeneratorMappings) { + switch ( generatorEnum ) { + case IDENTITY: + return "identity"; + case AUTO: + return useNewGeneratorMappings + ? "enhanced-sequence" + : "native"; + case TABLE: + return useNewGeneratorMappings + ? "enhanced-table" + : MultipleHiLoPerTableGenerator.class.getName(); + case SEQUENCE: + return useNewGeneratorMappings + ? "enhanced-sequence" + : "seqhilo"; + } + throw new AssertionFailure( "Unknown GeneratorType: " + generatorEnum ); } } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/attribute/AssociationAttribute.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/attribute/AssociationAttribute.java index 4458ce636a..51af34b508 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/attribute/AssociationAttribute.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/attribute/AssociationAttribute.java @@ -34,6 +34,7 @@ import org.jboss.jandex.AnnotationValue; import org.jboss.jandex.DotName; import org.hibernate.annotations.NotFoundAction; +import org.hibernate.metamodel.source.annotations.AnnotationBindingContext; import org.hibernate.metamodel.source.annotations.HibernateDotNames; import org.hibernate.metamodel.source.annotations.JandexHelper; @@ -50,16 +51,18 @@ public class AssociationAttribute extends BasicAttribute { Class attributeType, AttributeType attributeNature, String accessType, - Map> annotations) { - return new AssociationAttribute( name, attributeType, attributeNature, accessType, annotations ); + Map> annotations, + AnnotationBindingContext context) { + return new AssociationAttribute( name, attributeType, attributeNature, accessType, annotations, context ); } private AssociationAttribute(String name, Class javaType, AttributeType associationType, String accessType, - Map> annotations) { - super( name, javaType, accessType, annotations ); + Map> annotations, + AnnotationBindingContext context) { + super( name, javaType, accessType, annotations, context ); this.associationType = associationType; this.ignoreNotFound = ignoreNotFound(); diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/attribute/BasicAttribute.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/attribute/BasicAttribute.java index 3abc8399b5..80444241d9 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/attribute/BasicAttribute.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/attribute/BasicAttribute.java @@ -28,6 +28,7 @@ import java.util.Arrays; import java.util.List; import java.util.Map; import javax.persistence.FetchType; +import javax.persistence.GenerationType; import org.jboss.jandex.AnnotationInstance; import org.jboss.jandex.AnnotationValue; @@ -35,10 +36,15 @@ import org.jboss.jandex.DotName; import org.hibernate.AnnotationException; import org.hibernate.annotations.GenerationTime; +import org.hibernate.internal.util.StringHelper; import org.hibernate.mapping.PropertyGeneration; +import org.hibernate.metamodel.binding.IdGenerator; +import org.hibernate.metamodel.source.MappingException; +import org.hibernate.metamodel.source.annotations.AnnotationBindingContext; import org.hibernate.metamodel.source.annotations.HibernateDotNames; import org.hibernate.metamodel.source.annotations.JPADotNames; import org.hibernate.metamodel.source.annotations.JandexHelper; +import org.hibernate.metamodel.source.annotations.TypeEnumConversionHelper; /** * Represent a mapped attribute (explicitly or implicitly mapped). @@ -51,6 +57,12 @@ public class BasicAttribute extends MappedAttribute { */ private final boolean isId; + /** + * The id generator in case this basic attribute represents an simple id. Will be {@code null} in case there + * is no explicit id generator or the containing entity does not have a simple id + */ + private final IdGenerator idGenerator; + /** * Is this a versioned property (annotated w/ {@code @Version}. */ @@ -88,12 +100,20 @@ public class BasicAttribute extends MappedAttribute { private final String customReadFragment; private final String checkCondition; - public static BasicAttribute createSimpleAttribute(String name, Class attributeType, Map> annotations, String accessType) { - return new BasicAttribute( name, attributeType, accessType, annotations ); + public static BasicAttribute createSimpleAttribute(String name, + Class attributeType, + Map> annotations, + String accessType, + AnnotationBindingContext context) { + return new BasicAttribute( name, attributeType, accessType, annotations, context ); } - BasicAttribute(String name, Class attributeType, String accessType, Map> annotations) { - super( name, attributeType, accessType, annotations ); + BasicAttribute(String name, + Class attributeType, + String accessType, + Map> annotations, + AnnotationBindingContext context) { + super( name, attributeType, accessType, annotations, context ); AnnotationInstance idAnnotation = JandexHelper.getSingleAnnotation( annotations, JPADotNames.ID ); AnnotationInstance embeddedIdAnnotation = JandexHelper.getSingleAnnotation( @@ -112,6 +132,10 @@ public class BasicAttribute extends MappedAttribute { // an id must be unique and cannot be nullable columnValues.setUnique( true ); columnValues.setNullable( false ); + idGenerator = checkGeneratedValueAnnotation(); + } + else { + idGenerator = null; } this.isOptimisticLockable = checkOptimisticLockAnnotation(); @@ -175,6 +199,10 @@ public class BasicAttribute extends MappedAttribute { return checkCondition; } + public IdGenerator getIdGenerator() { + return idGenerator; + } + @Override public String toString() { final StringBuilder sb = new StringBuilder(); @@ -290,6 +318,36 @@ public class BasicAttribute extends MappedAttribute { } return checkCondition; } + + private IdGenerator checkGeneratedValueAnnotation() { + IdGenerator generator = null; + AnnotationInstance generatedValueAnnotation = JandexHelper.getSingleAnnotation( + annotations(), + JPADotNames.GENERATED_VALUE + ); + if ( generatedValueAnnotation != null ) { + String name = JandexHelper.getValue( generatedValueAnnotation, "generator", String.class ); + if ( StringHelper.isNotEmpty( name ) ) { + generator = getContext().getMetadataImplementor().getIdGenerator( name ); + if ( generator == null ) { + throw new MappingException( String.format( "Unable to find named generator %s", name ), null ); + } + } + else { + GenerationType genType = JandexHelper.getEnumValue( + generatedValueAnnotation, + "strategy", + GenerationType.class + ); + String strategy = TypeEnumConversionHelper.generationTypeToGeneratorStrategyName( + genType, + getContext().getMetadataImplementor().getOptions().useNewIdentifierGenerators() + ); + generator = new IdGenerator( null, strategy, null ); + } + } + return generator; + } } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/attribute/MappedAttribute.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/attribute/MappedAttribute.java index c087663394..c777e2b126 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/attribute/MappedAttribute.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/attribute/MappedAttribute.java @@ -31,6 +31,7 @@ import org.jboss.jandex.AnnotationInstance; import org.jboss.jandex.AnnotationValue; import org.jboss.jandex.DotName; +import org.hibernate.metamodel.source.annotations.AnnotationBindingContext; import org.hibernate.metamodel.source.annotations.HibernateDotNames; import org.hibernate.metamodel.source.annotations.JandexHelper; @@ -71,13 +72,19 @@ public abstract class MappedAttribute implements Comparable { */ private final Map explicitHibernateTypeParameters; - MappedAttribute(String name, Class attributeType, String accessType, Map> annotations) { + private final AnnotationBindingContext context; + + MappedAttribute(String name, Class attributeType, String accessType, Map> annotations, AnnotationBindingContext context) { + this.context = context; this.annotations = annotations; this.name = name; this.attributeType = attributeType; this.accessType = accessType; - final AnnotationInstance typeAnnotation = JandexHelper.getSingleAnnotation(annotations(), HibernateDotNames.TYPE ); + final AnnotationInstance typeAnnotation = JandexHelper.getSingleAnnotation( + annotations(), + HibernateDotNames.TYPE + ); if ( typeAnnotation != null ) { this.explicitHibernateTypeName = typeAnnotation.value( "type" ).asString(); this.explicitHibernateTypeParameters = extractTypeParameters( typeAnnotation ); @@ -123,6 +130,10 @@ public abstract class MappedAttribute implements Comparable { return explicitHibernateTypeParameters; } + public AnnotationBindingContext getContext() { + return context; + } + Map> annotations() { return annotations; } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/attribute/SimpleIdentifierSourceImpl.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/attribute/SimpleIdentifierSourceImpl.java index ab26c01e8a..cf4cc77d2e 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/attribute/SimpleIdentifierSourceImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/attribute/SimpleIdentifierSourceImpl.java @@ -23,6 +23,7 @@ */ package org.hibernate.metamodel.source.annotations.attribute; +import org.hibernate.AssertionFailure; import org.hibernate.metamodel.binding.IdGenerator; import org.hibernate.metamodel.source.binder.SimpleIdentifierSource; import org.hibernate.metamodel.source.binder.SingularAttributeSource; @@ -34,6 +35,14 @@ public class SimpleIdentifierSourceImpl implements SimpleIdentifierSource { private final BasicAttribute attribute; public SimpleIdentifierSourceImpl(BasicAttribute attribute) { + if ( !attribute.isId() ) { + throw new AssertionFailure( + String.format( + "A non id attribute was passed to SimpleIdentifierSourceImpl: %s", + attribute.toString() + ) + ); + } this.attribute = attribute; } @@ -49,7 +58,7 @@ public class SimpleIdentifierSourceImpl implements SimpleIdentifierSource { @Override public IdGenerator getIdentifierGeneratorDescriptor() { - return null; //To change body of implemented methods use File | Settings | File Templates. + return attribute.getIdGenerator(); } } 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 98465d35ea..66740a701c 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 @@ -60,8 +60,8 @@ import org.hibernate.metamodel.source.annotations.ReflectionHelper; import org.hibernate.metamodel.source.annotations.attribute.AssociationAttribute; import org.hibernate.metamodel.source.annotations.attribute.AttributeOverride; import org.hibernate.metamodel.source.annotations.attribute.AttributeType; -import org.hibernate.metamodel.source.annotations.attribute.MappedAttribute; import org.hibernate.metamodel.source.annotations.attribute.BasicAttribute; +import org.hibernate.metamodel.source.annotations.attribute.MappedAttribute; /** * Base class for a configured entity, mapped super class or embeddable @@ -246,7 +246,7 @@ public class ConfiguredClass { AnnotationInstance accessAnnotation = JandexHelper.getSingleAnnotation( classInfo, JPADotNames.ACCESS ); if ( accessAnnotation != null && accessAnnotation.target().getClass().equals( ClassInfo.class ) ) { - accessType = JandexHelper.getValueAsEnum( accessAnnotation, "value", AccessType.class ); + accessType = JandexHelper.getEnumValue( accessAnnotation, "value", AccessType.class ); } return accessType; @@ -334,7 +334,7 @@ public class ConfiguredClass { continue; } - AccessType accessType = JandexHelper.getValueAsEnum( accessAnnotation, "value", AccessType.class ); + AccessType accessType = JandexHelper.getEnumValue( accessAnnotation, "value", AccessType.class ); if ( !isExplicitAttributeAccessAnnotationPlacedCorrectly( annotationTarget, accessType ) ) { continue; @@ -446,7 +446,7 @@ public class ConfiguredClass { switch ( attributeNature ) { case BASIC: { BasicAttribute attribute = BasicAttribute.createSimpleAttribute( - attributeName, attributeType, annotations, accessTypeString + attributeName, attributeType, annotations, accessTypeString, getContext() ); if ( attribute.isId() ) { idAttributeMap.put( attributeName, attribute ); @@ -469,7 +469,7 @@ public class ConfiguredClass { // TODO handle the different association types default: { AssociationAttribute attribute = AssociationAttribute.createAssociationAttribute( - attributeName, attributeType, attributeNature, accessTypeString, annotations + attributeName, attributeType, attributeNature, accessTypeString, annotations, getContext() ); associationAttributeMap.put( attributeName, attribute ); } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/global/IdGeneratorBinder.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/global/IdGeneratorBinder.java index 0ed792fc38..2406cc41a1 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/global/IdGeneratorBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/global/IdGeneratorBinder.java @@ -32,7 +32,6 @@ import javax.persistence.SequenceGenerator; import org.jboss.jandex.AnnotationInstance; import org.jboss.logging.Logger; -import org.hibernate.AssertionFailure; import org.hibernate.annotations.GenericGenerator; import org.hibernate.annotations.GenericGenerators; import org.hibernate.cfg.AvailableSettings; @@ -50,6 +49,7 @@ import org.hibernate.metamodel.source.annotations.AnnotationBindingContext; import org.hibernate.metamodel.source.annotations.HibernateDotNames; import org.hibernate.metamodel.source.annotations.JPADotNames; import org.hibernate.metamodel.source.annotations.JandexHelper; +import org.hibernate.metamodel.source.annotations.TypeEnumConversionHelper; /** * Binds {@link SequenceGenerator}, {@link javax.persistence.TableGenerator}, {@link GenericGenerator}, and @@ -138,19 +138,21 @@ public class IdGeneratorBinder { private static void bindSequenceGenerator(MetadataImplementor metadata, AnnotationInstance generator) { String name = JandexHelper.getValue( generator, "name", String.class ); - String strategy; - Map prms = new HashMap(); - addStringParameter( generator, "sequenceName", prms, SequenceStyleGenerator.SEQUENCE_PARAM ); + Map parameterMap = new HashMap(); + addStringParameter( generator, "sequenceName", parameterMap, SequenceStyleGenerator.SEQUENCE_PARAM ); boolean useNewIdentifierGenerators = metadata.getOptions().useNewIdentifierGenerators(); - strategy = generatorType( GenerationType.SEQUENCE, useNewIdentifierGenerators ); + String strategy = TypeEnumConversionHelper.generationTypeToGeneratorStrategyName( + GenerationType.SEQUENCE, + useNewIdentifierGenerators + ); if ( useNewIdentifierGenerators ) { - addStringParameter( generator, "catalog", prms, PersistentIdentifierGenerator.CATALOG ); - addStringParameter( generator, "schema", prms, PersistentIdentifierGenerator.SCHEMA ); - prms.put( + addStringParameter( generator, "catalog", parameterMap, PersistentIdentifierGenerator.CATALOG ); + addStringParameter( generator, "schema", parameterMap, PersistentIdentifierGenerator.SCHEMA ); + parameterMap.put( SequenceStyleGenerator.INCREMENT_PARAM, String.valueOf( JandexHelper.getValue( generator, "allocationSize", Integer.class ) ) ); - prms.put( + parameterMap.put( SequenceStyleGenerator.INITIAL_PARAM, String.valueOf( JandexHelper.getValue( generator, "initialValue", Integer.class ) ) ); @@ -159,44 +161,46 @@ public class IdGeneratorBinder { if ( JandexHelper.getValue( generator, "initialValue", Integer.class ) != 1 ) { LOG.unsupportedInitialValue( AvailableSettings.USE_NEW_ID_GENERATOR_MAPPINGS ); } - prms.put( + parameterMap.put( SequenceHiLoGenerator.MAX_LO, String.valueOf( JandexHelper.getValue( generator, "allocationSize", Integer.class ) - 1 ) ); } - metadata.addIdGenerator( new IdGenerator( name, strategy, prms ) ); + metadata.addIdGenerator( new IdGenerator( name, strategy, parameterMap ) ); LOG.tracef( "Add sequence generator with name: %s", name ); } private static void bindTableGenerator(MetadataImplementor metadata, AnnotationInstance generator) { String name = JandexHelper.getValue( generator, "name", String.class ); - String strategy; - Map prms = new HashMap(); - addStringParameter( generator, "catalog", prms, PersistentIdentifierGenerator.CATALOG ); - addStringParameter( generator, "schema", prms, PersistentIdentifierGenerator.SCHEMA ); + Map parameterMap = new HashMap(); + addStringParameter( generator, "catalog", parameterMap, PersistentIdentifierGenerator.CATALOG ); + addStringParameter( generator, "schema", parameterMap, PersistentIdentifierGenerator.SCHEMA ); boolean useNewIdentifierGenerators = metadata.getOptions().useNewIdentifierGenerators(); - strategy = generatorType( GenerationType.TABLE, useNewIdentifierGenerators ); + String strategy = TypeEnumConversionHelper.generationTypeToGeneratorStrategyName( + GenerationType.TABLE, + useNewIdentifierGenerators + ); if ( useNewIdentifierGenerators ) { - prms.put( TableGenerator.CONFIG_PREFER_SEGMENT_PER_ENTITY, "true" ); - addStringParameter( generator, "table", prms, TableGenerator.TABLE_PARAM ); - addStringParameter( generator, "pkColumnName", prms, TableGenerator.SEGMENT_COLUMN_PARAM ); - addStringParameter( generator, "pkColumnValue", prms, TableGenerator.SEGMENT_VALUE_PARAM ); - addStringParameter( generator, "valueColumnName", prms, TableGenerator.VALUE_COLUMN_PARAM ); - prms.put( + parameterMap.put( TableGenerator.CONFIG_PREFER_SEGMENT_PER_ENTITY, "true" ); + addStringParameter( generator, "table", parameterMap, TableGenerator.TABLE_PARAM ); + addStringParameter( generator, "pkColumnName", parameterMap, TableGenerator.SEGMENT_COLUMN_PARAM ); + addStringParameter( generator, "pkColumnValue", parameterMap, TableGenerator.SEGMENT_VALUE_PARAM ); + addStringParameter( generator, "valueColumnName", parameterMap, TableGenerator.VALUE_COLUMN_PARAM ); + parameterMap.put( TableGenerator.INCREMENT_PARAM, String.valueOf( JandexHelper.getValue( generator, "allocationSize", String.class ) ) ); - prms.put( + parameterMap.put( TableGenerator.INITIAL_PARAM, String.valueOf( JandexHelper.getValue( generator, "initialValue", String.class ) + 1 ) ); } else { - addStringParameter( generator, "table", prms, MultipleHiLoPerTableGenerator.ID_TABLE ); - addStringParameter( generator, "pkColumnName", prms, MultipleHiLoPerTableGenerator.PK_COLUMN_NAME ); - addStringParameter( generator, "pkColumnValue", prms, MultipleHiLoPerTableGenerator.PK_VALUE_NAME ); - addStringParameter( generator, "valueColumnName", prms, MultipleHiLoPerTableGenerator.VALUE_COLUMN_NAME ); - prms.put( + addStringParameter( generator, "table", parameterMap, MultipleHiLoPerTableGenerator.ID_TABLE ); + addStringParameter( generator, "pkColumnName", parameterMap, MultipleHiLoPerTableGenerator.PK_COLUMN_NAME ); + addStringParameter( generator, "pkColumnValue", parameterMap, MultipleHiLoPerTableGenerator.PK_VALUE_NAME ); + addStringParameter( generator, "valueColumnName", parameterMap, MultipleHiLoPerTableGenerator.VALUE_COLUMN_NAME ); + parameterMap.put( TableHiLoGenerator.MAX_LO, String.valueOf( JandexHelper.getValue( generator, "allocationSize", Integer.class ) - 1 ) ); @@ -204,27 +208,7 @@ public class IdGeneratorBinder { if ( JandexHelper.getValue( generator, "uniqueConstraints", AnnotationInstance[].class ).length > 0 ) { LOG.ignoringTableGeneratorConstraints( name ); } - metadata.addIdGenerator( new IdGenerator( name, strategy, prms ) ); + metadata.addIdGenerator( new IdGenerator( name, strategy, parameterMap ) ); LOG.tracef( "Add table generator with name: %s", name ); } - - public static String generatorType(GenerationType generatorEnum, boolean useNewGeneratorMappings) { - switch ( generatorEnum ) { - case IDENTITY: - return "identity"; - case AUTO: - return useNewGeneratorMappings - ? "enhanced-sequence" - : "native"; - case TABLE: - return useNewGeneratorMappings - ? "enhanced-table" - : MultipleHiLoPerTableGenerator.class.getName(); - case SEQUENCE: - return useNewGeneratorMappings - ? "enhanced-sequence" - : "seqhilo"; - } - throw new AssertionFailure( "Unknown GeneratorType: " + generatorEnum ); - } } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/xml/mocker/AccessHelper.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/xml/mocker/AccessHelper.java index 9bd9289ec4..034cf92499 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/xml/mocker/AccessHelper.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/xml/mocker/AccessHelper.java @@ -59,7 +59,7 @@ class AccessHelper implements JPADotNames { return null; } else { - return JandexHelper.getValueAsEnum( annotationInstance, "value", XMLAccessType.class ); + return JandexHelper.getEnumValue( annotationInstance, "value", XMLAccessType.class ); } } @@ -149,7 +149,7 @@ class AccessHelper implements JPADotNames { if ( MockHelper.isNotEmpty( accessAnnotationInstances ) ) { for ( AnnotationInstance annotationInstance : accessAnnotationInstances ) { if ( annotationInstance.target() != null && annotationInstance.target() instanceof ClassInfo ) { - return JandexHelper.getValueAsEnum( + return JandexHelper.getEnumValue( annotationInstance, "value", XMLAccessType.class @@ -179,7 +179,7 @@ class AccessHelper implements JPADotNames { continue; } if ( JandexHelper.getPropertyName( indexedPropertyTarget ).equals( attributeName ) ) { - XMLAccessType accessType = JandexHelper.getValueAsEnum( + XMLAccessType accessType = JandexHelper.getEnumValue( annotationInstance, "value", XMLAccessType.class diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/binder/Binder.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/binder/Binder.java index 42967483a2..c90806e4f8 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/binder/Binder.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/binder/Binder.java @@ -531,7 +531,7 @@ public class Binder { private Class determineJavaType(final Attribute attribute) { try { - final Class ownerClass = attribute.getAttributeContainer().getClassReference(); + final Class ownerClass = attribute.getAttributeContainer().getClassReference(); AttributeJavaTypeDeterminerDelegate delegate = new AttributeJavaTypeDeterminerDelegate( attribute.getName() ); BeanInfoHelper.visitBeanInfo( ownerClass, delegate ); return delegate.javaType; diff --git a/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/entity/IdentifierGeneratorTest.java b/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/entity/IdentifierGeneratorTest.java index bdbf362051..014fa1ae4c 100644 --- a/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/entity/IdentifierGeneratorTest.java +++ b/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/entity/IdentifierGeneratorTest.java @@ -1,20 +1,25 @@ package org.hibernate.metamodel.source.annotations.entity; -import java.util.Iterator; -import javax.persistence.Column; import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; import javax.persistence.Id; -import javax.persistence.SecondaryTable; import org.junit.Test; -import org.hibernate.AssertionFailure; +import org.hibernate.annotations.GenericGenerator; +import org.hibernate.id.IdentifierGenerator; +import org.hibernate.id.IdentityGenerator; +import org.hibernate.id.MultipleHiLoPerTableGenerator; +import org.hibernate.id.SequenceHiLoGenerator; +import org.hibernate.id.UUIDHexGenerator; +import org.hibernate.metamodel.MetadataSources; import org.hibernate.metamodel.binding.EntityBinding; -import org.hibernate.metamodel.relational.SimpleValue; -import org.hibernate.metamodel.relational.Table; +import org.hibernate.metamodel.source.MappingException; +import org.hibernate.service.ServiceRegistryBuilder; import static junit.framework.Assert.assertEquals; -import static junit.framework.Assert.assertFalse; +import static junit.framework.Assert.assertNull; import static junit.framework.Assert.assertTrue; import static junit.framework.Assert.fail; @@ -23,41 +28,124 @@ import static junit.framework.Assert.fail; */ public class IdentifierGeneratorTest extends BaseAnnotationBindingTestCase { @Entity - @SecondaryTable(name = "SECOND_TABLE") - class EntityWithSecondaryTable { + class NoGenerationEntity { @Id private long id; - - @Column(table = "SECOND_TABLE") - private String name; } @Test - @Resources(annotatedClasses = EntityWithSecondaryTable.class) - public void testSecondaryTableExists() { - EntityBinding binding = getEntityBinding( EntityWithSecondaryTable.class ); - Table table = (Table) binding.getTable( "SECOND_TABLE" ); - assertEquals( "The secondary table should exist", "SECOND_TABLE", table.getTableName().getName() ); + @Resources(annotatedClasses = NoGenerationEntity.class) + public void testNoIdGeneration() { + EntityBinding binding = getEntityBinding( NoGenerationEntity.class ); + IdentifierGenerator generator = binding.getHierarchyDetails().getEntityIdentifier().getIdentifierGenerator(); + assertNull( generator ); + } - Iterator valueIterator = table.values().iterator(); - assertTrue( valueIterator.hasNext() ); - org.hibernate.metamodel.relational.Column column = (org.hibernate.metamodel.relational.Column) valueIterator.next(); - assertEquals( "Wrong column name", "name", column.getColumnName().getName() ); - assertFalse( valueIterator.hasNext() ); + @Entity + class AutoEntity { + @Id + @GeneratedValue + private long id; + + public long getId() { + return id; + } } @Test - @Resources(annotatedClasses = EntityWithSecondaryTable.class) - public void testRetrievingUnknownTable() { - EntityBinding binding = getEntityBinding( EntityWithSecondaryTable.class ); + @Resources(annotatedClasses = AutoEntity.class) + public void testAutoGenerationType() { + EntityBinding binding = getEntityBinding( AutoEntity.class ); + IdentifierGenerator generator = binding.getHierarchyDetails().getEntityIdentifier().getIdentifierGenerator(); + + assertEquals( "Wrong generator", IdentityGenerator.class, generator.getClass() ); + } + + @Entity + class TableEntity { + @Id + @GeneratedValue(strategy = GenerationType.TABLE) + private long id; + + public long getId() { + return id; + } + } + + @Test + @Resources(annotatedClasses = TableEntity.class) + public void testTableGenerationType() { + EntityBinding binding = getEntityBinding( TableEntity.class ); + IdentifierGenerator generator = binding.getHierarchyDetails().getEntityIdentifier().getIdentifierGenerator(); + + assertEquals( "Wrong generator", MultipleHiLoPerTableGenerator.class, generator.getClass() ); + } + + @Entity + class SequenceEntity { + @Id + @GeneratedValue(strategy = GenerationType.SEQUENCE) + private long id; + + public long getId() { + return id; + } + } + + @Test + @Resources(annotatedClasses = SequenceEntity.class) + public void testSequenceGenerationType() { + EntityBinding binding = getEntityBinding( SequenceEntity.class ); + IdentifierGenerator generator = binding.getHierarchyDetails().getEntityIdentifier().getIdentifierGenerator(); + + assertEquals( "Wrong generator", SequenceHiLoGenerator.class, generator.getClass() ); + } + + + @Entity + class NamedGeneratorEntity { + @Id + @GeneratedValue(generator = "my-generator") + private long id; + + public long getId() { + return id; + } + } + + @Test + public void testUndefinedGenerator() { try { - binding.getTable( "FOO" ); + sources = new MetadataSources( new ServiceRegistryBuilder().buildServiceRegistry() ); + sources.addAnnotatedClass( NamedGeneratorEntity.class ); + sources.buildMetadata(); fail(); } - catch ( AssertionFailure e ) { - assertTrue( e.getMessage().startsWith( "Unable to find table" ) ); + catch ( MappingException e ) { + assertTrue( e.getMessage().startsWith( "Unable to find named generator" ) ); } } + + @Entity + @GenericGenerator(name = "my-generator", strategy = "uuid") + class NamedGeneratorEntity2 { + @Id + @GeneratedValue(generator = "my-generator") + private long id; + + public long getId() { + return id; + } + } + + @Test + @Resources(annotatedClasses = NamedGeneratorEntity2.class) + public void testNamedGenerator() { + EntityBinding binding = getEntityBinding( NamedGeneratorEntity2.class ); + IdentifierGenerator generator = binding.getHierarchyDetails().getEntityIdentifier().getIdentifierGenerator(); + + assertEquals( "Wrong generator", UUIDHexGenerator.class, generator.getClass() ); + } }