当前位置: 首页 > 知识库问答 >
问题:

Springboot自定义选择查询返回没有找到能够从类型

裴硕
2023-03-14
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]

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

共有1个答案

尹光辉
2023-03-14

您正在联接来自不同表的列,然后将其分配给不同的对象。它不是这样工作的,UserApplicationModel似乎不是托管实体。对于这样的场景,您必须使用投影(dto映射)。查看以下查询:

@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/

 类似资料: