Springboot自定义选择查询返回没有找到能够从类型转换的转换器 [英] Springboot custom Select Query returns No converter found capable of converting from type

查看:81
本文介绍了Springboot自定义选择查询返回没有找到能够从类型转换的转换器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Springboot JPA中执行自定义选择查询,

I am trying to execute a custom select query in Springboot JPA,

public interface idnOauth2AccessTokenRepository extends JpaRepository<idnOauth2AccessToken, String>,
        JpaSpecificationExecutor<idnOauth2AccessToken> {
    @Query(value = "select IOCA.userName, IOCA.appName, IOAT.refreshToken, IOAT.timeCreated, IOAT.tokenScopeHash, IOAT.tokenState, IOAT.validityPeriod from idnOauth2AccessToken IOAT inner join idnOauthConsumerApps IOCA on IOCA.ID = IOAT.consumerKeyID where IOAT.tokenState='ACTIVE'")
    List<userApplicationModel> getUserApplicationModel();
}

但是当我执行时,我得到一个错误

But when I execute I get an error of

org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [org.springframework.data.jpa.repository.query.AbstractJpaQuery$TupleConverter$TupleBackedMap] to type [com.adl.egw.Model.user.userApplicationModel]

我尝试了来自互联网的不同类型的答案,但似乎没有任何工作正常.我还尝试为userApplicationModel实现一个新的存储库,但是没有用.

I tried different type of answers from the internet, but I nothing seems to work fine. I also tried implementing a new repository for userApplicationModel but didn't work.

任何可能有帮助的答案或实施方式.

Any answers or implementation which could help.

推荐答案

您要连接来自不同表的列,然后分配给其他对象.这种方式无法正常工作,而且 userApplicationModel 似乎不是托管实体.对于这种情况,您必须使用projection(dto映射).看一下以下 Query :

You are joining columns from different tables and then assigning to a different object. It does not work this way + the userApplicationModel doesn't seem managed entity. For such scenarios, you have to use projection(dto mapping). Take a look of the following Query:

@Query(value = "select new your.package.UserApplicationModelProjection(IOCA.userName, IOCA.appName, IOAT.refreshToken, IOAT.timeCreated, IOAT.tokenScopeHash, IOAT.tokenState, IOAT.validityPeriod)" 
             + " from idnOauth2AccessToken IOAT inner join idnOauthConsumerApps IOCA on IOCA.ID = IOAT.consumerKeyID where IOAT.tokenState='ACTIVE'")
List<UserApplicationModelProjection> getUserApplicationModel();

要映射到的类:

public class UserApplicationModelProjection {
    private String userName;
    private String appName;
    private String refreshToken
    private OffsetDateTime timeCreated
    private String tokenScopeHash;
    private String tokenState; //mind the data type
    private int validityPeriod; //update the data type
    
    public UserApplicationModelProjection(String userName, 
                                        String appName, 
                                        String refreshToken, 
                                        OffsetDateTime timeCreated, 
                                        String tokenScopeHash, 
                                        String tokenState, 
                                        int validityPeriod) 
    {
        this.userName = userName;
        this.appName = appName; 
        this.refreshToken = refreshToken;
        this.timeCreated = timeCreated;
        this.tokenScopeHash = tokenScopeHash; 
        this.tokenState = tokenState;
        this.validityPeriod = validityPeriod;
    }
    
    // Getters only
    
}

检查此内容以获取详细说明: https://vladmihalcea.com/the-best-way-to-map-a-projection-query-to-a-dto-with-jpa-and-hibernate/

Check this for detailed explanation: https://vladmihalcea.com/the-best-way-to-map-a-projection-query-to-a-dto-with-jpa-and-hibernate/

这篇关于Springboot自定义选择查询返回没有找到能够从类型转换的转换器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆