我正在尝试实现Interface-based Projection,但无法使其与我的自定义类型列一起使用。
下面我想做的事:
存储库:
@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 AttributeConverter<MyCustomType, String> {
@Override
public String convertToDatabaseColumn(MyCustomType o) {
// whatever;
}
@Override
public MyCustomType convertToEntityAttribute(String value) {
// whatever;
}
}
[当我在存储库上调用getResults()时,收到了预期的结果列表,但是当我尝试对其中一个结果调用getCustomType()时,出现异常:
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
显然没有注册我的自定义类型转换器。
如果我在ConversionService的断点处停止并在运行时手动添加转换器,则投影将正常进行。
所以问题是:我可以在基于接口的投影过程中以某种方式将我的自定义转换器注册到spring jpa使用的ConversionService吗?
我正在尝试实现基于接口的投影,但无法使其与我的自定义类型列一起使用。下面是我要执行的操作示例:存储库:@Query(value =“从...中选择SELECT customType ...
使用的ConversionService
是DefaultConversionService.getSharedInstance()
。