我想通过根据某些条件过滤现有元素来从现有列表中收集新列表,但是下面的代码抛出了强制转换异常(java.lang.object;无法强制转换为PaymentDelayInfo)。这是什么原因呢?
我尝试先将列表元素放入流中,然后尝试过滤和收集,但它也无法正常工作。
public MaxLatencyCounts calculateMaxLatencyCounts(LocalDate reportDate, String loanId) {
final List<PaymentDelayInfo> maxLatencyDaysInLast12Month = oraclePaymentDelayInformationRepository.getPaymentDelayInfoListInLast12Months(reportDate, loanId);
if (maxLatencyDaysInLast12Month == null || maxLatencyDaysInLast12Month.isEmpty()) {
return null;
}
List<PaymentDelayInfo> collect = maxLatencyDaysInLast12Month
.stream()
.filter(maxLatencyDaysInLast6 -> maxLatencyDaysInLast6.getDate() != null)
.filter(date -> date.getDate().isAfter(reportDate.minusMonths(6))).collect(Collectors.toList());
final List<PaymentDelayInfo> maxLatencyDaysInLast3Month = maxLatencyDaysInLast12Month
.stream()
.filter(maxLatencyDaysInLast3 -> maxLatencyDaysInLast3.getDate() != null)
.filter(date -> date.getDate().isAfter(reportDate.minusMonths(3)))
.collect(Collectors.toList());
final MaxLatencyCounts maxLatencyCounts = MaxLatencyCounts.builder()
.maxLatencyDayInLast3Month(calculateMaxLatencyDayCount(maxLatencyDaysInLast3Month))
.maxLatencyDayInLast6Month(calculateMaxLatencyDayCount(collect))
.maxLatencyDayInLast12Month(calculateMaxLatencyDayCount(maxLatencyDaysInLast12Month))
.build();
return maxLatencyCounts;
}
因此,我想创建两个新列表及其由适合条件的旧列表元素组成的元素。我该怎么办?
更新:在存储库代码下方;
public List<PaymentDelayInfo> getPaymentDelayInfoListInLast12Months(final LocalDate reportDate, final String loanId) {
final String reportDateParameter = reportDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
final String query = "SELECT a.* " +
"FROM FINANCE.LATENCY_DAYS a " +
"WHERE a.talep_no = :loanId " +
"AND a.tarih >= add_months(trunc(TO_DATE(:reportDate,'yyyy-mm-dd')),-12)";
final Query paymentDelayInfoListQuery = entityManager.createNativeQuery(query);
paymentDelayInfoListQuery.setParameter("reportDate", reportDateParameter);
paymentDelayInfoListQuery.setParameter("loanId", loanId);
return paymentDelayInfoListQuery.getResultList();
}
及以下实体;
@Entity
@Table(name = "LATENCY_DAYS", schema = "FINANCE")
@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Cacheable(false)
@IdClass(PaymentDelayInfoId.class)
public class PaymentDelayInfo {
@Id
@Column(name = "GECIKME_GUN")
private Integer delay;
@Id
@Column(name = "TALEP_NO")
private String loanId;
@Id
@Column(name = "TARIH")
private LocalDate date;
}
class PaymentDelayInfoId implements Serializable {
private Integer delay;
private String loanId;
private LocalDate date;
}
下面是我的堆栈跟踪:
{"timestamp":"2019-05-02 10:33:26.438","level":"WARN","thread":"http-nio-4567-exec-2","mdc":{"traceId":"ca59a358bf36528a","spanId":"ca59a358bf36528a","spanExportable":"true","X-Span-Export":"true","X-B3-SpanId":"ca59a358bf36528a","X-B3-TraceId":"ca59a358bf36528a"},"logger":"com.kocfinans.loancontactinfo.application.LoanContactInfoController","message":"Unhandled exception occurred!","context":"default","exception":"…
java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.kocfinans.loancontactinfo.infrastructure.entities.PaymentDelayInfo
at java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:174)
at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1382)
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499)
at com.kocfinans.loancontactinfo.application.calculation.PaymentDelayCalculationService.calculateMaxLatencyCounts(PaymentDelayCalculationService.java:49)
at com.kocfinans.loancontactinfo.application.DefaultLoanContactInfoService.generateLoanContactInfoResponse(DefaultLoanContactInfoService.java:57)
at com.kocfinans.loancontactinfo.application.LoanContactInfoController.getLoanContactInformation(LoanContactInfoController.java:30)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:209)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:891)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:797)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:991)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:925)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:981)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:873)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:635)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:858)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.boot.actuate.web.trace.servlet.HttpTraceFilter.doFilterInternal(HttpTraceFilter.java:90)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:109)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:93)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.cloud.sleuth.instrument.web.ExceptionLoggingFilter.doFilter(ExceptionLoggingFilter.java:48)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at brave.servlet.TracingFilter.doFilter(TracingFilter.java:86)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.boot.actuate.metrics.web.servlet.WebMvcMetricsFilter.filterAndRecordMetrics(WebMvcMetricsFilter.java:117)
at org.springframework.boot.actuate.metrics.web.servlet.WebMvcMetricsFilter.doFilterInternal(WebMvcMetricsFilter.java:106)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:493)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:800)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:806)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1498)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)
…"}
答案 0 :(得分:0)
return paymentDelayInfoListQuery.getResultList();
会返回一个List<Object[]>
,它不能转换为List<PaymentDelayInfo>
。
您需要做的是使用entityManager
返回TypedQuery<T>
的方法之一
entityManager#createQuery(String qlString, Class<T> resultClass)
是您在这种情况下要寻找的东西