问题
I am trying to execute a custom select query in 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();
}
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:
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 = "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();
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/
来源:https://stackoverflow.com/questions/64551110/springboot-custom-select-query-returns-no-converter-found-capable-of-converting