Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Hibernate AttributeConverter:无法实例化异常_Hibernate_Jpa - Fatal编程技术网

Hibernate AttributeConverter:无法实例化异常

Hibernate AttributeConverter:无法实例化异常,hibernate,jpa,Hibernate,Jpa,我正在使用hibernate core 5.1.0,并且我已经实现了一个转换器,它为字段构建了一个HashMap。问题是hibernate在构建sessionFactory时失败。它抛出了一个“无法实例化AttributeConverter”,我已经附加了整个跟踪 有趣的是,如果我创建一个扩展HashMap的类并在转换器中使用该类(以及Hibernate实体类),那么这个异常就会消失。似乎转换类型不应该使用泛型 解决这个问题还有其他选择吗 我的转换器: @Converter(autoApply

我正在使用hibernate core 5.1.0,并且我已经实现了一个转换器,它为字段构建了一个HashMap。问题是hibernate在构建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 );
        }
    }
@转换器(autoApply=true)
公共类JsonKeyValueConverter实现
AttributeConverter//不工作
//AttributeConverter//works
{
公共字符串convertToDatabaseColumn(HashMap arg0){
如果(arg0==null){
返回null;
}
返回DBUtility.GSON.toJson(arg0);
}
公钥值数据convertToEntityAttribute(字符串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.ProcessAnnotatedClass(Configuration.java:3788) 在 org.hibernate.cfg.Configuration$MetadataSourceQueue.processMetadata(Configuration.java:3742) 在 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 AttributeConverterDefinition.(AttributeConverterDefinition.java:67) 在 org.hibernate.cfg.AbstractPropertyHolder.makeAttributeConverterDefinition(AbstractPropertyHolder.java:129) …还有28个


谢谢!

你说得对,不幸的是,AttributeConverter不适用于参数化类型(泛型),因此最简单的方法是使用:

@Converter(autoApply=true)
public class JsonKeyValueConverter implements 
           AttributeConverter<HashMap, String> {
@转换器(autoApply=true)
公共类JsonKeyValueConverter实现
属性转换器{
它允许您直接在重写的方法中使用
HashMap