JPA查询出错

时间:2015-07-21 11:02:51

标签: java hibernate jpa casting

我正在创建Web服务以获取基于另一个下拉列表的下拉列表。

JPA课程:

public interface TbiAuthorizationMasterDao extends CrudRepository<TbiAuthorizationMaster, Integer>, JpaRepository<TbiAuthorizationMaster, Integer> {

@Query("select a.authorizationName from TbiAuthorizationMaster a, TbiAuthorizationDetail d where a.authorizationId=d.tbiAuthorizationMaster.authorizationId and d.valueName=?1")
    List<TbiAuthorizationMaster> findByValueName(String valueName);
}  

持久性等级:

@Override
    public List getReportList(String reportType) {
        List<TbiAuthorizationMaster> reportList = tbiAuthorizationMasterDao.findByValueName(reportType);
        List<AuthMaster> detailResponse = new ArrayList<>();

        for(TbiAuthorizationMaster aDeatil: reportList){
            AuthMaster res = new AuthMaster();
            res.setAuthorizationId(aDeatil.getAuthorizationId());
            res.setAuthorizationName(aDeatil.getAuthorizationName());
            detailResponse.add(res);
        }
        return detailResponse;
    }

我收到错误,我不理解。 Bcoz我在其他服务中使用过它。它工作正常,在这里我无法在查询中加入2个表

错误日志:

java.lang.ClassCastException: java.lang.String cannot be cast to com.acinfotech.timebound.jpa.model.TbiAuthorizationMaster
    at com.acinfotech.timebound.jpa.service.ReportJobsPersistenceServiceImpl.getReportList(ReportJobsPersistenceServiceImpl.java:7876)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
    at com.sun.proxy.$Proxy81.getReportList(Unknown Source)
    at com.acinfotech.timebound.restservice.service.RestServiceImpl.getReportList(RestServiceImpl.java:2771)
    at com.acinfotech.timebound.restservice.service.RestrsService.listReports(RestrsService.java:9940)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)

行中的错误:

for(TbiAuthorizationMaster aDeatil: reportList){  

有人可以帮忙吗。

2 个答案:

答案 0 :(得分:4)

您正在尝试将a.authorizationName(我猜是String)投射到TbiAuthorizationMaster课程。

尝试将查询更改为:

@Query("select a from TbiAuthorizationMaster a, TbiAuthorizationDetail d where a.authorizationId=d.tbiAuthorizationMaster.authorizationId and d.valueName=?1")
List<TbiAuthorizationMaster> findByValueName(String valueName);

答案 1 :(得分:2)

这里的问题是您在查询中选择了collections.OrderedDict(),并且您正在将结果分配给String个对象的列表。

因此,您尝试将TbiAuthorizationMaster转换为String类。

你有两个选择:

1。更改您的查询,这样它将返回整个TbiAuthorizationMaster对象:

TbiAuthorizationMaster

2。或者在结果中使用@Query("select a from TbiAuthorizationMaster a, TbiAuthorizationDetail d where a.authorizationId=d.tbiAuthorizationMaster.authorizationId and d.valueName=?1") List<TbiAuthorizationMaster> findByValueName(String valueName); 来匹配List<String>,如下所示:

authorizationName