0

my class is:

@AllArgsConstructor
@NoArgsConstructor
public class RoleData {

    @Column(name = "role_id")
    private String roleId;
    
    @Column(name = "use_mail_send")
    private Boolean useMailSend;

    @Column(name = "mail_send")
    private Boolean mailSend;

    @Column(name = "use_output")
    private Boolean useOutput;

    @Column(name = "possible_to_output")
    private Boolean possibleToOutput;

    @Column(name = "use_import")
    private Boolean useImport;

    @Column(name = "possible_to_import")
    private Boolean possibleToImport;
}

my JPA native query is:

@Query(value = "select cast(r.role_id as text) as role_id, " +
            "       fm.use_mail_send, " +
            "       rd.mail_send, " +
            "       fm.use_output, " +
            "       rd.possible_to_output, " +
            "       fm.use_import, " +
            "       rd.possible_to_import " +
            "from role_detail rd " +
            "         join role r on rd.role_id = r.role_id " +
            "         join feature_mst fm on fm.company_code = rd.company_code and fm.feature_code = rd.feature_code and " +
            "                                fm.feature_category = r.feature_category " +
            "where rd.role_id = :roleId", nativeQuery = true)
    List<RoleData> getAllByRoleId(@Param("roleId") UUID roleId);

I see it can fetch data, but throw exception when convert it to my RoleData class:

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.ecometrue.common.dto.responses.RoleData]
    at org.springframework.core.convert.support.GenericConversionService.handleConverterNotFound(GenericConversionService.java:322)
    at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:195)
    at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:175)
    at org.springframework.data.repository.query.ResultProcessor$ProjectingConverter.convert(ResultProcessor.java:297)
    at org.springframework.data.repository.query.ResultProcessor$ChainingConverter.lambda$and$0(ResultProcessor.java:217)
    at org.springframework.data.repository.query.ResultProcessor$ChainingConverter.convert(ResultProcessor.java:228)
    at org.springframework.data.repository.query.ResultProcessor.processResult(ResultProcessor.java:156)
    at org.springframework.data.jpa.repository.query.AbstractJpaQuery.doExecute(AbstractJpaQuery.java:157)
    at org.springframework.data.jpa.repository.query.AbstractJpaQuery.execute(AbstractJpaQuery.java:142)
    at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor$QueryMethodInvoker.invoke(QueryExecutorMethodInterceptor.java:195)
    at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.doInvoke(QueryExecutorMethodInterceptor.java:152)
    at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.invoke(QueryExecutorMethodInterceptor.java:130)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
    at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:80)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:367)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:118)

I have google multiple resource, and see this bug report: https://github.com/spring-projects/spring-data-jpa/issues/1349?focusedCommentId=133359&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel

I found a solution, replace RoleData to Object in repository:

List<Object[]> getAllByRoleId(@Param("roleId") UUID roleId);

But i want to parse data to DTO object.

Long Do
  • 33
  • 5
  • 1
    Should not you give you class @Entiry annotation? – Igor Kanshyn Mar 18 '21 at 02:57
  • @IgorKanshyn I have try it, but it dont work: ``` org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.Object[]] to type [com.ecometrue.common.dto.responses.RoleData] for value '{e3dd3d87-4929-4144-80f4-4472d6c9e876, false, false, true, true, true, true}'; nested exception is org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.lang.String] to type [com.ecometrue.common.dto.responses.RoleData] ``` – Long Do Mar 18 '21 at 03:04
  • Why are you writing (1) queries (2) native queries at all? Simply name your method `findByRoleId` and Spring Data will create the query for you. – chrylis -cautiouslyoptimistic- Mar 18 '21 at 03:07
  • @chrylis-cautiouslyoptimistic- I'm using native query and get this problem. I want to know why and resolve it in native query. please help. – Long Do Mar 18 '21 at 03:10
  • Does this answer your question? [How to map sql native query result into DTO in spring jpa repository?](https://stackoverflow.com/questions/64762080/how-to-map-sql-native-query-result-into-dto-in-spring-jpa-repository) – Nikolai Shevchenko Mar 18 '21 at 09:17
  • I have a better solution then ussing List in repo is using: List> – Long Do Apr 09 '21 at 16:06
  • But it must convert to class manually – Long Do Apr 09 '21 at 16:07

2 Answers2

0

Annotations are missing. First of all you need @Entity annotation. Also you need to have a primary key defined in your table as each JPA entity must have a primary key which uniquely identifies it

 @Id
 @GeneratedValue(strategy=GenerationType.AUTO)
 private Long id;
aang13
  • 389
  • 1
  • 3
  • 14
0

You are missing getters and setters. Add @Data annotation on top of your class.

Update-Extra: If you want to save data via that entity in pg, you need to create a sequence for that table. Also, add your primary key identifier as aang13 mentioned. Otherwise, without sequence, your primary key will not be incremented like MySQL db.

user404
  • 1,934
  • 1
  • 16
  • 32