HHH-7963 DynamicParameterizedType is not binded

This commit is contained in:
Strong Liu 2013-01-22 00:23:12 +08:00
parent 68ebf7e2bc
commit d2243f4e14
10 changed files with 76 additions and 45 deletions

View File

@ -33,6 +33,8 @@ import java.util.Properties;
import org.hibernate.AssertionFailure;
import org.hibernate.EntityMode;
import org.hibernate.boot.registry.classloading.spi.ClassLoaderService;
import org.hibernate.boot.registry.classloading.spi.ClassLoadingException;
import org.hibernate.cfg.NotYetImplementedException;
import org.hibernate.internal.util.ReflectHelper;
import org.hibernate.internal.util.ValueHolder;
@ -536,9 +538,6 @@ class HibernateTypeHelper {
try {
return metadata.getTypeResolver().heuristicType( typeName, typeParameters );
}
catch ( NotYetImplementedException e ){
throw e;
}
catch ( Exception ignore ) {
}
}

View File

@ -25,27 +25,45 @@ package org.hibernate.metamodel.internal.source.annotations;
import java.util.Map;
import org.hibernate.internal.util.ValueHolder;
import org.hibernate.metamodel.internal.source.annotations.attribute.MappedAttribute;
import org.hibernate.metamodel.spi.source.ExplicitHibernateTypeSource;
/**
* @author Hardy Ferentschik
* @author Strong Liu
*/
public class ExplicitHibernateTypeSourceImpl implements ExplicitHibernateTypeSource {
private final MappedAttribute attribute;
private final ValueHolder<String> nameHolder;
private final ValueHolder<Map<String, String>> parameterHolder;
public ExplicitHibernateTypeSourceImpl(MappedAttribute attribute) {
this.attribute = attribute;
public ExplicitHibernateTypeSourceImpl(final MappedAttribute attribute) {
this.nameHolder = new ValueHolder<String>(
new ValueHolder.DeferredInitializer<String>() {
@Override
public String initialize() {
return attribute.getHibernateTypeResolver().getExplicitHibernateTypeName();
}
}
);
this.parameterHolder = new ValueHolder<Map<String, String>>(
new ValueHolder.DeferredInitializer<Map<String, String>>() {
@Override
public Map<String, String> initialize() {
return attribute.getHibernateTypeResolver().getExplicitHibernateTypeParameters();
}
}
);
}
@Override
public String getName() {
return attribute.getHibernateTypeResolver().getExplicitHibernateTypeName();
return nameHolder.getValue();
}
@Override
public Map<String, String> getParameters() {
return attribute.getHibernateTypeResolver().getExplicitHibernateTypeParameters();
return parameterHolder.getValue();
}
}

View File

@ -25,10 +25,13 @@
package org.hibernate.metamodel.internal.source.annotations.attribute.type;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.hibernate.internal.util.StringHelper;
import org.hibernate.metamodel.internal.source.annotations.attribute.MappedAttribute;
import org.hibernate.usertype.DynamicParameterizedType;
import org.jboss.jandex.AnnotationInstance;
/**
@ -63,13 +66,17 @@ public abstract class AbstractAttributeTypeResolver implements AttributeTypeReso
@Override
final public Map<String, String> getExplicitHibernateTypeParameters() {
Map<String, String> result = new HashMap<String, String>( );
//this is only use by enum type and serializable blob type, but we put there anyway
result.put(
DynamicParameterizedType.RETURNED_CLASS,
mappedAttribute.getAttributeType().getName()
);
if ( StringHelper.isNotEmpty( getExplicitHibernateTypeName() ) ) {
return resolveHibernateTypeParameters(
getTypeDeterminingAnnotationInstance() );
}
else {
return Collections.emptyMap();
result.putAll( resolveHibernateTypeParameters(
getTypeDeterminingAnnotationInstance() ) );
}
return result;
}
final protected boolean hasEntityTypeDef() {

View File

@ -76,7 +76,6 @@ public class EnumeratedTypeResolver extends AbstractAttributeTypeResolver {
@Override
protected Map<String, String> resolveHibernateTypeParameters(AnnotationInstance annotationInstance) {
HashMap<String, String> typeParameters = new HashMap<String, String>();
typeParameters.put( EnumType.ENUM, mappedAttribute.getAttributeType().getName() );
if ( annotationInstance != null ) {
javax.persistence.EnumType enumType = JandexHelper.getEnumValue(
annotationInstance,
@ -85,9 +84,11 @@ public class EnumeratedTypeResolver extends AbstractAttributeTypeResolver {
);
if ( javax.persistence.EnumType.ORDINAL.equals( enumType ) ) {
typeParameters.put( EnumType.TYPE, String.valueOf( Types.INTEGER ) );
typeParameters.put( EnumType.NAMED, String.valueOf( false ) );
}
else if ( javax.persistence.EnumType.STRING.equals( enumType ) ) {
typeParameters.put( EnumType.TYPE, String.valueOf( Types.VARCHAR ) );
typeParameters.put( EnumType.NAMED, String.valueOf( true ) );
}
else {
throw new AssertionFailure( "Unknown EnumType: " + enumType );

View File

@ -27,9 +27,12 @@ package org.hibernate.metamodel.internal.source.annotations.attribute.type;
import java.io.Serializable;
import java.sql.Blob;
import java.sql.Clob;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.hibernate.boot.registry.classloading.spi.ClassLoaderService;
import org.hibernate.internal.util.StringHelper;
import org.hibernate.metamodel.internal.source.annotations.attribute.MappedAttribute;
import org.hibernate.metamodel.internal.source.annotations.util.JPADotNames;
import org.hibernate.metamodel.internal.source.annotations.util.JandexHelper;
@ -38,6 +41,8 @@ import org.hibernate.type.PrimitiveCharacterArrayClobType;
import org.hibernate.type.SerializableToBlobType;
import org.hibernate.type.StandardBasicTypes;
import org.hibernate.type.WrappedMaterializedBlobType;
import org.hibernate.usertype.DynamicParameterizedType;
import org.jboss.jandex.AnnotationInstance;
/**
@ -58,9 +63,10 @@ public class LobTypeResolver extends AbstractAttributeTypeResolver {
@Override
public String resolveAnnotatedHibernateTypeName(AnnotationInstance annotationInstance) {
if ( annotationInstance == null ) {
//only check attributes annotated with @Lob
return null;
}
String type = null;
String type = "blob";
if ( Clob.class.isAssignableFrom( mappedAttribute.getAttributeType() ) ) {
type = StandardBasicTypes.CLOB.getName();
}
@ -85,22 +91,11 @@ public class LobTypeResolver extends AbstractAttributeTypeResolver {
else if ( Serializable.class.isAssignableFrom( mappedAttribute.getAttributeType() ) ) {
type = SerializableToBlobType.class.getName();
}
else {
type = "blob";
}
return type;
}
@Override
protected Map<String, String> resolveHibernateTypeParameters(AnnotationInstance annotationInstance) {
if ( getExplicitHibernateTypeName().equals( SerializableToBlobType.class.getName() ) ) {
HashMap<String, String> typeParameters = new HashMap<String, String>();
typeParameters.put(
SerializableToBlobType.CLASS_NAME,
mappedAttribute.getAttributeType().getName()
);
return typeParameters;
}
return null;
return Collections.emptyMap();
}
}

View File

@ -39,6 +39,7 @@ import org.hibernate.AssertionFailure;
import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.internal.util.ReflectHelper;
import org.hibernate.internal.util.StringHelper;
import org.hibernate.internal.util.config.ConfigurationHelper;
import org.hibernate.usertype.DynamicParameterizedType;
import org.hibernate.usertype.EnhancedUserType;
@ -68,7 +69,10 @@ import org.hibernate.usertype.EnhancedUserType;
@SuppressWarnings("unchecked")
public class EnumType implements EnhancedUserType, DynamicParameterizedType, Serializable, StringRepresentableType {
private static final Logger LOG = Logger.getLogger( EnumType.class.getName() );
/**
* @deprecated use {@link DynamicParameterizedType#RETURNED_CLASS} instead.
*/
@Deprecated
public static final String ENUM = "enumClass";
public static final String NAMED = "useNamed";
public static final String TYPE = "type";
@ -232,6 +236,9 @@ public class EnumType implements EnhancedUserType, DynamicParameterizedType, Ser
}
else {
String enumClassName = (String) parameters.get( ENUM );
if( StringHelper.isEmpty(enumClassName)){
enumClassName = (String)parameters.get( DynamicParameterizedType.RETURNED_CLASS );
}
try {
enumClass = ReflectHelper.classForName( enumClassName, this.getClass() ).asSubclass( Enum.class );
}

View File

@ -28,6 +28,7 @@ import java.util.Properties;
import org.hibernate.MappingException;
import org.hibernate.internal.util.ReflectHelper;
import org.hibernate.internal.util.StringHelper;
import org.hibernate.type.descriptor.java.SerializableTypeDescriptor;
import org.hibernate.type.descriptor.sql.BlobTypeDescriptor;
import org.hibernate.usertype.DynamicParameterizedType;
@ -36,15 +37,14 @@ import org.hibernate.usertype.DynamicParameterizedType;
* @author Brett Meyer
*/
public class SerializableToBlobType<T extends Serializable> extends AbstractSingleColumnStandardBasicType<T> implements DynamicParameterizedType {
/**
* @deprecated use {@link DynamicParameterizedType#RETURNED_CLASS} instead.
*/
@Deprecated
public static final String CLASS_NAME = "classname";
private static final long serialVersionUID = 1L;
/**
* @param sqlTypeDescriptor
* @param javaTypeDescriptor
*/
public SerializableToBlobType() {
super( BlobTypeDescriptor.DEFAULT, new SerializableTypeDescriptor( Serializable.class ) );
}
@ -67,7 +67,10 @@ public class SerializableToBlobType<T extends Serializable> extends AbstractSing
} else {
String className = parameters.getProperty( CLASS_NAME );
if ( className == null ) {
throw new MappingException( "No class name defined for type: " + SerializableToBlobType.class.getName() );
className = parameters.getProperty( DynamicParameterizedType.RETURNED_CLASS );
if ( StringHelper.isEmpty( className ) ) {
throw new MappingException( "No class name defined for type: " + SerializableToBlobType.class.getName() );
}
}
try {
setJavaTypeDescriptor( new SerializableTypeDescriptor<T>( ReflectHelper.classForName( className ) ) );

View File

@ -133,9 +133,6 @@ public class TypeResolver implements Serializable {
try {
Class typeClass = ReflectHelper.classForName( typeName );
if ( typeClass != null ) {
if( DynamicParameterizedType.class.isAssignableFrom( typeClass )){
throw new NotYetImplementedException( "Custom dynamicParameterizedType is not supported yet" );
}
return typeFactory.byClass( typeClass, parameters );
}
}

View File

@ -36,17 +36,23 @@ import java.lang.annotation.Annotation;
* @author Janario Oliveira
*/
public interface DynamicParameterizedType extends ParameterizedType {
@Deprecated
public static final String PARAMETER_TYPE = "org.hibernate.type.ParameterType";
@Deprecated
public static final String IS_DYNAMIC = "org.hibernate.type.ParameterType.dynamic";
public static final String RETURNED_CLASS = "org.hibernate.type.ParameterType.returnedClass";
public static final String IS_PRIMARY_KEY = "org.hibernate.type.ParameterType.primaryKey";
public static final String ENTITY = "org.hibernate.type.ParameterType.entityClass";
public static final String PROPERTY = "org.hibernate.type.ParameterType.propertyName";
public static final String ACCESS_TYPE = "org.hibernate.type.ParameterType.accessType";
public static final String XPROPERTY = "org.hibernate.type.ParameterType.xproperty";
public static final String IS_PRIMARY_KEY = "org.hibernate.type.ParameterType.primaryKey";
@Deprecated
public static final String ENTITY = "org.hibernate.type.ParameterType.entityClass";
@Deprecated
public static final String PROPERTY = "org.hibernate.type.ParameterType.propertyName";
@Deprecated
public static final String ACCESS_TYPE = "org.hibernate.type.ParameterType.accessType";
@Deprecated
public static final String XPROPERTY = "org.hibernate.type.ParameterType.xproperty";
@Deprecated
public static interface ParameterType {
public Class getReturnedClass();

View File

@ -4,7 +4,6 @@ import org.junit.Test;
import org.hibernate.Session;
import org.hibernate.metamodel.spi.binding.EntityBinding;
import org.hibernate.testing.FailureExpectedWithNewMetamodel;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.hibernate.type.SerializableToBlobType;
import org.hibernate.type.Type;
@ -16,7 +15,6 @@ import static org.junit.Assert.assertEquals;
*
* @author Janario Oliveira
*/
@FailureExpectedWithNewMetamodel(jiraKey = "HHH-7936")
public class SerializableToBlobTypeTest extends BaseCoreFunctionalTestCase {
@Test
public void testTypeDefinition() {