spring data jpa-基于接口的投影中的自定义类型转换 [英] spring data jpa - Custom type conversion in interface-based projection

查看:462
本文介绍了spring data jpa-基于接口的投影中的自定义类型转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现基于接口的投影,但是我无法使其与我的自定义类型列配合使用.

I'm trying to implement Interface-based Projection but I cannot make it work with my custom type column.

下面是我要做的事的例子:

Below example of what I'm trying to do:

存储库:

@Query(value = "SELECT customType from TABLE", nativeQuery = true)
List<TestClass> getResults();

接口投影:

public interface TestClass {
  @Convert(converter = MyCustomTypeConverter.class)
  MyCustomType getCustomType();
}

转换器:

@Converter
public class MyCustomTypeConverter implements Converter<String, MyCustomType> {

      @Override
      public MyCustomType convert(String source) {
        // whatever
      }
}

当我在存储库上调用getResults()时,会收到预期的结果列表,但是当我尝试对其中一个结果调用getCustomType()时,会出现异常:

When I call getResults() on repository I receive list of results as expected, but when I try to call getCustomType() on one of results I get exception:

java.lang.IllegalArgumentException: Projection type must be an interface!
at org.springframework.util.Assert.isTrue(Assert.java:118)
at org.springframework.data.projection.ProxyProjectionFactory.createProjection(ProxyProjectionFactory.java:100)
at org.springframework.data.projection.SpelAwareProxyProjectionFactory.createProjection(SpelAwareProxyProjectionFactory.java:45)
at org.springframework.data.projection.ProjectingMethodInterceptor.getProjection(ProjectingMethodInterceptor.java:131)
at org.springframework.data.projection.ProjectingMethodInterceptor.invoke(ProjectingMethodInterceptor.java:80)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.data.projection.ProxyProjectionFactory$TargetAwareMethodInterceptor.invoke(ProxyProjectionFactory.java:245)

我发现问题出在

org.springframework.data.projection.ProxyProjectionFactory

使用

org.springframework.core.convert.support.DefaultConversionService

显然没有注册我的自定义类型转换器.

which obviously doesn't have my custom type converter registered.

如果我在ConversionService的断点处停止并在运行时手动添加转换器,则投影将正常进行.

If I stop on breakpoint in ConversionService and manually add my converter in runtime, projection will work without any problem.

所以问题是:我可以在基于接口的投影过程中以某种方式将我的自定义转换器注册到spring jpa使用的ConversionService吗?

我将转换器添加到InitializingBean中的DefaultConversionService的sharedInstance中,如下所示,并且可以正常工作.

I added my converter to DefaultConversionService's sharedInstance in InitializingBean like below and it worked.

@Component
public class DefaultConversionServiceInitializer implements InitializingBean {

    @Override
    public void afterPropertiesSet() {
        DefaultConversionService conversionService = (DefaultConversionService) DefaultConversionService.getSharedInstance();
        conversionService.addConverter(new MyCustomTypeConverter());
    }
}

推荐答案

使用的ConversionServiceDefaultConversionService.getSharedInstance().

因此,您应该可以访问它并添加您的转换器.

So you should be able to access that and add your converter.

这篇关于spring data jpa-基于接口的投影中的自定义类型转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆