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
This commit is contained in:
Steve Ebersole 2010-01-27 08:00:27 +00:00
parent a0d8e6a415
commit 4ec26fa4cf
13 changed files with 506 additions and 54 deletions

View File

@ -146,6 +146,7 @@ import org.hibernate.id.MultipleHiLoPerTableGenerator;
import org.hibernate.id.PersistentIdentifierGenerator; import org.hibernate.id.PersistentIdentifierGenerator;
import org.hibernate.id.SequenceHiLoGenerator; import org.hibernate.id.SequenceHiLoGenerator;
import org.hibernate.id.TableHiLoGenerator; import org.hibernate.id.TableHiLoGenerator;
import org.hibernate.id.enhanced.SequenceStyleGenerator;
import org.hibernate.mapping.Any; import org.hibernate.mapping.Any;
import org.hibernate.mapping.Component; import org.hibernate.mapping.Component;
import org.hibernate.mapping.DependantValue; 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(); IdGenerator idGen = new IdGenerator();
if ( mappings.getSchemaName() != null ) { if ( mappings.getSchemaName() != null ) {
idGen.addParam( PersistentIdentifierGenerator.SCHEMA, mappings.getSchemaName() ); idGen.addParam( PersistentIdentifierGenerator.SCHEMA, mappings.getSchemaName() );
@ -356,12 +357,42 @@ public final class
if ( mappings.getCatalogName() != null ) { if ( mappings.getCatalogName() != null ) {
idGen.addParam( PersistentIdentifierGenerator.CATALOG, mappings.getCatalogName() ); idGen.addParam( PersistentIdentifierGenerator.CATALOG, mappings.getCatalogName() );
} }
final boolean useNewGeneratorMappings = mappings.useNewGeneratorMappings();
if ( ann == null ) { if ( ann == null ) {
idGen = null; idGen = null;
} }
else if ( ann instanceof TableGenerator ) { else if ( ann instanceof TableGenerator ) {
TableGenerator tabGen = (TableGenerator) ann; TableGenerator tabGen = (TableGenerator) ann;
idGen.setName( tabGen.name() ); idGen.setName( tabGen.name() );
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.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() );
}
}
else {
idGen.setIdentifierGeneratorStrategy( MultipleHiLoPerTableGenerator.class.getName() ); idGen.setIdentifierGeneratorStrategy( MultipleHiLoPerTableGenerator.class.getName() );
if ( !BinderHelper.isDefault( tabGen.table() ) ) { if ( !BinderHelper.isDefault( tabGen.table() ) ) {
@ -373,7 +404,10 @@ public final class
if ( !BinderHelper.isDefault( tabGen.schema() ) ) { if ( !BinderHelper.isDefault( tabGen.schema() ) ) {
idGen.addParam( MultipleHiLoPerTableGenerator.SCHEMA, tabGen.schema() ); idGen.addParam( MultipleHiLoPerTableGenerator.SCHEMA, tabGen.schema() );
} }
//FIXME implements uniqueconstrains //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() ) ) { if ( !BinderHelper.isDefault( tabGen.pkColumnName() ) ) {
idGen.addParam( MultipleHiLoPerTableGenerator.PK_COLUMN_NAME, tabGen.pkColumnName() ); idGen.addParam( MultipleHiLoPerTableGenerator.PK_COLUMN_NAME, tabGen.pkColumnName() );
@ -385,11 +419,28 @@ public final class
idGen.addParam( MultipleHiLoPerTableGenerator.PK_VALUE_NAME, tabGen.pkColumnValue() ); idGen.addParam( MultipleHiLoPerTableGenerator.PK_VALUE_NAME, tabGen.pkColumnValue() );
} }
idGen.addParam( TableHiLoGenerator.MAX_LO, String.valueOf( tabGen.allocationSize() - 1 ) ); idGen.addParam( TableHiLoGenerator.MAX_LO, String.valueOf( tabGen.allocationSize() - 1 ) );
}
log.trace( "Add table generator with name: {}", idGen.getName() ); log.trace( "Add table generator with name: {}", idGen.getName() );
} }
else if ( ann instanceof SequenceGenerator ) { else if ( ann instanceof SequenceGenerator ) {
SequenceGenerator seqGen = (SequenceGenerator) ann; SequenceGenerator seqGen = (SequenceGenerator) ann;
idGen.setName( seqGen.name() ); idGen.setName( seqGen.name() );
if ( useNewGeneratorMappings ) {
idGen.setIdentifierGeneratorStrategy( SequenceStyleGenerator.class.getName() );
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() ) );
}
else {
idGen.setIdentifierGeneratorStrategy( "seqhilo" ); idGen.setIdentifierGeneratorStrategy( "seqhilo" );
if ( !BinderHelper.isDefault( seqGen.sequenceName() ) ) { if ( !BinderHelper.isDefault( seqGen.sequenceName() ) ) {
@ -399,12 +450,14 @@ public final class
// steve : or just use o.h.id.enhanced.SequenceStyleGenerator // steve : or just use o.h.id.enhanced.SequenceStyleGenerator
if ( seqGen.initialValue() != 1 ) { if ( seqGen.initialValue() != 1 ) {
log.warn( log.warn(
"Hibernate does not support SequenceGenerator.initialValue()" "Hibernate does not support SequenceGenerator.initialValue() unless '{}' set",
AnnotationConfiguration.USE_NEW_ID_GENERATOR_MAPPINGS
); );
} }
idGen.addParam( SequenceHiLoGenerator.MAX_LO, String.valueOf( seqGen.allocationSize() - 1 ) ); idGen.addParam( SequenceHiLoGenerator.MAX_LO, String.valueOf( seqGen.allocationSize() - 1 ) );
log.trace( "Add sequence generator with name: {}", idGen.getName() ); log.trace( "Add sequence generator with name: {}", idGen.getName() );
} }
}
else if ( ann instanceof GenericGenerator ) { else if ( ann instanceof GenericGenerator ) {
GenericGenerator genGen = (GenericGenerator) ann; GenericGenerator genGen = (GenericGenerator) ann;
idGen.setName( genGen.name() ); idGen.setName( genGen.name() );
@ -1804,7 +1857,7 @@ public final class
GeneratedValue generatedValue = property.getAnnotation( GeneratedValue.class ); GeneratedValue generatedValue = property.getAnnotation( GeneratedValue.class );
String generatorType = generatedValue != null ? String generatorType = generatedValue != null ?
generatorType( generatedValue.strategy() ) : generatorType( generatedValue.strategy(), mappings ) :
"assigned"; "assigned";
String generatorName = generatedValue != null ? String generatorName = generatedValue != null ?
generatedValue.generator() : generatedValue.generator() :
@ -2032,7 +2085,7 @@ public final class
localGenerators.putAll( buildLocalGenerators( property, mappings ) ); localGenerators.putAll( buildLocalGenerators( property, mappings ) );
GeneratedValue generatedValue = property.getAnnotation( GeneratedValue.class ); 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; String generator = generatedValue != null ? generatedValue.generator() : BinderHelper.ANNOTATION_STRING_DEFAULT;
BinderHelper.makeIdGenerator( (SimpleValue) comp.getProperty(property.getName()).getValue(), generatorType, generator, mappings, localGenerators); 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() ); 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 ) { switch ( generatorEnum ) {
case IDENTITY: case IDENTITY:
return "identity"; return "identity";
case AUTO: case AUTO:
return "native"; return useNewGeneratorMappings
? org.hibernate.id.enhanced.SequenceStyleGenerator.class.getName()
: "native";
case TABLE: case TABLE:
return MultipleHiLoPerTableGenerator.class.getName(); return useNewGeneratorMappings
? org.hibernate.id.enhanced.TableGenerator.class.getName()
: MultipleHiLoPerTableGenerator.class.getName();
case SEQUENCE: case SEQUENCE:
return "seqhilo"; return useNewGeneratorMappings
? org.hibernate.id.enhanced.SequenceStyleGenerator.class.getName()
: "seqhilo";
} }
throw new AssertionFailure( "Unknown GeneratorType: " + generatorEnum ); throw new AssertionFailure( "Unknown GeneratorType: " + generatorEnum );
} }
@ -2509,7 +2569,7 @@ public final class
} }
} }
private static HashMap<String, IdGenerator> buildLocalGenerators(XAnnotatedElement annElt, Mappings mappings) { private static HashMap<String, IdGenerator> buildLocalGenerators(XAnnotatedElement annElt, ExtendedMappings mappings) {
HashMap<String, IdGenerator> generators = new HashMap<String, IdGenerator>(); HashMap<String, IdGenerator> generators = new HashMap<String, IdGenerator>();
TableGenerator tabGen = annElt.getAnnotation( TableGenerator.class ); TableGenerator tabGen = annElt.getAnnotation( TableGenerator.class );
SequenceGenerator seqGen = annElt.getAnnotation( SequenceGenerator.class ); SequenceGenerator seqGen = annElt.getAnnotation( SequenceGenerator.class );

View File

@ -108,10 +108,16 @@ public class AnnotationConfiguration extends Configuration {
/** /**
* Setting used to give the name of the default {@link org.hibernate.annotations.CacheConcurrencyStrategy} * Setting used to give the name of the default {@link org.hibernate.annotations.CacheConcurrencyStrategy}
* to use when either {@link javax.persistence.Cacheable @Cacheable} or * 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"; 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. * 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 ); 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) { public IdGenerator getGenerator(String name) {
return getGenerator( name, null ); return getGenerator( name, null );
} }

View File

@ -180,4 +180,12 @@ public interface ExtendedMappings extends Mappings {
public PropertyData getPropertyAnnotatedWithMapsId(XClass entityType, String propertyName); public PropertyData getPropertyAnnotatedWithMapsId(XClass entityType, String propertyName);
public void addPropertyAnnotatedWithMapsId(XClass entityType, PropertyData property); 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();
} }

View File

@ -32,6 +32,7 @@ import org.hibernate.Session;
import org.hibernate.SessionFactory; import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration; import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Environment; import org.hibernate.cfg.Environment;
import org.hibernate.engine.SessionFactoryImplementor;
/** /**
* A base class for all annotation tests. * A base class for all annotation tests.
@ -83,6 +84,10 @@ public abstract class TestCase extends HibernateTestCase {
return sessions; return sessions;
} }
protected SessionFactoryImplementor sfi() {
return (SessionFactoryImplementor) getSessions();
}
@Override @Override
protected void buildConfiguration() throws Exception { protected void buildConfiguration() throws Exception {
if ( getSessions() != null ) { if ( getSessions() != null ) {

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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() ) );
}
}

View File

@ -1,18 +1,26 @@
################################################################################ #
# Copyright (c) 2007, Red Hat Middleware, LLC. All rights reserved. # # Hibernate, Relational Persistence for Idiomatic Java
# # #
# This copyrighted material is made available to anyone wishing to use, modify,# # Copyright (c) 2010, Red Hat Inc. or third-party contributors as
# copy, or redistribute it subject to the terms and conditions of the GNU # # indicated by the @author tags or express copyright attribution
# Lesser General Public License, v. 2.1. This program is distributed in the # # statements applied by the authors. All third-party contributions are
# hope that it will be useful, but WITHOUT A WARRANTY; without even the implied# # distributed under license by Red Hat Inc.
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # #
# Lesser General Public License for more details. You should have received a # # This copyrighted material is made available to anyone wishing to use, modify,
# copy of the GNU Lesser General Public License, v.2.1 along with this # # copy, or redistribute it subject to the terms and conditions of the GNU
# distribution; if not, write to the Free Software Foundation, Inc., # # Lesser General Public License, as published by the Free Software Foundation.
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. # #
# # # This program is distributed in the hope that it will be useful,
# Red Hat Author(s): Steve Ebersole # # 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.dialect ${db.dialect}
hibernate.connection.driver_class ${jdbc.driver} hibernate.connection.driver_class ${jdbc.driver}
hibernate.connection.url ${jdbc.url} hibernate.connection.url ${jdbc.url}

View File

@ -46,6 +46,12 @@ public interface DatabaseStructure {
*/ */
public int getTimesAccessed(); public int getTimesAccessed();
/**
* The configured initial value
* @return The configured initial value
*/
public int getInitialValue();
/** /**
* The configured increment size * The configured increment size
* @return The configured increment size * @return The configured increment size

View File

@ -79,6 +79,13 @@ public class SequenceStructure implements DatabaseStructure {
return accessCounter; return accessCounter;
} }
/**
* {@inheritDoc}
*/
public int getInitialValue() {
return initialValue;
}
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */

View File

@ -84,6 +84,13 @@ public class TableStructure extends TransactionHelper implements DatabaseStructu
return tableName; return tableName;
} }
/**
* {@inheritDoc}
*/
public int getInitialValue() {
return initialValue;
}
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */