HHH-9577 - Make UUID generation the default (AUTO) for UUID type;

HHH-9562 - Dialect specific UUID handling
This commit is contained in:
Steve Ebersole 2015-03-21 14:45:44 -05:00
parent a381e368eb
commit 7a1c155099
6 changed files with 170 additions and 19 deletions

View File

@ -36,6 +36,7 @@ import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.UUID;
import javax.persistence.Basic;
import javax.persistence.Cacheable;
import javax.persistence.CollectionTable;
@ -162,6 +163,7 @@ import org.hibernate.engine.spi.FilterDefinition;
import org.hibernate.id.MultipleHiLoPerTableGenerator;
import org.hibernate.id.PersistentIdentifierGenerator;
import org.hibernate.id.SequenceHiLoGenerator;
import org.hibernate.id.UUIDGenerator;
import org.hibernate.id.enhanced.SequenceStyleGenerator;
import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.internal.util.StringHelper;
@ -2423,15 +2425,16 @@ public final class AnnotationBinder {
|| property.isAnnotationPresent( EmbeddedId.class );
GeneratedValue generatedValue = property.getAnnotation( GeneratedValue.class );
String generatorType = generatedValue != null ?
generatorType( generatedValue.strategy(), buildingContext ) :
"assigned";
String generatorName = generatedValue != null ?
generatedValue.generator() :
BinderHelper.ANNOTATION_STRING_DEFAULT;
String generatorType = generatedValue != null
? generatorType( generatedValue.strategy(), buildingContext, returnedClass )
: "assigned";
String generatorName = generatedValue != null
? generatedValue.generator()
: BinderHelper.ANNOTATION_STRING_DEFAULT;
if ( isComponent ) {
//a component must not have any generator
generatorType = "assigned";
} //a component must not have any generator
}
BinderHelper.makeIdGenerator( idValue, generatorType, generatorName, buildingContext, localGenerators );
if ( LOG.isTraceEnabled() ) {
@ -2742,7 +2745,7 @@ public final class AnnotationBinder {
GeneratedValue generatedValue = property.getAnnotation( GeneratedValue.class );
String generatorType = generatedValue != null
? generatorType( generatedValue.strategy(), buildingContext )
? generatorType( generatedValue.strategy(), buildingContext, property.getType() )
: "assigned";
String generator = generatedValue != null ? generatedValue.generator() : BinderHelper.ANNOTATION_STRING_DEFAULT;
@ -3226,23 +3229,38 @@ public final class AnnotationBinder {
propertyHolder.addProperty( prop, columns, inferredData.getDeclaringClass() );
}
private static String generatorType(GenerationType generatorEnum, MetadataBuildingContext buildingContext) {
private static String generatorType(
GenerationType generatorEnum,
MetadataBuildingContext buildingContext,
XClass javaTypeXClass) {
boolean useNewGeneratorMappings = buildingContext.getBuildingOptions().isUseNewIdentifierGenerators();
switch ( generatorEnum ) {
case IDENTITY:
case IDENTITY: {
return "identity";
case AUTO:
return useNewGeneratorMappings
? org.hibernate.id.enhanced.SequenceStyleGenerator.class.getName()
: "native";
case TABLE:
}
case AUTO: {
final Class javaType = buildingContext.getBuildingOptions()
.getReflectionManager()
.toClass( javaTypeXClass );
if ( UUID.class.isAssignableFrom( javaType ) ) {
return UUIDGenerator.class.getName();
}
else {
return useNewGeneratorMappings
? org.hibernate.id.enhanced.SequenceStyleGenerator.class.getName()
: "native";
}
}
case TABLE: {
return useNewGeneratorMappings
? org.hibernate.id.enhanced.TableGenerator.class.getName()
: MultipleHiLoPerTableGenerator.class.getName();
case SEQUENCE:
}
case SEQUENCE: {
return useNewGeneratorMappings
? org.hibernate.id.enhanced.SequenceStyleGenerator.class.getName()
: "seqhilo";
}
}
throw new AssertionFailure( "Unknown GeneratorType: " + generatorEnum );
}

View File

@ -23,6 +23,10 @@
*/
package org.hibernate.dialect;
import org.hibernate.boot.model.TypeContributions;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.type.PostgresUUIDType;
/**
* An SQL dialect for Postgres 8.2 and later, adds support for "if exists" when dropping tables
*
@ -33,4 +37,12 @@ public class PostgreSQL82Dialect extends PostgreSQL81Dialect {
public boolean supportsIfExistsBeforeTableName() {
return true;
}
@Override
public void contributeTypes(TypeContributions typeContributions, ServiceRegistry serviceRegistry) {
super.contributeTypes( typeContributions, serviceRegistry );
// HHH-9562
typeContributions.contributeType( PostgresUUIDType.INSTANCE );
}
}

View File

@ -79,7 +79,6 @@ public class BasicTypeRegistry implements Serializable {
register( ClassType.INSTANCE );
register( UUIDBinaryType.INSTANCE );
register( UUIDCharType.INSTANCE );
register( PostgresUUIDType.INSTANCE );
register( BinaryType.INSTANCE );
register( WrapperBinaryType.INSTANCE );

View File

@ -57,6 +57,12 @@ public class PostgresUUIDType extends AbstractSingleColumnStandardBasicType<UUID
return "pg-uuid";
}
@Override
protected boolean registerUnderJavaType() {
// register this type under UUID when it is added to the basic type registry
return true;
}
public static class PostgresUUIDSqlTypeDescriptor implements SqlTypeDescriptor {
public static final PostgresUUIDSqlTypeDescriptor INSTANCE = new PostgresUUIDSqlTypeDescriptor();

View File

@ -25,9 +25,8 @@ package org.hibernate.id.uuid;
import java.util.UUID;
import org.junit.Test;
import org.hibernate.testing.junit4.BaseUnitTestCase;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;

View File

@ -0,0 +1,117 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2015, 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.id.uuid;
import java.util.UUID;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.internal.MetadataImpl;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.id.IdentifierGenerator;
import org.hibernate.id.UUIDGenerator;
import org.hibernate.mapping.PersistentClass;
import org.hibernate.mapping.RootClass;
import org.hibernate.testing.junit4.BaseUnitTestCase;
import org.junit.Test;
import static org.hibernate.testing.junit4.ExtraAssertions.assertTyping;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
/**
* Tests a UUID attribute annotated as a generated id value.
*
* @author Steve Ebersole
*/
public class GeneratedValueTest extends BaseUnitTestCase {
@Test
public void testGeneratedUuidId() {
StandardServiceRegistry ssr = new StandardServiceRegistryBuilder()
.applySetting( AvailableSettings.HBM2DDL_AUTO, "create-drop" )
.build();
try {
Metadata metadata = new MetadataSources( ssr ).addAnnotatedClass( TheEntity.class ).buildMetadata();
( (MetadataImpl) metadata ).validate();
PersistentClass entityBinding = metadata.getEntityBinding( TheEntity.class.getName() );
assertEquals( UUID.class, entityBinding.getIdentifier().getType().getReturnedClass() );
IdentifierGenerator generator = entityBinding.getIdentifier().createIdentifierGenerator(
metadata.getIdentifierGeneratorFactory(),
metadata.getDatabase().getDialect(),
null,
null,
(RootClass) entityBinding
);
assertTyping( UUIDGenerator.class, generator );
// now a functional test
SessionFactory sf = metadata.buildSessionFactory();
try {
TheEntity theEntity = new TheEntity();
Session s = sf.openSession();
s.beginTransaction();
s.save( theEntity );
s.getTransaction().commit();
s.close();
assertNotNull( theEntity.id );
s = sf.openSession();
s.beginTransaction();
s.delete( theEntity );
s.getTransaction().commit();
s.close();
}
finally {
try {
sf.close();
}
catch (Exception ignore) {
}
}
}
finally {
StandardServiceRegistryBuilder.destroy( ssr );
}
}
@Entity(name = "TheEntity")
@Table(name = "TheEntity")
public static class TheEntity {
@Id
@GeneratedValue
public UUID id;
}
}