Home > other >  Using projection with a interface in `@Query` annotation with a native query gives "Target type
Using projection with a interface in `@Query` annotation with a native query gives "Target type

Time:10-18

I have a table with a clob in an oracle 19 database which I try to fetch with a native query using a @Query annotation with a projection from a Spring boot 2.7.4 application. I get the following error message:

java.lang.UnsupportedOperationException: Cannot project jdk.proxy2.$Proxy281 implementing java.sql.Clob,org.hibernate.engine.jdbc.WrappedClob,java.io.Serializable to java.lang.String; Target type is not an interface and no matching Converter found

The query from my repository class:


@Query(
       value = """
               select submission_id as "submissionId", text as "textAnswer"
               from answer
               where answer_id = :answerId
               """,
       nativeQuery = true)
public MyDTO findDTO(Long answerId);

My interface which I use for the projection:

public interface MyDTO {
    String getTextAnswer();
}

From my domain object annotated with @Entity:

private String textAnswer;

My testcase which reproduce which reproduce this error. If I comment out the line with a.getTextAnswer() it runs ok.

@Test
public void findFormPublishedAnswersInterfaceDTOById() {
    FormPublishedAnswersInterfaceDTO a = answerRepository.findFormPublishedAnswersInterfaceDTOById(21540241L);
    assertEquals("test", a.getTextAnswer());
}

I have tried different hints I found by the help of google :) like annotating private String textAnswer with @Lob, @Type(type = "text") and @Column(name = "text", columnDefinition = "CLOB") without any luck.

CodePudding user response:

If you read the exception carefully, you should understand that the JDBC driver reports that the type is a Clob, so your DTO must look like this instead:

public interface MyDTO {
    Clob getTextAnswer();
}
  • Related