Error with JPA request

I am creating a web service to get a dropdown based on another dropdown.

JPA class:

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);
}  

      

Resistance class:

@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;
    }

      

I am getting an error that I don't understand. Bcoz I have used the same in another service. It worked fine, in this it doesn't work where I join 2 tables in a query

Error log:

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)

      

Error in line:

for(TbiAuthorizationMaster aDeatil: reportList){  

      

can someone help on this.

+3


source to share


2 answers


You are trying to apply a.authorizationName

(I think it is String

) to a class TbiAuthorizationMaster

.

Try changing your query to:



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

      

+4


source


The problem is when you select String

in your query and assign the result to your list of objects TbiAuthorizationMaster

.

So you are trying to apply the String

to class TbiAuthorizationMaster

.

Here you have two options:

1. Modify your query to return the entire object TbiAuthorizationMaster

:



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

      

2. Or use List<String>

the result to match authorizationName

, for example:

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

      

+2


source







All Articles