HHH-7183 : Bind HibernateTypeDescriptor fields once

This commit is contained in:
Gail Badner 2012-03-16 17:23:21 -07:00
parent 0b3baf5812
commit f42efc1598
3 changed files with 74 additions and 81 deletions

View File

@ -55,11 +55,13 @@ import org.hibernate.metamodel.spi.binding.EntityDiscriminator;
import org.hibernate.metamodel.spi.binding.EntityVersion;
import org.hibernate.metamodel.spi.binding.HibernateTypeDescriptor;
import org.hibernate.metamodel.spi.binding.IdGenerator;
import org.hibernate.metamodel.spi.binding.IndexedPluralAttributeBinding;
import org.hibernate.metamodel.spi.binding.InheritanceType;
import org.hibernate.metamodel.spi.binding.ManyToOneAttributeBinding;
import org.hibernate.metamodel.spi.binding.MetaAttribute;
import org.hibernate.metamodel.spi.binding.PluralAttributeBinding;
import org.hibernate.metamodel.spi.binding.PluralAttributeElementNature;
import org.hibernate.metamodel.spi.binding.PluralAttributeIndexBinding;
import org.hibernate.metamodel.spi.binding.PluralAttributeKeyBinding;
import org.hibernate.metamodel.spi.binding.RelationalValueBinding;
import org.hibernate.metamodel.spi.binding.SecondaryTable;
@ -272,8 +274,9 @@ public class Binder {
attributeSource.getGeneration() );
final HibernateTypeDescriptor hibernateTypeDescriptor = attributeBinding.getHibernateTypeDescriptor();
bindHibernateTypeDescriptor(
attributeBinding,
attributeSource.getTypeInformation()
attributeBinding.getHibernateTypeDescriptor(),
attributeSource.getTypeInformation(),
createSingularAttributeJavaType( attributeBinding.getAttribute() )
);
Type resolvedType = getHeuristicType( hibernateTypeDescriptor );
resolveHibernateResolvedType( attributeBinding.getHibernateTypeDescriptor(), resolvedType );
@ -342,11 +345,12 @@ public class Binder {
private void bindCollectionIndex(
final AbstractPluralAttributeBinding attributeBinding,
final PluralAttributeSource attributeSource,
final String defaultElementJavaTypeNam) {
if ( attributeSource.getPluralAttributeNature() != PluralAttributeNature.LIST &&
attributeSource.getPluralAttributeNature() != PluralAttributeNature.MAP ) {
final String defaultElementJavaTypeName) {
if ( ! IndexedPluralAttributeBinding.class.isInstance( attributeBinding ) ) {
return;
}
final PluralAttributeIndexBinding indexBinding
= ( (IndexedPluralAttributeBinding) attributeBinding ).getPluralAttributeIndexBinding();
// todo : implement
throw new NotYetImplementedException();
}
@ -516,13 +520,6 @@ public class Binder {
return;
}
if ( attributeBinding.getPluralAttributeElementBinding().getPluralAttributeElementNature() == PluralAttributeElementNature.BASIC ) {
bindHibernateTypeDescriptor(
attributeBinding.getPluralAttributeElementBinding().getHibernateTypeDescriptor(),
( (BasicPluralAttributeElementSource) attributeSource.getElementSource() ).getExplicitHibernateTypeSource(),
reflectedCollectionJavaTypes != null && reflectedCollectionJavaTypes.getCollectionElementType() != null ?
reflectedCollectionJavaTypes.getCollectionElementType().getName() :
null
);
if ( attributeSource.getPluralAttributeNature() == PluralAttributeNature.SET ) {
bindBasicElementSetTablePrimaryKey( ( SetBinding ) attributeBinding );
} else {
@ -670,21 +667,50 @@ public class Binder {
}
private void bindHibernateTypeDescriptor(
final SingularAttributeBinding singularAttributeBinding,
final ExplicitHibernateTypeSource typeSource) {
final HibernateTypeDescriptor hibernateTypeDescriptor,
final ExplicitHibernateTypeSource explicitTypeSource,
final org.hibernate.internal.util.Value<Class<?>> defaultJavaType) {
// if there is an explicit type name specified, then there's no reason to
// initialize the default Java type name; simply pass a null default instead.
bindHibernateTypeDescriptor(
singularAttributeBinding.getHibernateTypeDescriptor(),
typeSource,
determineJavaType( singularAttributeBinding.getAttribute() ).getName()
hibernateTypeDescriptor,
explicitTypeSource,
explicitTypeSource.getName() == null ? defaultJavaType.getValue().getName() : null
);
}
private void bindExplicitHibernateTypeDescriptor(
private void bindHibernateTypeDescriptor(
final HibernateTypeDescriptor hibernateTypeDescriptor,
final String typeName,
final Map<String, String> typeParameters) {
final ExplicitHibernateTypeSource explicitTypeSource,
final String defaultJavaTypeName) {
if ( explicitTypeSource.getName() == null ) {
if ( hibernateTypeDescriptor.getJavaTypeName() != null ) {
throw new MappingException(
String.format(
"Attempt to re-initialize (non-explicit) Java type name; current=%s new=%s",
hibernateTypeDescriptor.getJavaTypeName(),
defaultJavaTypeName
),
bindingContexts.peek().getOrigin()
);
}
hibernateTypeDescriptor.setJavaTypeName( defaultJavaTypeName );
}
else {
final String typeName = explicitTypeSource.getName();
final Map<String,String> typeParameters = explicitTypeSource.getParameters();
// Check if user-specified name is of a User-Defined Type (UDT)
final TypeDefinition typeDef = metadata.getTypeDefinition( typeName );
if ( hibernateTypeDescriptor.getExplicitTypeName() != null ) {
throw new MappingException(
String.format(
"Attempt to re-initialize explicity-mapped Java type name; current=%s new=%s",
hibernateTypeDescriptor.getExplicitTypeName(),
typeName
),
bindingContexts.peek().getOrigin()
);
}
if ( typeDef == null ) {
hibernateTypeDescriptor.setExplicitTypeName( typeName );
} else {
@ -695,28 +721,20 @@ public class Binder {
hibernateTypeDescriptor.getTypeParameters().putAll( typeParameters );
}
}
}
private Class<?> determineJavaType(Attribute attribute) {
private static org.hibernate.internal.util.Value<Class<?>> createSingularAttributeJavaType(
final SingularAttribute attribute) {
org.hibernate.internal.util.Value.DeferredInitializer<Class<?>> deferredInitializer =
new org.hibernate.internal.util.Value.DeferredInitializer<Class<?>>() {
public Class<?> initialize() {
return ReflectHelper.reflectedPropertyClass(
attribute.getAttributeContainer().getClassReference(),
attribute.getName()
);
}
private void bindHibernateTypeDescriptor(
final HibernateTypeDescriptor hibernateTypeDescriptor,
final ExplicitHibernateTypeSource explicitTypeSource,
final String defaultJavaTypeName) {
if ( explicitTypeSource.getName() == null ) {
if ( hibernateTypeDescriptor.getJavaTypeName() == null ) {
hibernateTypeDescriptor.setJavaTypeName( defaultJavaTypeName );
}
}
else {
bindExplicitHibernateTypeDescriptor(
hibernateTypeDescriptor, explicitTypeSource.getName(), explicitTypeSource.getParameters()
);
}
};
return new org.hibernate.internal.util.Value<Class<?>>( deferredInitializer );
}
private void bindIdentifier( final EntityBinding rootEntityBinding, final IdentifierSource identifierSource ) {
@ -752,9 +770,11 @@ public class Binder {
attributeSource,
attribute,
attributeBindingContainer.seekEntityBinding().getPrimaryTable() );
org.hibernate.internal.util.Value<Class<?>> referencedJavaTypeValue =
createSingularAttributeJavaType( attribute );
final String referencedEntityName = attributeSource.getReferencedEntityName() != null
? attributeSource.getReferencedEntityName()
: determineJavaType( attribute ).getName();
: referencedJavaTypeValue.getValue().getName();
final EntityBinding referencedEntityBinding = entityBinding( referencedEntityName );
final AttributeBinding referencedAttributeBinding =
attributeSource.getReferencedEntityAttributeName() == null
@ -786,8 +806,9 @@ public class Binder {
);
// TODO: need to be able to deal with composite IDs
bindHibernateTypeDescriptor(
attributeBinding,
attributeSource.getTypeInformation()
attributeBinding.getHibernateTypeDescriptor(),
attributeSource.getTypeInformation(),
referencedJavaTypeValue
);
resolveHibernateResolvedType( attributeBinding.getHibernateTypeDescriptor(), resolvedType );
resolveJdbcDataType( resolvedType, (AbstractValue) relationalValueBindings.get( 0 ).getValue() );

View File

@ -123,35 +123,7 @@ public class HibernateTypeHelper {
public ReflectedCollectionJavaTypes getReflectedCollectionJavaTypes(
PluralAttributeBinding attributeBinding) {
final ReflectedCollectionJavaTypes reflectedCollectionJavaTypes = determineJavaType( attributeBinding.getAttribute() );
if ( reflectedCollectionJavaTypes != null ) {
if ( reflectedCollectionJavaTypes.collectionType != null ) {
if ( attributeBinding.getHibernateTypeDescriptor().getJavaTypeName() == null ) {
attributeBinding.getHibernateTypeDescriptor().setJavaTypeName(
reflectedCollectionJavaTypes.collectionType.getName()
);
}
}
if ( reflectedCollectionJavaTypes.collectionElementType != null ) {
if ( attributeBinding.getPluralAttributeElementBinding().getHibernateTypeDescriptor().getJavaTypeName() == null ) {
attributeBinding.getPluralAttributeElementBinding().getHibernateTypeDescriptor().setJavaTypeName(
reflectedCollectionJavaTypes.collectionElementType.getName()
);
}
}
if ( reflectedCollectionJavaTypes.collectionIndexType != null
&& IndexedPluralAttributeBinding.class.isInstance( attributeBinding ) ) {
final PluralAttributeIndexBinding indexBinding
= ( (IndexedPluralAttributeBinding) attributeBinding ).getPluralAttributeIndexBinding();
if ( indexBinding.getHibernateTypeDescriptor().getJavaTypeName() == null ) {
indexBinding.getHibernateTypeDescriptor().setJavaTypeName(
reflectedCollectionJavaTypes.collectionIndexType.getName()
);
}
}
}
return reflectedCollectionJavaTypes;
return determineJavaType( attributeBinding.getAttribute() );
}
private Class<?> determineJavaType(final SingularAttribute attribute) {

View File

@ -22,7 +22,7 @@
<set name="thePropertyRefSet">
<key column="pid" property-ref="name"/>
<element column="property_ref_set_stuff" type="Integer"/>
<element column="property_ref_set_stuff" type="integer"/>
</set>
</class>