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

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

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

问题

我正在尝试在Spring Boot 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实现一个新的存储库,但没有成功。

是否有任何答案或实现可以帮助解决这个问题。

英文:

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

public interface idnOauth2AccessTokenRepository extends JpaRepository&lt;idnOauth2AccessToken, String&gt;,
        JpaSpecificationExecutor&lt;idnOauth2AccessToken&gt; {
    @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;)
    List&lt;userApplicationModel&gt; 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]

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

@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; //注意数据类型
	private int validityPeriod; //更新数据类型
	
	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;
	}
	
	// 仅包含 Getter 方法
	
}

详细说明请查看此链接: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:

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

And the class to map to:

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
	
}

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:

确定