2

There is an entity with CLOB property:

@Entity
public class Person {

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  private long id;

  private String firstName;
  private String lastName;

  @Column(columnDefinition = "clob")
  @Lob
  private String description;

  //getters and setters here
}

Now I would like to have a method that can return only part of this class, so here is a projection class:

public interface PersonProjection {
  String getLastName();
  String getDescription();
}

and the repository:

@Repository
public interface PersonRepository extends PagingAndSortingRepository<Person, Long> {

  @Query(value = "select pe.last_name as lastName, pe.description from person pe where pe.last_name = :lastName", nativeQuery = true)
  List<PersonProjection> findAllByLastName(@Param("lastName") String lastName);
}

Using native query in this example is not needed but in my case I would like to call an Oracle's function in the where clause (JSON_TEXTCONTAINS to be specific).

Now using this repository in the controller causes:

Could not write JSON: Projection type must be an interface!; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Projection type must be an interface! (through reference chain: java.util.ArrayList[0]->com.sun.proxy.$Proxy103[\"description\"])

Exception seems to be caused because query returns description field as an instance of java.sql.Clob interface, but in projection class this field is defined as String. Exception is not thrown when entity class is used as a return type from repository method (and there description is also defined as a String).

Complete project to check this problem can be found here.

Piotr Chowaniec
  • 1,160
  • 12
  • 25
  • Did you find a solution to this problem? I've run into the same use case and am stuck. – Stensig Sep 27 '19 at 13:29
  • 1
    @AndreasSJ No, I did not. My workaround is to use entity class in the repository find... method as a result and then manually convert entity to required projection class. – Piotr Chowaniec Sep 29 '19 at 21:34
  • A nice fix that worked for me on PostgreSQL (but I think would on Oracle too): https://stackoverflow.com/questions/15377158/how-to-show-clob-type-in-a-select-in-sql-server – Mohamed El-Beltagy Jun 27 '22 at 05:06

1 Answers1

0

you can parse clob to varchar in sql. Ex: dbms_lob.substr( pe.description, 4000, 1 ) as description