From 4ec26fa4cf59dad8bf938f7a6f86aa8dba9eabbb Mon Sep 17 00:00:00 2001 From: Steve Ebersole Date: Wed, 27 Jan 2010 08:00:27 +0000 Subject: [PATCH] HHH-4690 - Consider adding a flag for legacy/new generators git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@18642 1b8cb986-b30d-0410-93ca-fae66ebed9b2 --- .../org/hibernate/cfg/AnnotationBinder.java | 136 +++++++++++++----- .../cfg/AnnotationConfiguration.java | 19 ++- .../org/hibernate/cfg/ExtendedMappings.java | 8 ++ .../hibernate/test/annotations/TestCase.java | 5 + .../id/generationmappings/AutoEntity.java | 48 +++++++ .../CompleteSequenceEntity.java | 58 ++++++++ .../MinimalSequenceEntity.java | 51 +++++++ .../MinimalTableEntity.java | 55 +++++++ .../NewGeneratorMappingsTest.java | 122 ++++++++++++++++ .../src/test/resources/hibernate.properties | 38 +++-- .../id/enhanced/DatabaseStructure.java | 6 + .../id/enhanced/SequenceStructure.java | 7 + .../hibernate/id/enhanced/TableStructure.java | 7 + 13 files changed, 506 insertions(+), 54 deletions(-) create mode 100644 annotations/src/test/java/org/hibernate/test/annotations/id/generationmappings/AutoEntity.java create mode 100644 annotations/src/test/java/org/hibernate/test/annotations/id/generationmappings/CompleteSequenceEntity.java create mode 100644 annotations/src/test/java/org/hibernate/test/annotations/id/generationmappings/MinimalSequenceEntity.java create mode 100644 annotations/src/test/java/org/hibernate/test/annotations/id/generationmappings/MinimalTableEntity.java create mode 100644 annotations/src/test/java/org/hibernate/test/annotations/id/generationmappings/NewGeneratorMappingsTest.java diff --git a/annotations/src/main/java/org/hibernate/cfg/AnnotationBinder.java b/annotations/src/main/java/org/hibernate/cfg/AnnotationBinder.java index f70b16c472..8a79c13ccc 100644 --- a/annotations/src/main/java/org/hibernate/cfg/AnnotationBinder.java +++ b/annotations/src/main/java/org/hibernate/cfg/AnnotationBinder.java @@ -146,6 +146,7 @@ import org.hibernate.id.MultipleHiLoPerTableGenerator; import org.hibernate.id.PersistentIdentifierGenerator; import org.hibernate.id.SequenceHiLoGenerator; import org.hibernate.id.TableHiLoGenerator; +import org.hibernate.id.enhanced.SequenceStyleGenerator; import org.hibernate.mapping.Any; import org.hibernate.mapping.Component; import org.hibernate.mapping.DependantValue; @@ -348,7 +349,7 @@ public final class } } - private static IdGenerator buildIdGenerator(java.lang.annotation.Annotation ann, Mappings mappings) { + private static IdGenerator buildIdGenerator(java.lang.annotation.Annotation ann, ExtendedMappings mappings) { IdGenerator idGen = new IdGenerator(); if ( mappings.getSchemaName() != null ) { idGen.addParam( PersistentIdentifierGenerator.SCHEMA, mappings.getSchemaName() ); @@ -356,54 +357,106 @@ public final class if ( mappings.getCatalogName() != null ) { idGen.addParam( PersistentIdentifierGenerator.CATALOG, mappings.getCatalogName() ); } + final boolean useNewGeneratorMappings = mappings.useNewGeneratorMappings(); if ( ann == null ) { idGen = null; } else if ( ann instanceof TableGenerator ) { TableGenerator tabGen = (TableGenerator) ann; idGen.setName( tabGen.name() ); - idGen.setIdentifierGeneratorStrategy( MultipleHiLoPerTableGenerator.class.getName() ); + if ( useNewGeneratorMappings ) { + idGen.setIdentifierGeneratorStrategy( org.hibernate.id.enhanced.TableGenerator.class.getName() ); + idGen.addParam( org.hibernate.id.enhanced.TableGenerator.CONFIG_PREFER_SEGMENT_PER_ENTITY, "true" ); - if ( !BinderHelper.isDefault( tabGen.table() ) ) { - idGen.addParam( MultipleHiLoPerTableGenerator.ID_TABLE, tabGen.table() ); + if ( !BinderHelper.isDefault( tabGen.catalog() ) ) { + idGen.addParam( org.hibernate.id.enhanced.TableGenerator.CATALOG, tabGen.catalog() ); + } + if ( !BinderHelper.isDefault( tabGen.schema() ) ) { + idGen.addParam( org.hibernate.id.enhanced.TableGenerator.SCHEMA, tabGen.schema() ); + } + if ( !BinderHelper.isDefault( tabGen.table() ) ) { + idGen.addParam( org.hibernate.id.enhanced.TableGenerator.TABLE_PARAM, tabGen.table() ); + } + if ( !BinderHelper.isDefault( tabGen.pkColumnName() ) ) { + idGen.addParam( org.hibernate.id.enhanced.TableGenerator.SEGMENT_COLUMN_PARAM, tabGen.pkColumnName() ); + } + if ( !BinderHelper.isDefault( tabGen.pkColumnValue() ) ) { + idGen.addParam( org.hibernate.id.enhanced.TableGenerator.SEGMENT_VALUE_PARAM, tabGen.pkColumnValue() ); + } + if ( !BinderHelper.isDefault( tabGen.valueColumnName() ) ) { + idGen.addParam( org.hibernate.id.enhanced.TableGenerator.VALUE_COLUMN_PARAM, tabGen.valueColumnName() ); + } + idGen.addParam( org.hibernate.id.enhanced.TableGenerator.INCREMENT_PARAM, String.valueOf( tabGen.allocationSize() ) ); + idGen.addParam( org.hibernate.id.enhanced.TableGenerator.INITIAL_PARAM, String.valueOf( tabGen.initialValue() ) ); + if ( tabGen.uniqueConstraints() != null && tabGen.uniqueConstraints().length > 0 ) { + log.warn( "Ignoring unique constraints specified on table generator [{}]", tabGen.name() ); + } } - if ( !BinderHelper.isDefault( tabGen.catalog() ) ) { - idGen.addParam( MultipleHiLoPerTableGenerator.CATALOG, tabGen.catalog() ); - } - if ( !BinderHelper.isDefault( tabGen.schema() ) ) { - idGen.addParam( MultipleHiLoPerTableGenerator.SCHEMA, tabGen.schema() ); - } - //FIXME implements uniqueconstrains + else { + idGen.setIdentifierGeneratorStrategy( MultipleHiLoPerTableGenerator.class.getName() ); - if ( !BinderHelper.isDefault( tabGen.pkColumnName() ) ) { - idGen.addParam( MultipleHiLoPerTableGenerator.PK_COLUMN_NAME, tabGen.pkColumnName() ); + if ( !BinderHelper.isDefault( tabGen.table() ) ) { + idGen.addParam( MultipleHiLoPerTableGenerator.ID_TABLE, tabGen.table() ); + } + if ( !BinderHelper.isDefault( tabGen.catalog() ) ) { + idGen.addParam( MultipleHiLoPerTableGenerator.CATALOG, tabGen.catalog() ); + } + if ( !BinderHelper.isDefault( tabGen.schema() ) ) { + idGen.addParam( MultipleHiLoPerTableGenerator.SCHEMA, tabGen.schema() ); + } + //FIXME implement uniqueconstrains + if ( tabGen.uniqueConstraints() != null && tabGen.uniqueConstraints().length > 0 ) { + log.warn( "Ignoring unique constraints specified on table generator [{}]", tabGen.name() ); + } + + if ( !BinderHelper.isDefault( tabGen.pkColumnName() ) ) { + idGen.addParam( MultipleHiLoPerTableGenerator.PK_COLUMN_NAME, tabGen.pkColumnName() ); + } + if ( !BinderHelper.isDefault( tabGen.valueColumnName() ) ) { + idGen.addParam( MultipleHiLoPerTableGenerator.VALUE_COLUMN_NAME, tabGen.valueColumnName() ); + } + if ( !BinderHelper.isDefault( tabGen.pkColumnValue() ) ) { + idGen.addParam( MultipleHiLoPerTableGenerator.PK_VALUE_NAME, tabGen.pkColumnValue() ); + } + idGen.addParam( TableHiLoGenerator.MAX_LO, String.valueOf( tabGen.allocationSize() - 1 ) ); } - if ( !BinderHelper.isDefault( tabGen.valueColumnName() ) ) { - idGen.addParam( MultipleHiLoPerTableGenerator.VALUE_COLUMN_NAME, tabGen.valueColumnName() ); - } - if ( !BinderHelper.isDefault( tabGen.pkColumnValue() ) ) { - idGen.addParam( MultipleHiLoPerTableGenerator.PK_VALUE_NAME, tabGen.pkColumnValue() ); - } - idGen.addParam( TableHiLoGenerator.MAX_LO, String.valueOf( tabGen.allocationSize() - 1 ) ); log.trace( "Add table generator with name: {}", idGen.getName() ); } else if ( ann instanceof SequenceGenerator ) { SequenceGenerator seqGen = (SequenceGenerator) ann; idGen.setName( seqGen.name() ); - idGen.setIdentifierGeneratorStrategy( "seqhilo" ); + if ( useNewGeneratorMappings ) { + idGen.setIdentifierGeneratorStrategy( SequenceStyleGenerator.class.getName() ); - if ( !BinderHelper.isDefault( seqGen.sequenceName() ) ) { - idGen.addParam( org.hibernate.id.SequenceGenerator.SEQUENCE, seqGen.sequenceName() ); + if ( !BinderHelper.isDefault( seqGen.catalog() ) ) { + idGen.addParam( SequenceStyleGenerator.CATALOG, seqGen.catalog() ); + } + if ( !BinderHelper.isDefault( seqGen.schema() ) ) { + idGen.addParam( SequenceStyleGenerator.SCHEMA, seqGen.schema() ); + } + if ( !BinderHelper.isDefault( seqGen.sequenceName() ) ) { + idGen.addParam( SequenceStyleGenerator.SEQUENCE_PARAM, seqGen.sequenceName() ); + } + idGen.addParam( SequenceStyleGenerator.INCREMENT_PARAM, String.valueOf( seqGen.allocationSize() ) ); + idGen.addParam( SequenceStyleGenerator.INITIAL_PARAM, String.valueOf( seqGen.initialValue() ) ); } - //FIXME: work on initialValue() through SequenceGenerator.PARAMETERS - // steve : or just use o.h.id.enhanced.SequenceStyleGenerator - if ( seqGen.initialValue() != 1 ) { - log.warn( - "Hibernate does not support SequenceGenerator.initialValue()" - ); + else { + idGen.setIdentifierGeneratorStrategy( "seqhilo" ); + + if ( !BinderHelper.isDefault( seqGen.sequenceName() ) ) { + idGen.addParam( org.hibernate.id.SequenceGenerator.SEQUENCE, seqGen.sequenceName() ); + } + //FIXME: work on initialValue() through SequenceGenerator.PARAMETERS + // steve : or just use o.h.id.enhanced.SequenceStyleGenerator + if ( seqGen.initialValue() != 1 ) { + log.warn( + "Hibernate does not support SequenceGenerator.initialValue() unless '{}' set", + AnnotationConfiguration.USE_NEW_ID_GENERATOR_MAPPINGS + ); + } + idGen.addParam( SequenceHiLoGenerator.MAX_LO, String.valueOf( seqGen.allocationSize() - 1 ) ); + log.trace( "Add sequence generator with name: {}", idGen.getName() ); } - idGen.addParam( SequenceHiLoGenerator.MAX_LO, String.valueOf( seqGen.allocationSize() - 1 ) ); - log.trace( "Add sequence generator with name: {}", idGen.getName() ); } else if ( ann instanceof GenericGenerator ) { GenericGenerator genGen = (GenericGenerator) ann; @@ -1804,7 +1857,7 @@ public final class GeneratedValue generatedValue = property.getAnnotation( GeneratedValue.class ); String generatorType = generatedValue != null ? - generatorType( generatedValue.strategy() ) : + generatorType( generatedValue.strategy(), mappings ) : "assigned"; String generatorName = generatedValue != null ? generatedValue.generator() : @@ -2032,7 +2085,7 @@ public final class localGenerators.putAll( buildLocalGenerators( property, mappings ) ); GeneratedValue generatedValue = property.getAnnotation( GeneratedValue.class ); - String generatorType = generatedValue != null ? generatorType( generatedValue.strategy() ) : "assigned"; + String generatorType = generatedValue != null ? generatorType( generatedValue.strategy(), mappings ) : "assigned"; String generator = generatedValue != null ? generatedValue.generator() : BinderHelper.ANNOTATION_STRING_DEFAULT; BinderHelper.makeIdGenerator( (SimpleValue) comp.getProperty(property.getName()).getValue(), generatorType, generator, mappings, localGenerators); @@ -2395,16 +2448,23 @@ public final class propertyHolder.addProperty( prop, columns, inferredData.getDeclaringClass() ); } - private static String generatorType(GenerationType generatorEnum) { + private static String generatorType(GenerationType generatorEnum, ExtendedMappings mappings) { + boolean useNewGeneratorMappings = mappings.useNewGeneratorMappings(); switch ( generatorEnum ) { case IDENTITY: return "identity"; case AUTO: - return "native"; + return useNewGeneratorMappings + ? org.hibernate.id.enhanced.SequenceStyleGenerator.class.getName() + : "native"; case TABLE: - return MultipleHiLoPerTableGenerator.class.getName(); + return useNewGeneratorMappings + ? org.hibernate.id.enhanced.TableGenerator.class.getName() + : MultipleHiLoPerTableGenerator.class.getName(); case SEQUENCE: - return "seqhilo"; + return useNewGeneratorMappings + ? org.hibernate.id.enhanced.SequenceStyleGenerator.class.getName() + : "seqhilo"; } throw new AssertionFailure( "Unknown GeneratorType: " + generatorEnum ); } @@ -2509,7 +2569,7 @@ public final class } } - private static HashMap buildLocalGenerators(XAnnotatedElement annElt, Mappings mappings) { + private static HashMap buildLocalGenerators(XAnnotatedElement annElt, ExtendedMappings mappings) { HashMap generators = new HashMap(); TableGenerator tabGen = annElt.getAnnotation( TableGenerator.class ); SequenceGenerator seqGen = annElt.getAnnotation( SequenceGenerator.class ); diff --git a/annotations/src/main/java/org/hibernate/cfg/AnnotationConfiguration.java b/annotations/src/main/java/org/hibernate/cfg/AnnotationConfiguration.java index 0fe182b21b..b0b686671b 100644 --- a/annotations/src/main/java/org/hibernate/cfg/AnnotationConfiguration.java +++ b/annotations/src/main/java/org/hibernate/cfg/AnnotationConfiguration.java @@ -108,10 +108,16 @@ public class AnnotationConfiguration extends Configuration { /** * Setting used to give the name of the default {@link org.hibernate.annotations.CacheConcurrencyStrategy} * to use when either {@link javax.persistence.Cacheable @Cacheable} or - * {@link Cache @Cache} is used. {@link Cache @Cache(strategy=".."} is used to override. + * {@link Cache @Cache} is used. {@link Cache @Cache(strategy="..")} is used to override. */ public static final String DEFAULT_CACHE_CONCURRENCY_STRATEGY = "org.hibernate.cache.default_cache_concurrency_strategy"; + /** + * Setting which indicates the Hibernate {@link org.hibernate.id.IdentifierGenerator} name to use as the mapping + * for {@link javax.persistence.GenerationType#AUTO @GeneratedValue(strategy=AUTO...)} + */ + public static final String USE_NEW_ID_GENERATOR_MAPPINGS = "org.hibernate.id.new_generator_mappings"; + /** * Class name of the class needed to enable Search. */ @@ -1286,6 +1292,17 @@ public class AnnotationConfiguration extends Configuration { map.put( property.getProperty().getAnnotation( MapsId.class ).value(), property ); } + private Boolean useNewGeneratorMappings; + + @SuppressWarnings({ "UnnecessaryUnboxing" }) + public boolean useNewGeneratorMappings() { + if ( useNewGeneratorMappings == null ) { + final String booleanName = getConfigurationProperties().getProperty( USE_NEW_ID_GENERATOR_MAPPINGS ); + useNewGeneratorMappings = Boolean.valueOf( booleanName ); + } + return useNewGeneratorMappings.booleanValue(); + } + public IdGenerator getGenerator(String name) { return getGenerator( name, null ); } diff --git a/annotations/src/main/java/org/hibernate/cfg/ExtendedMappings.java b/annotations/src/main/java/org/hibernate/cfg/ExtendedMappings.java index 2bbf9180bd..c552a2af02 100644 --- a/annotations/src/main/java/org/hibernate/cfg/ExtendedMappings.java +++ b/annotations/src/main/java/org/hibernate/cfg/ExtendedMappings.java @@ -180,4 +180,12 @@ public interface ExtendedMappings extends Mappings { public PropertyData getPropertyAnnotatedWithMapsId(XClass entityType, String propertyName); public void addPropertyAnnotatedWithMapsId(XClass entityType, PropertyData property); + + /** + * Should we use the new generator strategy mappings. This is controlled by the + * {@link AnnotationConfiguration#USE_NEW_ID_GENERATOR_MAPPINGS} setting. + * + * @return True if the new generators should be used, false otherwise. + */ + public boolean useNewGeneratorMappings(); } \ No newline at end of file diff --git a/annotations/src/test/java/org/hibernate/test/annotations/TestCase.java b/annotations/src/test/java/org/hibernate/test/annotations/TestCase.java index d829b582f2..b6b7e2f483 100644 --- a/annotations/src/test/java/org/hibernate/test/annotations/TestCase.java +++ b/annotations/src/test/java/org/hibernate/test/annotations/TestCase.java @@ -32,6 +32,7 @@ import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.AnnotationConfiguration; import org.hibernate.cfg.Environment; +import org.hibernate.engine.SessionFactoryImplementor; /** * A base class for all annotation tests. @@ -83,6 +84,10 @@ public abstract class TestCase extends HibernateTestCase { return sessions; } + protected SessionFactoryImplementor sfi() { + return (SessionFactoryImplementor) getSessions(); + } + @Override protected void buildConfiguration() throws Exception { if ( getSessions() != null ) { diff --git a/annotations/src/test/java/org/hibernate/test/annotations/id/generationmappings/AutoEntity.java b/annotations/src/test/java/org/hibernate/test/annotations/id/generationmappings/AutoEntity.java new file mode 100644 index 0000000000..a3cd39d381 --- /dev/null +++ b/annotations/src/test/java/org/hibernate/test/annotations/id/generationmappings/AutoEntity.java @@ -0,0 +1,48 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2010, 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.test.annotations.id.generationmappings; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; + +/** + * TODO : javadoc + * + * @author Steve Ebersole + */ +@Entity +public class AutoEntity { + private Long id; + + @Id @GeneratedValue + public Long getId() { + return id; + } + + public void setId(Long long1) { + id = long1; + } +} diff --git a/annotations/src/test/java/org/hibernate/test/annotations/id/generationmappings/CompleteSequenceEntity.java b/annotations/src/test/java/org/hibernate/test/annotations/id/generationmappings/CompleteSequenceEntity.java new file mode 100644 index 0000000000..aa000c6b57 --- /dev/null +++ b/annotations/src/test/java/org/hibernate/test/annotations/id/generationmappings/CompleteSequenceEntity.java @@ -0,0 +1,58 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2010, 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.test.annotations.id.generationmappings; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; + +/** + * TODO : javadoc + * + * @author Steve Ebersole + */ +@Entity +public class CompleteSequenceEntity { + public static final String SEQ_NAME = "some_other_sequence"; + private Long id; + + @Id + @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "COMPLETE_SEQ") + @javax.persistence.SequenceGenerator( + name = "COMPLETE_SEQ", + sequenceName = SEQ_NAME, + initialValue = 1000, + allocationSize = 52, + catalog = "my_catalog", + schema = "my_schema" + ) + public Long getId() { + return id; + } + + public void setId(Long long1) { + id = long1; + } +} \ No newline at end of file diff --git a/annotations/src/test/java/org/hibernate/test/annotations/id/generationmappings/MinimalSequenceEntity.java b/annotations/src/test/java/org/hibernate/test/annotations/id/generationmappings/MinimalSequenceEntity.java new file mode 100644 index 0000000000..6a4b738d5d --- /dev/null +++ b/annotations/src/test/java/org/hibernate/test/annotations/id/generationmappings/MinimalSequenceEntity.java @@ -0,0 +1,51 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2010, 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.test.annotations.id.generationmappings; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; + +/** + * TODO : javadoc + * + * @author Steve Ebersole + */ +@Entity +public class MinimalSequenceEntity { + public static final String SEQ_NAME = "some_sequence"; + private Long id; + + @Id + @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "MINIMAL_SEQ") + @javax.persistence.SequenceGenerator( name = "MINIMAL_SEQ", sequenceName = SEQ_NAME ) + public Long getId() { + return id; + } + + public void setId(Long long1) { + id = long1; + } +} diff --git a/annotations/src/test/java/org/hibernate/test/annotations/id/generationmappings/MinimalTableEntity.java b/annotations/src/test/java/org/hibernate/test/annotations/id/generationmappings/MinimalTableEntity.java new file mode 100644 index 0000000000..607f836c17 --- /dev/null +++ b/annotations/src/test/java/org/hibernate/test/annotations/id/generationmappings/MinimalTableEntity.java @@ -0,0 +1,55 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2010, 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.test.annotations.id.generationmappings; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.Table; +import javax.persistence.TableGenerator; + +/** + * TODO : javadoc + * + * @author Steve Ebersole + */ +@Entity +@Table( name = "MINIMAL_TBL" ) +public class MinimalTableEntity { + public static final String TBL_NAME = "minimal_tbl"; + + private Long id; + + @Id + @GeneratedValue(strategy = GenerationType.TABLE, generator = "MINIMAL_TBL") + @TableGenerator( name = "MINIMAL_TBL", table = TBL_NAME ) + public Long getId() { + return id; + } + + public void setId(Long long1) { + id = long1; + } +} \ No newline at end of file diff --git a/annotations/src/test/java/org/hibernate/test/annotations/id/generationmappings/NewGeneratorMappingsTest.java b/annotations/src/test/java/org/hibernate/test/annotations/id/generationmappings/NewGeneratorMappingsTest.java new file mode 100644 index 0000000000..399d2f7e3f --- /dev/null +++ b/annotations/src/test/java/org/hibernate/test/annotations/id/generationmappings/NewGeneratorMappingsTest.java @@ -0,0 +1,122 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2010, 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.test.annotations.id.generationmappings; + +import org.hibernate.cfg.AnnotationConfiguration; +import org.hibernate.cfg.Configuration; +import org.hibernate.cfg.Environment; +import org.hibernate.id.IdentifierGenerator; +import org.hibernate.id.enhanced.OptimizerFactory; +import org.hibernate.id.enhanced.SequenceStyleGenerator; +import org.hibernate.id.enhanced.TableGenerator; +import org.hibernate.persister.entity.EntityPersister; +import org.hibernate.test.annotations.TestCase; + +/** + * Test mapping the {@link javax.persistence.GenerationType GenerationTypes} to the corresponding + * hibernate generators using the new scheme + * + * @author Steve Ebersole + */ +public class NewGeneratorMappingsTest extends TestCase { + @Override + protected Class[] getAnnotatedClasses() { + return new Class[] { + MinimalSequenceEntity.class, + CompleteSequenceEntity.class, + AutoEntity.class, + MinimalTableEntity.class + }; + } + + @Override + protected void configure(Configuration cfg) { + super.configure( cfg ); + cfg.setProperty( AnnotationConfiguration.USE_NEW_ID_GENERATOR_MAPPINGS, "true" ); + cfg.setProperty( Environment.HBM2DDL_AUTO, "" ); + } + + @Override + protected boolean recreateSchema() { + return false; + } + + @Override + protected void runSchemaGeneration() { + } + + @Override + protected void runSchemaDrop() { + } + + public void testMinimalSequenceEntity() { + final EntityPersister persister = sfi().getEntityPersister( MinimalSequenceEntity.class.getName() ); + IdentifierGenerator generator = persister.getIdentifierGenerator(); + assertTrue( SequenceStyleGenerator.class.isInstance( generator ) ); + SequenceStyleGenerator seqGenerator = (SequenceStyleGenerator) generator; + assertEquals( MinimalSequenceEntity.SEQ_NAME, seqGenerator.getDatabaseStructure().getName() ); + // 1 is the annotation default + assertEquals( 1, seqGenerator.getDatabaseStructure().getInitialValue() ); + // 50 is the annotation default + assertEquals( 50, seqGenerator.getDatabaseStructure().getIncrementSize() ); + assertFalse( OptimizerFactory.NoopOptimizer.class.isInstance( seqGenerator.getOptimizer() ) ); + } + + public void testCompleteSequenceEntity() { + final EntityPersister persister = sfi().getEntityPersister( CompleteSequenceEntity.class.getName() ); + IdentifierGenerator generator = persister.getIdentifierGenerator(); + assertTrue( SequenceStyleGenerator.class.isInstance( generator ) ); + SequenceStyleGenerator seqGenerator = (SequenceStyleGenerator) generator; + assertEquals( "my_catalog.my_schema."+CompleteSequenceEntity.SEQ_NAME, seqGenerator.getDatabaseStructure().getName() ); + assertEquals( 1000, seqGenerator.getDatabaseStructure().getInitialValue() ); + assertEquals( 52, seqGenerator.getDatabaseStructure().getIncrementSize() ); + assertFalse( OptimizerFactory.NoopOptimizer.class.isInstance( seqGenerator.getOptimizer() ) ); + } + + public void testAutoEntity() { + final EntityPersister persister = sfi().getEntityPersister( AutoEntity.class.getName() ); + IdentifierGenerator generator = persister.getIdentifierGenerator(); + assertTrue( SequenceStyleGenerator.class.isInstance( generator ) ); + SequenceStyleGenerator seqGenerator = (SequenceStyleGenerator) generator; + assertEquals( SequenceStyleGenerator.DEF_SEQUENCE_NAME, seqGenerator.getDatabaseStructure().getName() ); + assertEquals( SequenceStyleGenerator.DEFAULT_INITIAL_VALUE, seqGenerator.getDatabaseStructure().getInitialValue() ); + assertEquals( SequenceStyleGenerator.DEFAULT_INCREMENT_SIZE, seqGenerator.getDatabaseStructure().getIncrementSize() ); + } + + public void testMinimalTableEntity() { + final EntityPersister persister = sfi().getEntityPersister( MinimalTableEntity.class.getName() ); + IdentifierGenerator generator = persister.getIdentifierGenerator(); + assertTrue( TableGenerator.class.isInstance( generator ) ); + TableGenerator tabGenerator = (TableGenerator) generator; + assertEquals( MinimalTableEntity.TBL_NAME, tabGenerator.getTableName() ); + assertEquals( TableGenerator.DEF_SEGMENT_COLUMN, tabGenerator.getSegmentColumnName() ); + assertEquals( "MINIMAL_TBL", tabGenerator.getSegmentValue() ); + assertEquals( TableGenerator.DEF_VALUE_COLUMN, tabGenerator.getValueColumnName() ); + // 0 is the annotation default + assertEquals( 0, tabGenerator.getInitialValue() ); + // 50 is the annotation default + assertEquals( 50, tabGenerator.getIncrementSize() ); + assertTrue( OptimizerFactory.PooledOptimizer.class.isInstance( tabGenerator.getOptimizer() ) ); + } +} diff --git a/annotations/src/test/resources/hibernate.properties b/annotations/src/test/resources/hibernate.properties index b6eeb1a820..8a96be5568 100644 --- a/annotations/src/test/resources/hibernate.properties +++ b/annotations/src/test/resources/hibernate.properties @@ -1,18 +1,26 @@ -################################################################################ -# Copyright (c) 2007, Red Hat Middleware, LLC. All rights reserved. # -# # -# 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, v. 2.1. This program is distributed in the # -# hope that it will be useful, but WITHOUT A 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, v.2.1 along with this # -# distribution; if not, write to the Free Software Foundation, Inc., # -# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. # -# # -# Red Hat Author(s): Steve Ebersole # -################################################################################ +# +# Hibernate, Relational Persistence for Idiomatic Java +# +# Copyright (c) 2010, 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 +# hibernate.dialect ${db.dialect} hibernate.connection.driver_class ${jdbc.driver} hibernate.connection.url ${jdbc.url} diff --git a/core/src/main/java/org/hibernate/id/enhanced/DatabaseStructure.java b/core/src/main/java/org/hibernate/id/enhanced/DatabaseStructure.java index 182f066375..1ce605da88 100644 --- a/core/src/main/java/org/hibernate/id/enhanced/DatabaseStructure.java +++ b/core/src/main/java/org/hibernate/id/enhanced/DatabaseStructure.java @@ -46,6 +46,12 @@ public interface DatabaseStructure { */ public int getTimesAccessed(); + /** + * The configured initial value + * @return The configured initial value + */ + public int getInitialValue(); + /** * The configured increment size * @return The configured increment size diff --git a/core/src/main/java/org/hibernate/id/enhanced/SequenceStructure.java b/core/src/main/java/org/hibernate/id/enhanced/SequenceStructure.java index d7f5fb5ca5..d4a7c95ad8 100644 --- a/core/src/main/java/org/hibernate/id/enhanced/SequenceStructure.java +++ b/core/src/main/java/org/hibernate/id/enhanced/SequenceStructure.java @@ -79,6 +79,13 @@ public class SequenceStructure implements DatabaseStructure { return accessCounter; } + /** + * {@inheritDoc} + */ + public int getInitialValue() { + return initialValue; + } + /** * {@inheritDoc} */ diff --git a/core/src/main/java/org/hibernate/id/enhanced/TableStructure.java b/core/src/main/java/org/hibernate/id/enhanced/TableStructure.java index c1816fa631..bed34d7741 100644 --- a/core/src/main/java/org/hibernate/id/enhanced/TableStructure.java +++ b/core/src/main/java/org/hibernate/id/enhanced/TableStructure.java @@ -84,6 +84,13 @@ public class TableStructure extends TransactionHelper implements DatabaseStructu return tableName; } + /** + * {@inheritDoc} + */ + public int getInitialValue() { + return initialValue; + } + /** * {@inheritDoc} */