【发布时间】:2016-07-12 03:18:16
【问题描述】:
我使用的是 hibernate-core 5.1.0,并且我已经实现了一个转换器,它为一个字段构建了一个 HashMap。问题是在构建 sessionFactory 时休眠失败。它抛出“无法实例化 AttributeConverter”,我已经附加了整个跟踪。
有趣的是,如果我创建一个扩展 HashMap
还有其他方法可以解决这个问题吗?
我的转换器:
@Converter(autoApply=true)
public class JsonKeyValueConverter implements
AttributeConverter<HashMap<String, String>, String> // DOESNT work
//AttributeConverter<ClassExtendingHashMap, String> // works
{
public String convertToDatabaseColumn(HashMap<String, String> arg0) {
if ( arg0 == null ) {
return null;
}
return DBUtility.GSON.toJson(arg0);
}
public KeyValueData convertToEntityAttribute(String arg0) {
arg0 = StringUtils.isBlank(arg0) ? null : arg0;
return (KeyValueData) DBUtility.GSON.fromJson(arg0, HashMap.class );
}
}
java.lang.IllegalStateException:无法实例化 属性转换器 [org.labs.collab.repo.entity.conversion.JsonKeyValueConverter 在 org.hibernate.cfg.AbstractPropertyHolder.resolveAttributeConverterDefinition(AbstractPropertyHolder.java:98) 在 org.hibernate.cfg.annotations.PropertyBinder.makePropertyAndValue(PropertyBinder.java:195) 在 org.hibernate.cfg.annotations.PropertyBinder.makePropertyValueAndBind(PropertyBinder.java:216) 在 org.hibernate.cfg.AnnotationBinder.processElementAnnotations(AnnotationBinder.java:2238) 在 org.hibernate.cfg.AnnotationBinder.processIdPropertiesIfNotAlready(AnnotationBinder.java:963) 在 org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:796) 在 org.hibernate.cfg.Configuration$MetadataSourceQueue.processAnnotatedClassesQueue(Configuration.java:3788) 在 org.hibernate.cfg.Configuration$MetadataSourceQueue.processMetadata(Configuration.java:3742) 在 org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1410) 在 org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1844) 引起:org.hibernate.AnnotationException:无法创建 AttributeConverter 实例位于 org.hibernate.cfg.AbstractPropertyHolder.makeAttributeConverterDefinition(AbstractPropertyHolder.java:132) 在 org.hibernate.cfg.AbstractPropertyHolder.resolveAttributeConverterDefinition(AbstractPropertyHolder.java:95) ... 27 更多原因:java.lang.ClassCastException: sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl 不能 转换为 java.lang.Class 在 org.hibernate.cfg.AttributeConverterDefinition.(AttributeConverterDefinition.java:67) 在 org.hibernate.cfg.AbstractPropertyHolder.makeAttributeConverterDefinition(AbstractPropertyHolder.java:129) ... 28 更多
谢谢!
【问题讨论】: