web-dev-qa-db-ja.com

Spring JPA ::型から変換できるコンバータが見つかりません

JPAでspringを使用し、@ queryでSQL queryを使用してクエリを実行し、結果をオブジェクトにマッピングしようとしています。すべての列が必要なわけではないため、別のエンティティクラスと他のdtoへのマッピングがあります。エラーを下回る

タイプ[org.springframework.data.jpa.repository.query.AbstractJpaQuery $ TupleConverter $ TupleBackedMap]からタイプ[webapi.dto.StatsDTO] "に変換できるコンバーターが見つかりませんでした。

以下は私のリポジトリインターフェイスです

public interface UserRepository extends CrudRepository<UserEntity, Long> {
    @Query(value = "select count(type_id) userCount, type_id, modified_at from "
                + "user_campaign_objective where camp_id = ?1 group by objective_type_id,modified_at", nativeQuery = true)

        List<StatsDTO> getStatsDTO(Long camp_id);
}

以下はStatsDTOです。

@Data
@NoArgsConstructor
@AllArgsConstructor
public class StatsDTO {
    private Integer userCount;
    private Byte typeId;
    private Instant modifiedAt;

}
  1. 上記の問題を修正する方法。
  2. 問題が解決できない場合、spring data jpaを使用して@queryなしでそれを行うことはできますか?どうやって?

編集:: JPQLの使用::

@Query("select new webapi.campaign.dto.StatsDTO(count(u.objective_type_id),u.objective_type_id,u.modified_at) "
        + "from user_campaign_objective u where u.campaign_id = ?1 group by u.objective_type_id,u.modified_at")

エラーを下回る

    Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: user_campaign_objective is not mapped [select new webapi.campaign.dto.StatsDTO(count(u.objective_type_id),u.objective_type_id,u.modified_at) from user_campaign_objective u where u.campaign_id = ?1 group by u.objective_type_id,u.modified_at]
    at org.hibernate.hql.internal.ast.QuerySyntaxException.generateQueryException(QuerySyntaxException.Java:79) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]     
at org.hibernate.QueryException.wrapWithQueryString(QueryException.Java:103) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]    
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.Java:217) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]   at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.Java:141) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]     at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.Java:115) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]    
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.Java:77) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]     at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.Java:153) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]   
at org.hibernate.internal.AbstractSharedSessionContract.getQueryPlan(AbstractSharedSessionContract.Java:553) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]    
at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.Java:662) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]     ... 79 common frames omitted Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: user_campaign_objective is not mapped  
at org.hibernate.hql.internal.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.Java:169) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final] 
    at org.hibernate.hql.internal.ast.tree.FromElementFactory.addFromElement(FromElementFactory.Java:91) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]    at org.hibernate.hql.internal.ast.tree.FromClause.addFromElement(FromClause.Java:79) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]    
at org.hibernate.hql.internal.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.Java:326) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final] 
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.Java:3706) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]    
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.Java:3595) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]    
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.Java:720) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]  
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.Java:576) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]   
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.Java:313) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]     
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.Java:261) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]   
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.Java:266) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final] 
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.Java:189) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]   ... 85 common frames omitted

Sqlを使用してnativequeryを使用している場合、以下のエラーを取得します

@Query(value = "select count(objective_type_id), objective_type_id, modified_at from "
        + "user_campaign_objective where campaign_id = ?1 group by objective_type_id,modified_at", nativeQuery = true)

スタックトレース

org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [org.springframework.data.jpa.repository.query.AbstractJpaQuery$TupleConverter$TupleBackedMap] to type [webapi.campaign.dto.StatsDTO]   
at org.springframework.core.convert.support.GenericConversionService.handleConverterNotFound(GenericConversionService.Java:321) ~[spring-core-5.0.7.RELEASE.jar:5.0.7.RELEASE]  
at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.Java:194) ~[spring-core-5.0.7.RELEASE.jar:5.0.7.RELEASE]  
at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.Java:174) ~[spring-core-5.0.7.RELEASE.jar:5.0.7.RELEASE]  
at org.springframework.data.repository.query.ResultProcessor$ProjectingConverter.convert(ResultProcessor.Java:293) ~[spring-data-commons-2.0.8.RELEASE.jar:2.0.8.RELEASE]   
at org.springframework.data.repository.query.ResultProcessor$ChainingConverter.lambda$and$0(ResultProcessor.Java:213) ~[spring-data-commons-2.0.8.RELEASE.jar:2.0.8.RELEASE]    
at org.springframework.data.repository.query.ResultProcessor$ChainingConverter.convert(ResultProcessor.Java:224) ~[spring-data-commons-2.0.8.RELEASE.jar:2.0.8.RELEASE]     
at org.springframework.data.repository.query.ResultProcessor.processResult(ResultProcessor.Java:152) ~[spring-data-commons-2.0.8.RELEASE.jar:2.0.8.RELEASE]     

at org.springframework.data.jpa.repository.query.AbstractJpaQuery.doExecute(AbstractJpaQuery.Java:141) ~[spring-data-jpa-2.0.8.RELEASE.jar:2.0.8.RELEASE]   
at org.springframework.data.jpa.repository.query.AbstractJpaQuery.execute(AbstractJpaQuery.Java:125) ~[spring-data-jpa-2.0.8.RELEASE.jar:2.0.8.RELEASE]     at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.Java:590) ~[spring-data-commons-2.0.8.RELEASE.jar:2.0.8.RELEASE]  
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.Java:578) ~[spring-data-commons-2.0.8.RELEASE.jar:2.0.8.RELEASE]    
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.Java:185) ~[spring-aop-5.0.7.RELEASE.jar:5.0.7.RELEASE]  
at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.Java:59) ~[spring-data-commons-2.0.8.RELEASE.jar:2.0.8.RELEASE]     
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.Java:185) ~[spring-aop-5.0.7.RELEASE.jar:5.0.7.RELEASE]  
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.Java:294) ~[spring-tx-5.0.7.RELEASE.jar:5.0.7.RELEASE]     
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.Java:98) ~[spring-tx-5.0.7.RELEASE.jar:5.0.7.RELEASE]   
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.Java:185) ~[spring-aop-5.0.7.RELEASE.jar:5.0.7.RELEASE]  
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.Java:139) ~[spring-tx-5.0.7.RELEASE.jar:5.0.7.RELEASE]  
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.Java:185) ~[spring-aop-5.0.7.RELEASE.jar:5.0.7.RELEASE]  
at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.Java:135) ~[spring-data-jpa-2.0.8.RELEASE.jar:2.0.8.RELEASE]    
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.Java:185) ~[spring-aop-5.0.7.RELEASE.jar:5.0.7.RELEASE]  
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.Java:92) ~[spring-aop-5.0.7.RELEASE.jar:5.0.7.RELEASE]    
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.Java:185) ~[spring-aop-5.0.7.RELEASE.jar:5.0.7.RELEASE]  
at org.springframework.data.repository.core.support.SurroundingTransactionDetectorMethodInterceptor.invoke(SurroundingTransactionDetectorMethodInterceptor.Java:61) ~[spring-data-commons-2.0.8.RELEASE.jar:2.0.8.RELEASE]  
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.Java:185) ~[spring-aop-5.0.7.RELEASE.jar:5.0.7.RELEASE]  
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.Java:212) ~[spring-aop-5.0.7.RELEASE.jar:5.0.7.RELEASE]   
at com.Sun.proxy.$Proxy138.findByCampaignID(Unknown Source) ~[na:na]    
at waypedia.webapi.campaign.fullymanaged.CampaignCreatorService.campaignStats(CampaignCreatorService.Java:477) ~[main/:na]  
at waypedia.webapi.campaign.fullymanaged.CampaignCreatorController.campaignStats(CampaignCreatorController.Java:251) ~[main/:na]    
at Java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]  
at Java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.Java:62) ~[na:na]    
at Java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.Java:43) ~[na:na]    
at Java.base/Java.lang.reflect.Method.invoke(Method.Java:564) ~[na:na]  at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.Java:209) ~[spring-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]    
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.Java:136) ~[spring-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]    
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.Java:102) ~[spring-webmvc-5.0.7.RELEASE.jar:5.0.7.RELEASE]     
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.Java:877) ~[spring-webmvc-5.0.7.RELEASE.jar:5.0.7.RELEASE]   
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.Java:783) ~[spring-webmvc-5.0.7.RELEASE.jar:5.0.7.RELEASE]    
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.Java:87) ~[spring-webmvc-5.0.7.RELEASE.jar:5.0.7.RELEASE]    
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.Java:991) ~[spring-webmvc-5.0.7.RELEASE.jar:5.0.7.RELEASE]    
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.Java:925) ~[spring-webmvc-5.0.7.RELEASE.jar:5.0.7.RELEASE]     
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.Java:974) ~[spring-webmvc-5.0.7.RELEASE.jar:5.0.7.RELEASE]  
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.Java:877) ~[spring-webmvc-5.0.7.RELEASE.jar:5.0.7.RELEASE]  
at javax.servlet.http.HttpServlet.service(HttpServlet.Java:661) ~[Tomcat-embed-core-8.5.31.jar:8.5.31]  at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.Java:851) ~[spring-webmvc-5.0.7.RELEASE.jar:5.0.7.RELEASE]     
at javax.servlet.http.HttpServlet.service(HttpServlet.Java:742) ~[Tomcat-embed-core-8.5.31.jar:8.5.31]  
at org.Apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.Java:231) ~[Tomcat-embed-core-8.5.31.jar:8.5.31]     
at org.Apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.Java:166) ~[Tomcat-embed-core-8.5.31.jar:8.5.31]     
at org.Apache.Tomcat.websocket.server.WsFilter.doFilter(WsFilter.Java:52) ~[Tomcat-embed-websocket-8.5.31.jar:8.5.31]   
at org.Apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.Java:193) ~[Tomcat-embed-core-8.5.31.jar:8.5.31]     
at org.Apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.Java:166) ~[Tomcat-embed-core-8.5.31.jar:8.5.31]     
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.Java:320) ~[spring-security-web-5.0.6.RELEASE.jar:5.0.6.RELEASE]  
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.Java:127) ~[spring-security-web-5.0.6.RELEASE.jar:5.0.6.RELEASE]    
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.Java:91) ~[spring-security-web-5.0.6.RELEASE.jar:5.0.6.RELEASE]   
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.Java:334) ~[spring-security-web-5.0.6.RELEASE.jar:5.0.6.RELEASE]  
at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.Java:119) ~[spring-security-web-5.0.6.RELEASE.jar:5.0.6.RELEASE]  
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.Java:334) ~[spring-security-web-5.0.6.RELEASE.jar:5.0.6.RELEASE]  
at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.Java:137) ~[spring-security-web-5.0.6.RELEASE.jar:5.0.6.RELEASE]   
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.Java:334) ~[spring-security-web-5.0.6.RELEASE.jar:5.0.6.RELEASE]  
at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.Java:111) ~[spring-security-web-5.0.6.RELEASE.jar:5.0.6.RELEASE]    
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.Java:334) ~[spring-security-web-5.0.6.RELEASE.jar:5.0.6.RELEASE]  
at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.Java:170) ~[spring-security-web-5.0.6.RELEASE.jar:5.0.6.RELEASE]    
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.Java:334) ~[spring-security-web-5.0.6.RELEASE.jar:5.0.6.RELEASE]  
at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.Java:63) ~[spring-security-web-5.0.6.RELEASE.jar:5.0.6.RELEASE]   
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.Java:334) ~[spring-security-web-5.0.6.RELEASE.jar:5.0.6.RELEASE]  
at org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilterInternal(BasicAuthenticationFilter.Java:158) ~[spring-security-web-5.0.6.RELEASE.jar:5.0.6.RELEASE]    
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.Java:107) ~[spring-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]    
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.Java:334) ~[spring-security-web-5.0.6.RELEASE.jar:5.0.6.RELEASE]  
at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.Java:116) ~[spring-security-web-5.0.6.RELEASE.jar:5.0.6.RELEASE]   
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.Java:334) ~[spring-security-web-5.0.6.RELEASE.jar:5.0.6.RELEASE]  
at org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.Java:66) ~[spring-security-web-5.0.6.RELEASE.jar:5.0.6.RELEASE]   
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.Java:107) ~[spring-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.Java:334) ~[spring-security-web-5.0.6.RELEASE.jar:5.0.6.RELEASE]  
at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.Java:105) ~[spring-security-web-5.0.6.RELEASE.jar:5.0.6.RELEASE]     
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.Java:334) ~[spring-security-web-5.0.6.RELEASE.jar:5.0.6.RELEASE]  
at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.Java:56) ~[spring-security-web-5.0.6.RELEASE.jar:5.0.6.RELEASE]    
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.Java:107) ~[spring-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]    
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.Java:334) ~[spring-security-web-5.0.6.RELEASE.jar:5.0.6.RELEASE]  
at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.Java:215) ~[spring-security-web-5.0.6.RELEASE.jar:5.0.6.RELEASE]     at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.Java:178) ~[spring-security-web-5.0.6.RELEASE.jar:5.0.6.RELEASE]     
at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.Java:357) ~[spring-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]    at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.Java:270) ~[spring-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]  
at org.Apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.Java:193) ~[Tomcat-embed-core-8.5.31.jar:8.5.31]     
at org.Apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.Java:166) ~[Tomcat-embed-core-8.5.31.jar:8.5.31]     
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.Java:99) ~[spring-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]     
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.Java:107) ~[spring-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]    
at org.Apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.Java:193) ~[Tomcat-embed-core-8.5.31.jar:8.5.31]     
at org.Apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.Java:166) ~[Tomcat-embed-core-8.5.31.jar:8.5.31]     
at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.Java:109) ~[spring-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]    
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.Java:107) ~[spring-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]    
at org.Apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.Java:193) ~[Tomcat-embed-core-8.5.31.jar:8.5.31]     
at org.Apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.Java:166) ~[Tomcat-embed-core-8.5.31.jar:8.5.31]     
at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.Java:93) ~[spring-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]     at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.Java:107) ~[spring-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]    
at org.Apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.Java:193) ~[Tomcat-embed-core-8.5.31.jar:8.5.31]     at org.Apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.Java:166) ~[Tomcat-embed-core-8.5.31.jar:8.5.31]     
at org.springframework.session.web.http.SessionRepositoryFilter.doFilterInternal(SessionRepositoryFilter.Java:147) ~[spring-session-core-2.0.4.RELEASE.jar:2.0.4.RELEASE]   
at org.springframework.session.web.http.OncePerRequestFilter.doFilter(OncePerRequestFilter.Java:81) ~[spring-session-core-2.0.4.RELEASE.jar:2.0.4.RELEASE]  
at org.Apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.Java:193) ~[Tomcat-embed-core-8.5.31.jar:8.5.31]     
at org.Apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.Java:166) ~[Tomcat-embed-core-8.5.31.jar:8.5.31]     
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.Java:200) ~[spring-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]  
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.Java:107) ~[spring-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]    
at org.Apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.Java:193) ~[Tomcat-embed-core-8.5.31.jar:8.5.31]     
at org.Apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.Java:166) ~[Tomcat-embed-core-8.5.31.jar:8.5.31]     
at org.Apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.Java:198) ~[Tomcat-embed-core-8.5.31.jar:8.5.31]   
at org.Apache.catalina.core.StandardContextValve.invoke(StandardContextValve.Java:96) [Tomcat-embed-core-8.5.31.jar:8.5.31]     
at org.Apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.Java:496) [Tomcat-embed-core-8.5.31.jar:8.5.31]     
at org.Apache.catalina.core.StandardHostValve.invoke(StandardHostValve.Java:140) [Tomcat-embed-core-8.5.31.jar:8.5.31]  
at org.Apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.Java:81) [Tomcat-embed-core-8.5.31.jar:8.5.31]   
at org.Apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.Java:87) [Tomcat-embed-core-8.5.31.jar:8.5.31]   
at org.Apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.Java:342) [Tomcat-embed-core-8.5.31.jar:8.5.31]    
at org.Apache.coyote.http11.Http11Processor.service(Http11Processor.Java:803) [Tomcat-embed-core-8.5.31.jar:8.5.31]     
at org.Apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.Java:66) [Tomcat-embed-core-8.5.31.jar:8.5.31]   
at org.Apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.Java:790) [Tomcat-embed-core-8.5.31.jar:8.5.31]    
at org.Apache.Tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.Java:1468) [Tomcat-embed-core-8.5.31.jar:8.5.31]    
at org.Apache.Tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.Java:49) [Tomcat-embed-core-8.5.31.jar:8.5.31]    
at Java.base/Java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.Java:1135) [na:na]    
at Java.base/Java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.Java:635) [na:na]    
at org.Apache.Tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.Java:61) [Tomcat-embed-core-8.5.31.jar:8.5.31]     
at Java.base/Java.lang.Thread.run(Thread.Java:844) [na:na]
7
Anjali

コンストラクター式JPQLクエリを使用できます。クエリは次のようになります。

select new StatsDTO(count(u),u.typeId,u.modifiedAt) from UserCampaignObjective u where campId = ? group by objectiveTypeId,modifiedAt

ただし、コンストラクターStatsDTOが既に存在することを確認してください。

4
David Pham

projectionsを使用して、DTOをクエリによって返された列のゲッターとのインターフェースにすることで、この問題を解決し、結果を得ることができます。それは私が同じ問題に直面したときにしたことです。

たとえば、以下のようにStatsDTOをinterfaceに変更します。 '。'を使用している場合は、必ず列のエイリアスを指定してくださいオペレーター:

public interface StatsDTO {
    Integer getUserCount;
    Byte getTypeId;
    Instant getModifiedAt;
}

また、クエリで、userCount、typeId、modifiedAtなどの列のエイリアスをそれぞれ指定して、正しくマップされるようにします。

4
Thanthu

列名が一致していません:

  • userCount
  • type_id
  • modified_at

そしてあなたの財産名:

  • userCount
  • typeId
  • modifiedAt

これはネイティブクエリであるため、JPA命名戦略は適用されず、列名はプロパティ名と一致する必要があります。したがって、クエリを次のように変更すると、動作するはずです。

select count(type_id) userCount, type_id as typeId, modified_at as modifiedAt from ...
2
Jens Schauder

また、UserRepository Interface宣言を以下に変更してみてください。

public interface UserRepository extends CrudRepository<StatsDTO, Long> {
    @Query(value = "select count(type_id) userCount, type_id, modified_at from "
                + "user_campaign_objective where camp_id = ?1 group by objective_type_id,modified_at", nativeQuery = true)

        List<StatsDTO> getStatsDTO(Long camp_id);
}

変更は:CrudRepository <StatsDTO, Long> from CrudRepository < UserEntity, Long>

別のデータオブジェクトで宣言されたリポジトリ内で異なるデータオブジェクトを使用しているときに、同じ問題に直面しました。

0
arun sebasatian