![](/img/trans.png)
[英]Error 'No converter found capable of converting from type XXX' when using @Query("Select * from")
[英]Springboot custom Select Query returns No converter found capable of converting from type
我正在嘗試在 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
似乎不是托管實體。 對於這種情況,您必須使用投影(dto mapping)。 看看下面的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/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.