Springboot自定义选择查询返回“找不到转换器,无法从类型进行转换”

huangapple go评论114阅读模式
英文:

Springboot custom Select Query returns No converter found capable of converting from type

问题

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

  1. public interface idnOauth2AccessTokenRepository extends JpaRepository<idnOauth2AccessToken, String>,
  2. JpaSpecificationExecutor<idnOauth2AccessToken> {
  3. @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'")
  4. List<userApplicationModel> getUserApplicationModel();
  5. }

但是当我执行时,我收到一个错误消息:

  1. 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 am trying to execute a custom select query in Springboot JPA,

  1. public interface idnOauth2AccessTokenRepository extends JpaRepository&lt;idnOauth2AccessToken, String&gt;,
  2. JpaSpecificationExecutor&lt;idnOauth2AccessToken&gt; {
  3. @Query(value = &quot;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=&#39;ACTIVE&#39;&quot;)
  4. List&lt;userApplicationModel&gt; getUserApplicationModel();
  5. }

But when I execute I get an error of

  1. 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]

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.

答案1

得分: 2

你正在将来自不同表的列进行连接,然后赋值给不同的对象。这样是行不通的 + userApplicationModel 似乎不是托管实体。对于这种情况,你需要使用投影(DTO 映射)。请看以下 Query

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

以及要映射到的类:

  1. public class UserApplicationModelProjection {
  2. private String userName;
  3. private String appName;
  4. private String refreshToken
  5. private OffsetDateTime timeCreated
  6. private String tokenScopeHash;
  7. private String tokenState; //注意数据类型
  8. private int validityPeriod; //更新数据类型
  9. public UserApplicationModelProjection(String userName,
  10. String appName,
  11. String refreshToken,
  12. OffsetDateTime timeCreated,
  13. String tokenScopeHash,
  14. String tokenState,
  15. int validityPeriod)
  16. {
  17. this.userName = userName;
  18. this.appName = appName;
  19. this.refreshToken = refreshToken;
  20. this.timeCreated = timeCreated;
  21. this.tokenScopeHash = tokenScopeHash;
  22. this.tokenState = tokenState;
  23. this.validityPeriod = validityPeriod;
  24. }
  25. // 仅包含 Getter 方法
  26. }

详细说明请查看此链接:https://vladmihalcea.com/the-best-way-to-map-a-projection-query-to-a-dto-with-jpa-and-hibernate/

英文:

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:

  1. @Query(value = &quot;select new your.package.UserApplicationModelProjection(IOCA.userName, IOCA.appName, IOAT.refreshToken, IOAT.timeCreated, IOAT.tokenScopeHash, IOAT.tokenState, IOAT.validityPeriod)&quot;
  2. + &quot; from idnOauth2AccessToken IOAT inner join idnOauthConsumerApps IOCA on IOCA.ID = IOAT.consumerKeyID where IOAT.tokenState=&#39;ACTIVE&#39;&quot;)
  3. List&lt;UserApplicationModelProjection&gt; getUserApplicationModel();

And the class to map to:

  1. public class UserApplicationModelProjection {
  2. private String userName;
  3. private String appName;
  4. private String refreshToken
  5. private OffsetDateTime timeCreated
  6. private String tokenScopeHash;
  7. private String tokenState; //mind the data type
  8. private int validityPeriod; //update the data type
  9. public UserApplicationModelProjection(String userName,
  10. String appName,
  11. String refreshToken,
  12. OffsetDateTime timeCreated,
  13. String tokenScopeHash,
  14. String tokenState,
  15. int validityPeriod)
  16. {
  17. this.userName = userName;
  18. this.appName = appName;
  19. this.refreshToken = refreshToken;
  20. this.timeCreated = timeCreated;
  21. this.tokenScopeHash = tokenScopeHash;
  22. this.tokenState = tokenState;
  23. this.validityPeriod = validityPeriod;
  24. }
  25. // Getters only
  26. }

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

huangapple
  • 本文由 发表于 2020年10月27日 17:04:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/64551110.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定