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 {
return new String[] {
new StringBuffer( dialect.getCreateTableString() )
new StringBuilder( dialect.getCreateTableString() )
.append( ' ' )
.append( tableName )
.append( " ( " )

View File

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

View File

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

View File

@ -23,6 +23,7 @@
*/
package org.hibernate.metamodel.source.annotations.entity;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -40,6 +41,7 @@ import org.hibernate.annotations.OptimisticLockType;
import org.hibernate.annotations.PolymorphismType;
import org.hibernate.cache.spi.RegionFactory;
import org.hibernate.cache.spi.access.AccessType;
import org.hibernate.id.factory.DefaultIdentifierGeneratorFactory;
import org.hibernate.internal.util.StringHelper;
import org.hibernate.metamodel.binding.Caching;
import org.hibernate.metamodel.binding.EntityBinding;
@ -348,7 +350,7 @@ public class EntityBinder {
SimpleAttributeBinding attributeBinding = entityBinding.makeSimpleIdAttributeBinding( idName );
attributeBinding.initialize( new AttributeBindingStateImpl( (SimpleAttribute) idAttribute ) );
attributeBinding.initialize( new ColumnRelationalStateImpl( (SimpleAttribute) idAttribute, meta ) );
bindSingleIdGeneratedValue( entityBinding, idName );
}
@ -372,8 +374,9 @@ public class EntityBinder {
);
}
String generator = JandexHelper.getValueAsString( generatedValueAnn, "generator" );
IdGenerator idGenerator = null;
if ( StringHelper.isNotEmpty( generator ) ) {
IdGenerator idGenerator = ( (MetadataImpl) meta ).getIdGenerator( generator );
idGenerator = ( (MetadataImpl) meta ).getIdGenerator( generator );
if ( idGenerator == null ) {
throw new MappingException(
String.format(
@ -386,10 +389,24 @@ public class EntityBinder {
}
entityBinding.getEntityIdentifier().setIdGenerator( idGenerator );
}
GenerationType strategy = JandexHelper.getValueAsEnum( generatedValueAnn, "strategy", GenerationType.class );
entityBinding.getEntityIdentifier().setGenerationType( strategy );
GenerationType generationType = JandexHelper.getValueAsEnum( generatedValueAnn, "strategy", GenerationType.class );
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) {
for ( MappedAttribute mappedAttribute : configuredClass.getMappedAttributes() ) {
if ( mappedAttribute instanceof AssociationAttribute ) {

View File

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