【问题标题】:Hibernate AttributeConverter: Unable to instantiate exceptionHibernate AttributeConverter:无法实例化异常
【发布时间】:2016-07-12 03:18:16
【问题描述】:

我使用的是 hibernate-core 5.1.0,并且我已经实现了一个转换器,它为一个字段构建了一个 HashMap。问题是在构建 sessionFactory 时休眠失败。它抛出“无法实例化 AttributeConverter”,我已经附加了整个跟踪。

有趣的是,如果我创建一个扩展 HashMap 的类并在转换器(和 Hibernate 实体类)中使用该类,这个异常就会消失。似乎转换类型不应该使用泛型。

还有其他方法可以解决这个问题吗?

我的转换器:

 @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 更多

谢谢!

【问题讨论】:

    标签: hibernate jpa


    【解决方案1】:

    你是对的,不幸的是,AttributeConverter 不适用于参数类型(泛型),所以最简单的方法是使用:

    @Converter(autoApply=true)
    public class JsonKeyValueConverter implements 
               AttributeConverter<HashMap, String> {
    

    它允许您直接在覆盖的方法中使用HashMap&lt;String, String&gt;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-18
      • 2021-05-24
      • 1970-01-01
      • 1970-01-01
      • 2019-12-23
      • 1970-01-01
      • 2011-10-02
      • 2017-03-27
      相关资源
      最近更新 更多