【问题标题】:Springboot custom Select Query returns No converter found capable of converting from typeSpringboot自定义选择查询返回没有找到能够从类型转换的转换器
【发布时间】:2021-02-09 12:51:09
【问题描述】:

我正在尝试在 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();
}

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

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 实现一个新的存储库,但没有奏效。

任何可能有帮助的答案或实施。

【问题讨论】:

  • 你能用UserApplicationModel代替userApplicationModel吗?
  • @GaborSzabo 这没有帮助,我什至尝试重命名这个类。
  • 你能检查这里给出的答案stackoverflow.com/questions/61194243/…

标签: java spring hibernate jpa


【解决方案1】:

您正在连接来自不同表的列,然后分配给不同的对象。它不能以这种方式工作 + userApplicationModel 似乎不是托管实体。对于这种情况,您必须使用投影(dto 映射)。看看下面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/

【讨论】:

    猜你喜欢
    • 2018-02-15
    • 1970-01-01
    • 2020-11-19
    • 1970-01-01
    • 2019-10-20
    • 2019-02-10
    • 2021-09-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多