HHH-6520 if no @GeneratedValue on an simple @Id property, then the generator type should be assigned

This commit is contained in:
Strong Liu 2011-08-01 16:38:13 +08:00
parent 5a00cb9276
commit 3689e533fb
4 changed files with 33 additions and 15 deletions

View File

@ -27,6 +27,8 @@ import java.io.Serializable;
import java.util.Collections; import java.util.Collections;
import java.util.Map; import java.util.Map;
import org.hibernate.internal.util.collections.CollectionHelper;
/** /**
* Identifier generator container, Useful to keep named generator in annotations * Identifier generator container, Useful to keep named generator in annotations
* *
@ -42,7 +44,12 @@ public class IdGenerator implements Serializable {
Map<String, String> parameters ) { Map<String, String> parameters ) {
this.name = name; this.name = name;
this.strategy = strategy; this.strategy = strategy;
this.parameters = parameters; if ( CollectionHelper.isEmpty( parameters ) ) {
this.parameters = Collections.emptyMap();
}
else {
this.parameters = Collections.unmodifiableMap( parameters );
}
} }
/** /**
@ -63,13 +70,6 @@ public class IdGenerator implements Serializable {
* @return generator configuration parameters * @return generator configuration parameters
*/ */
public Map<String, String> getParameters() { public Map<String, String> getParameters() {
Map<String, String> returnedParameters; return parameters;
if ( parameters == null ) {
returnedParameters = Collections.emptyMap();
}
else {
returnedParameters = Collections.unmodifiableMap(parameters);
}
return returnedParameters;
} }
} }

View File

@ -29,12 +29,14 @@ import java.lang.reflect.Member;
import java.lang.reflect.ParameterizedType; import java.lang.reflect.ParameterizedType;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import org.hibernate.AssertionFailure; import org.hibernate.AssertionFailure;
import org.hibernate.EntityMode; import org.hibernate.EntityMode;
import org.hibernate.cfg.NotYetImplementedException; import org.hibernate.cfg.NotYetImplementedException;
import org.hibernate.id.IdentifierGenerator;
import org.hibernate.internal.util.StringHelper; import org.hibernate.internal.util.StringHelper;
import org.hibernate.internal.util.beans.BeanInfoHelper; import org.hibernate.internal.util.beans.BeanInfoHelper;
import org.hibernate.metamodel.binding.AbstractCollectionElement; import org.hibernate.metamodel.binding.AbstractCollectionElement;
@ -48,6 +50,7 @@ import org.hibernate.metamodel.binding.ComponentAttributeBinding;
import org.hibernate.metamodel.binding.EntityBinding; import org.hibernate.metamodel.binding.EntityBinding;
import org.hibernate.metamodel.binding.EntityDiscriminator; import org.hibernate.metamodel.binding.EntityDiscriminator;
import org.hibernate.metamodel.binding.HibernateTypeDescriptor; import org.hibernate.metamodel.binding.HibernateTypeDescriptor;
import org.hibernate.metamodel.binding.IdGenerator;
import org.hibernate.metamodel.binding.InheritanceType; import org.hibernate.metamodel.binding.InheritanceType;
import org.hibernate.metamodel.binding.ManyToOneAttributeBinding; import org.hibernate.metamodel.binding.ManyToOneAttributeBinding;
import org.hibernate.metamodel.binding.MetaAttribute; import org.hibernate.metamodel.binding.MetaAttribute;
@ -328,9 +331,15 @@ public class Binder {
); );
entityBinding.getHierarchyDetails().getEntityIdentifier().setValueBinding( idAttributeBinding ); entityBinding.getHierarchyDetails().getEntityIdentifier().setValueBinding( idAttributeBinding );
IdGenerator generator = identifierSource.getIdentifierGeneratorDescriptor();
if(generator == null){
Map<String,String> params = new HashMap<String, String>( );
params.put( IdentifierGenerator.ENTITY_NAME, entityBinding.getEntity().getName() );
generator = new IdGenerator( "default_assign_identity_generator", "assigned", params );
}
entityBinding.getHierarchyDetails() entityBinding.getHierarchyDetails()
.getEntityIdentifier() .getEntityIdentifier()
.setIdGenerator( identifierSource.getIdentifierGeneratorDescriptor() ); .setIdGenerator( generator );
final org.hibernate.metamodel.relational.Value relationalValue = idAttributeBinding.getValue(); final org.hibernate.metamodel.relational.Value relationalValue = idAttributeBinding.getValue();

View File

@ -32,6 +32,7 @@ import javax.persistence.Id;
import org.junit.Test; import org.junit.Test;
import org.hibernate.annotations.GenericGenerator; import org.hibernate.annotations.GenericGenerator;
import org.hibernate.id.Assigned;
import org.hibernate.id.IdentifierGenerator; import org.hibernate.id.IdentifierGenerator;
import org.hibernate.id.IdentityGenerator; import org.hibernate.id.IdentityGenerator;
import org.hibernate.id.MultipleHiLoPerTableGenerator; import org.hibernate.id.MultipleHiLoPerTableGenerator;
@ -39,10 +40,14 @@ import org.hibernate.id.SequenceHiLoGenerator;
import org.hibernate.id.UUIDHexGenerator; import org.hibernate.id.UUIDHexGenerator;
import org.hibernate.metamodel.MetadataSources; import org.hibernate.metamodel.MetadataSources;
import org.hibernate.metamodel.binding.EntityBinding; import org.hibernate.metamodel.binding.EntityBinding;
import org.hibernate.metamodel.binding.EntityIdentifier;
import org.hibernate.metamodel.source.MappingException; import org.hibernate.metamodel.source.MappingException;
import org.hibernate.metamodel.source.internal.IdentifierGeneratorResolver;
import org.hibernate.service.ServiceRegistryBuilder; import org.hibernate.service.ServiceRegistryBuilder;
import static junit.framework.Assert.assertEquals; import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertNotNull;
import static junit.framework.Assert.assertNull; import static junit.framework.Assert.assertNull;
import static junit.framework.Assert.assertTrue; import static junit.framework.Assert.assertTrue;
import static junit.framework.Assert.fail; import static junit.framework.Assert.fail;
@ -61,8 +66,12 @@ public class IdentifierGeneratorTest extends BaseAnnotationBindingTestCase {
@Resources(annotatedClasses = NoGenerationEntity.class) @Resources(annotatedClasses = NoGenerationEntity.class)
public void testNoIdGeneration() { public void testNoIdGeneration() {
EntityBinding binding = getEntityBinding( NoGenerationEntity.class ); EntityBinding binding = getEntityBinding( NoGenerationEntity.class );
IdentifierGenerator generator = binding.getHierarchyDetails().getEntityIdentifier().getIdentifierGenerator(); EntityIdentifier identifier = binding.getHierarchyDetails().getEntityIdentifier();
assertNull( generator ); IdentifierGenerator generator =identifier.getIdentifierGenerator();
assertNotNull( generator );
assertEquals( "Wrong generator", Assigned.class, generator.getClass() );
assertFalse( identifier.isEmbedded() );
} }
@Entity @Entity

View File

@ -38,9 +38,9 @@ public abstract class AbstractOperationTestCase extends BaseCoreFunctionalTestCa
cfg.setProperty( Environment.STATEMENT_BATCH_SIZE, "0" ); cfg.setProperty( Environment.STATEMENT_BATCH_SIZE, "0" );
} }
// public String[] getMappings() { public String[] getMappings() {
// return new String[] { "ops/Node.hbm.xml", "ops/Employer.hbm.xml", "ops/OptLockEntity.hbm.xml", "ops/OneToOne.hbm.xml", "ops/Competition.hbm.xml" }; return new String[] { "ops/Node.hbm.xml", "ops/Employer.hbm.xml", "ops/OptLockEntity.hbm.xml", "ops/OneToOne.hbm.xml", "ops/Competition.hbm.xml" };
// } }
public String getCacheConcurrencyStrategy() { public String getCacheConcurrencyStrategy() {
return null; return null;