HHH-6261 Bind @GeneratedValue

This commit is contained in:
Strong Liu 2011-05-31 14:48:44 +08:00 committed by Hardy Ferentschik
parent 3ff1b440c2
commit 19f629b461
5 changed files with 73 additions and 24 deletions

View File

@ -115,7 +115,7 @@ public class MultipleHiLoPerTableGenerator implements PersistentIdentifierGenera
public String[] sqlCreateStrings(Dialect dialect) throws HibernateException { public String[] sqlCreateStrings(Dialect dialect) throws HibernateException {
return new String[] { return new String[] {
new StringBuffer( dialect.getCreateTableString() ) new StringBuilder( dialect.getCreateTableString() )
.append( ' ' ) .append( ' ' )
.append( tableName ) .append( tableName )
.append( " ( " ) .append( " ( " )

View File

@ -27,8 +27,11 @@ import java.io.Serializable;
import java.util.Properties; import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import javax.persistence.GenerationType;
import org.jboss.logging.Logger; import org.jboss.logging.Logger;
import org.hibernate.AssertionFailure;
import org.hibernate.MappingException; import org.hibernate.MappingException;
import org.hibernate.dialect.Dialect; import org.hibernate.dialect.Dialect;
import org.hibernate.id.Assigned; import org.hibernate.id.Assigned;
@ -38,6 +41,7 @@ import org.hibernate.id.GUIDGenerator;
import org.hibernate.id.IdentifierGenerator; import org.hibernate.id.IdentifierGenerator;
import org.hibernate.id.IdentityGenerator; import org.hibernate.id.IdentityGenerator;
import org.hibernate.id.IncrementGenerator; import org.hibernate.id.IncrementGenerator;
import org.hibernate.id.MultipleHiLoPerTableGenerator;
import org.hibernate.id.SelectGenerator; import org.hibernate.id.SelectGenerator;
import org.hibernate.id.SequenceGenerator; import org.hibernate.id.SequenceGenerator;
import org.hibernate.id.SequenceHiLoGenerator; import org.hibernate.id.SequenceHiLoGenerator;
@ -93,6 +97,27 @@ public class DefaultIdentifierGeneratorFactory implements IdentifierGeneratorFac
} }
} }
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 );
}
@Override @Override
public void setDialect(Dialect dialect) { public void setDialect(Dialect dialect) {
LOG.debugf( "Setting dialect [%s]", dialect ); LOG.debugf( "Setting dialect [%s]", dialect );

View File

@ -24,15 +24,14 @@
package org.hibernate.metamodel.binding; package org.hibernate.metamodel.binding;
import java.util.Properties; import java.util.Properties;
import javax.persistence.GenerationType; import javax.persistence.GenerationType;
import org.jboss.logging.Logger;
import org.hibernate.id.IdentifierGenerator; import org.hibernate.id.IdentifierGenerator;
import org.hibernate.id.factory.IdentifierGeneratorFactory; import org.hibernate.id.factory.IdentifierGeneratorFactory;
import org.hibernate.internal.CoreMessageLogger; import org.hibernate.internal.CoreMessageLogger;
import org.jboss.logging.Logger;
/** /**
* Binds the entity identifier. * Binds the entity identifier.
* *
@ -40,14 +39,16 @@ import org.jboss.logging.Logger;
*/ */
public class EntityIdentifier { public class EntityIdentifier {
private static final CoreMessageLogger LOG = Logger.getMessageLogger(CoreMessageLogger.class, EntityIdentifier.class.getName()); private static final CoreMessageLogger LOG = Logger.getMessageLogger(
CoreMessageLogger.class,
EntityIdentifier.class.getName()
);
private final EntityBinding entityBinding; private final EntityBinding entityBinding;
private AttributeBinding attributeBinding; private AttributeBinding attributeBinding;
private IdentifierGenerator identifierGenerator; private IdentifierGenerator identifierGenerator;
private IdGenerator idGenerator; private IdGenerator idGenerator;
private GenerationType generationType; // todo : mappers, etc
// todo : generator, mappers, etc
/** /**
* Create an identifier * Create an identifier
@ -75,14 +76,18 @@ public class EntityIdentifier {
this.idGenerator = idGenerator; this.idGenerator = idGenerator;
} }
public void setGenerationType(GenerationType generationType) {
this.generationType = generationType;
}
public IdentifierGenerator createIdentifierGenerator(IdentifierGeneratorFactory factory) { public IdentifierGenerator createIdentifierGenerator(IdentifierGeneratorFactory factory) {
Properties props = new Properties(); if ( identifierGenerator == null ) {
props.putAll( idGenerator.getParameters() ); Properties props = new Properties();
identifierGenerator = factory.createIdentifierGenerator( idGenerator.getStrategy(), null, props ); if ( idGenerator != null ) {
props.putAll( idGenerator.getParameters() );
}
identifierGenerator = factory.createIdentifierGenerator(
idGenerator.getStrategy(),
getValueBinding().getHibernateTypeDescriptor().getExplicitType(),
props
);
}
return identifierGenerator; return identifierGenerator;
} }

View File

@ -23,6 +23,7 @@
*/ */
package org.hibernate.metamodel.source.annotations.entity; package org.hibernate.metamodel.source.annotations.entity;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -40,6 +41,7 @@ import org.hibernate.annotations.OptimisticLockType;
import org.hibernate.annotations.PolymorphismType; import org.hibernate.annotations.PolymorphismType;
import org.hibernate.cache.spi.RegionFactory; import org.hibernate.cache.spi.RegionFactory;
import org.hibernate.cache.spi.access.AccessType; import org.hibernate.cache.spi.access.AccessType;
import org.hibernate.id.factory.DefaultIdentifierGeneratorFactory;
import org.hibernate.internal.util.StringHelper; import org.hibernate.internal.util.StringHelper;
import org.hibernate.metamodel.binding.Caching; import org.hibernate.metamodel.binding.Caching;
import org.hibernate.metamodel.binding.EntityBinding; import org.hibernate.metamodel.binding.EntityBinding;
@ -348,7 +350,7 @@ public class EntityBinder {
SimpleAttributeBinding attributeBinding = entityBinding.makeSimpleIdAttributeBinding( idName ); SimpleAttributeBinding attributeBinding = entityBinding.makeSimpleIdAttributeBinding( idName );
attributeBinding.initialize( new AttributeBindingStateImpl( (SimpleAttribute) idAttribute ) ); attributeBinding.initialize( new AttributeBindingStateImpl( (SimpleAttribute) idAttribute ) );
attributeBinding.initialize( new ColumnRelationalStateImpl( (SimpleAttribute) idAttribute, meta ) ); attributeBinding.initialize( new ColumnRelationalStateImpl( (SimpleAttribute) idAttribute, meta ) );
bindSingleIdGeneratedValue( entityBinding, idName );
} }
@ -372,8 +374,9 @@ public class EntityBinder {
); );
} }
String generator = JandexHelper.getValueAsString( generatedValueAnn, "generator" ); String generator = JandexHelper.getValueAsString( generatedValueAnn, "generator" );
IdGenerator idGenerator = null;
if ( StringHelper.isNotEmpty( generator ) ) { if ( StringHelper.isNotEmpty( generator ) ) {
IdGenerator idGenerator = ( (MetadataImpl) meta ).getIdGenerator( generator ); idGenerator = ( (MetadataImpl) meta ).getIdGenerator( generator );
if ( idGenerator == null ) { if ( idGenerator == null ) {
throw new MappingException( throw new MappingException(
String.format( String.format(
@ -386,10 +389,24 @@ public class EntityBinder {
} }
entityBinding.getEntityIdentifier().setIdGenerator( idGenerator ); entityBinding.getEntityIdentifier().setIdGenerator( idGenerator );
} }
GenerationType strategy = JandexHelper.getValueAsEnum( generatedValueAnn, "strategy", GenerationType.class ); GenerationType generationType = JandexHelper.getValueAsEnum( generatedValueAnn, "strategy", GenerationType.class );
entityBinding.getEntityIdentifier().setGenerationType( strategy ); String strategy = DefaultIdentifierGeneratorFactory.generatorType( generationType, meta.getOptions().useNewIdentifierGenerators() );
if ( idGenerator != null && !strategy.equals( idGenerator.getStrategy() ) ) {
//todo how to ?
throw new MappingException(
String.format(
"Inconsistent Id Generation strategy of @GeneratedValue on %s.%s",
configuredClass.getName(),
idName
)
);
} else{
idGenerator = new IdGenerator( "NAME", strategy, new HashMap<String, String>( ) );
entityBinding.getEntityIdentifier().setIdGenerator( idGenerator );
}
} }
private void bindAttributes(EntityBinding entityBinding) { private void bindAttributes(EntityBinding entityBinding) {
for ( MappedAttribute mappedAttribute : configuredClass.getMappedAttributes() ) { for ( MappedAttribute mappedAttribute : configuredClass.getMappedAttributes() ) {
if ( mappedAttribute instanceof AssociationAttribute ) { if ( mappedAttribute instanceof AssociationAttribute ) {

View File

@ -25,6 +25,7 @@ package org.hibernate.metamodel.source.annotations.global;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import javax.persistence.GenerationType;
import javax.persistence.SequenceGenerator; import javax.persistence.SequenceGenerator;
import org.jboss.jandex.AnnotationInstance; import org.jboss.jandex.AnnotationInstance;
@ -40,6 +41,7 @@ import org.hibernate.id.SequenceHiLoGenerator;
import org.hibernate.id.TableHiLoGenerator; import org.hibernate.id.TableHiLoGenerator;
import org.hibernate.id.enhanced.SequenceStyleGenerator; import org.hibernate.id.enhanced.SequenceStyleGenerator;
import org.hibernate.id.enhanced.TableGenerator; import org.hibernate.id.enhanced.TableGenerator;
import org.hibernate.id.factory.DefaultIdentifierGeneratorFactory;
import org.hibernate.internal.CoreMessageLogger; import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.internal.util.StringHelper; import org.hibernate.internal.util.StringHelper;
import org.hibernate.metamodel.binding.IdGenerator; import org.hibernate.metamodel.binding.IdGenerator;
@ -113,8 +115,9 @@ public class IdGeneratorBinder {
String strategy; String strategy;
Map<String, String> prms = new HashMap<String, String>(); Map<String, String> prms = new HashMap<String, String>();
addStringParameter( generator, "sequenceName", prms, SequenceStyleGenerator.SEQUENCE_PARAM ); addStringParameter( generator, "sequenceName", prms, SequenceStyleGenerator.SEQUENCE_PARAM );
if ( metadata.getOptions().useNewIdentifierGenerators() ) { boolean useNewIdentifierGenerators = metadata.getOptions().useNewIdentifierGenerators();
strategy = SequenceStyleGenerator.class.getName(); strategy = DefaultIdentifierGeneratorFactory.generatorType( GenerationType.SEQUENCE, useNewIdentifierGenerators);
if ( useNewIdentifierGenerators ) {
addStringParameter( generator, "catalog", prms, PersistentIdentifierGenerator.CATALOG ); addStringParameter( generator, "catalog", prms, PersistentIdentifierGenerator.CATALOG );
addStringParameter( generator, "schema", prms, PersistentIdentifierGenerator.SCHEMA ); addStringParameter( generator, "schema", prms, PersistentIdentifierGenerator.SCHEMA );
prms.put( prms.put(
@ -127,7 +130,6 @@ public class IdGeneratorBinder {
); );
} }
else { else {
strategy = "seqhilo";
if ( JandexHelper.getValueAsInt( generator, "initialValue" ) != 1 ) { if ( JandexHelper.getValueAsInt( generator, "initialValue" ) != 1 ) {
LOG.unsupportedInitialValue( AvailableSettings.USE_NEW_ID_GENERATOR_MAPPINGS ); LOG.unsupportedInitialValue( AvailableSettings.USE_NEW_ID_GENERATOR_MAPPINGS );
} }
@ -147,8 +149,9 @@ public class IdGeneratorBinder {
Map<String, String> prms = new HashMap<String, String>(); Map<String, String> prms = new HashMap<String, String>();
addStringParameter( generator, "catalog", prms, PersistentIdentifierGenerator.CATALOG ); addStringParameter( generator, "catalog", prms, PersistentIdentifierGenerator.CATALOG );
addStringParameter( generator, "schema", prms, PersistentIdentifierGenerator.SCHEMA ); addStringParameter( generator, "schema", prms, PersistentIdentifierGenerator.SCHEMA );
if ( metadata.getOptions().useNewIdentifierGenerators() ) { boolean useNewIdentifierGenerators = metadata.getOptions().useNewIdentifierGenerators();
strategy = TableGenerator.class.getName(); strategy = DefaultIdentifierGeneratorFactory.generatorType( GenerationType.TABLE, useNewIdentifierGenerators);
if ( useNewIdentifierGenerators ) {
prms.put( TableGenerator.CONFIG_PREFER_SEGMENT_PER_ENTITY, "true" ); prms.put( TableGenerator.CONFIG_PREFER_SEGMENT_PER_ENTITY, "true" );
addStringParameter( generator, "table", prms, TableGenerator.TABLE_PARAM ); addStringParameter( generator, "table", prms, TableGenerator.TABLE_PARAM );
addStringParameter( generator, "pkColumnName", prms, TableGenerator.SEGMENT_COLUMN_PARAM ); addStringParameter( generator, "pkColumnName", prms, TableGenerator.SEGMENT_COLUMN_PARAM );
@ -164,7 +167,6 @@ public class IdGeneratorBinder {
); );
} }
else { else {
strategy = MultipleHiLoPerTableGenerator.class.getName();
addStringParameter( generator, "table", prms, MultipleHiLoPerTableGenerator.ID_TABLE ); addStringParameter( generator, "table", prms, MultipleHiLoPerTableGenerator.ID_TABLE );
addStringParameter( generator, "pkColumnName", prms, MultipleHiLoPerTableGenerator.PK_COLUMN_NAME ); addStringParameter( generator, "pkColumnName", prms, MultipleHiLoPerTableGenerator.PK_COLUMN_NAME );
addStringParameter( generator, "pkColumnValue", prms, MultipleHiLoPerTableGenerator.PK_VALUE_NAME ); addStringParameter( generator, "pkColumnValue", prms, MultipleHiLoPerTableGenerator.PK_VALUE_NAME );