Spring JPA native query to call store procedrure gives “No converter found capable of converting from type”

一个人想着一个人 提交于 2020-01-06 05:38:08

问题


I'm using Spring JPA and I need to have a native query to call stored procedure. From the result, I need to get only two fields i.e. code and msg. I made a class which contain two field code and msg. It isn't working, this is the error I'm getting:

Failed to complete request: 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.evampsaanga.sez.model.dto.NtnVerification]

So, here's my code:

 public interface CacheOtpRepository extends JpaRepository<CacheOtp, Long> {

@Query(value = "{call verify_ntn_opt_prc(:ntnNumber, :otpCode)}", nativeQuery = true)
            NtnVerification verifyNtnByOtpStoredProcedure(@Param("ntnNumber") String ntnNumber, @Param("otpCode") String otpCode);
    }

Here is my dto class

public class NtnVerification {
    private int code;
    private String msg;

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }
}


回答1:


try using spring data JPA interface projection

public interface NtnVerification {
  public int getCode;();
  public String getMsg();
}



回答2:


You are returning NtnVerification from a JpaRepository that takes care of the persistance of CacheOtp. You can either use Projections as mentioned by @ashish or call the procedure the Spring data jpa way.

The Spring data jpa way to call a Stored Procudure is to use @NamedStoredProcedureQuery on your entity class and then use @Procedure in your JPA repositories.

Your Entity

@NamedStoredProcedureQueries({
@NamedStoredProcedureQuery(
    name = "YOUR_PROCEDURE_NAME",
    procedureName = "PROCEDURE_NAME_IN_THE_DATABASE",
    parameters = {
          ...
          ...
    }
)})    
@Entity
public class NtnVerification {
    ...
    ...
}

Your Repository

@Transactional
public interface NtnVerificationRepository extends CrudRepository<NtnVerification , Long> {

    ...    

    @Procedure(name = YOUR_PROCEDURE_NAME)
    List<NtnVerification> getNtnVerifications();

}

You can refer to this tutorial for a more detailed explanation.



来源:https://stackoverflow.com/questions/59477716/spring-jpa-native-query-to-call-store-procedrure-gives-no-converter-found-capab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!