如何使用JPA本机查询读取Clob数据?以及jpa接口和服务类应该如何?

时间:2018-11-24 05:01:44

标签: spring spring-boot spring-data-jpa

public interface StudentRepo extends JpaRepository<Student, Integer> {

    @Query(value = "SELECT Name FROM STUDENT WHERE UNIQID=?", nativeQuery = true)
     public abstract _______ getStudentByUniqueKey(String uniqueKey);
}

这是什么返回类型?

3 个答案:

答案 0 :(得分:1)

答案是 String ,您还需要考虑在您的Student实体中为CLOB放置以下注释:

   @Column(name="name", columnDefinition="CLOB NOT NULL") 
   @Lob 
   private String name;

更新:

columnDefinition =“ ...”的值取决于您的数据库。例如。对于PostreSQL,它必须为columnDefinition="text"而不是columnDefinition="clob"

答案 1 :(得分:0)

您的StudentRep必须如下所示:

public interface StudentRepo extends JpaRepository<Student, Integer> {

    @Query(value = "SELECT name FROM Student WHERE uniqid=:uniqid", nativeQuery = true)
     public String getStudentNameByUniqueKey(@Param("uniqid")String uniqueKey);
}

如果要获取Student对象:

 @Query(value = "SELECT student FROM Student student WHERE student.uniqid=:uniqid", nativeQuery = true)
  public Student findStudentByUniqueKey(@Param("uniqid")String uniqueKey);

有关更多详细信息,您可以检查official doc

答案 2 :(得分:0)

我遇到了同样的问题,正在寻找解决方案。对于Oracle DB(在12c版本中有效),我们可以使用内置函数将Clob转换为String。这是特定于该问题的代码

public interface StudentRepo extends JpaRepository<Student, Integer> {

@Query(value = "SELECT dbms_lob.substr(Name) FROM STUDENT WHERE UNIQID=?", nativeQuery = true)
 public abstract String getStudentByUniqueKey(String uniqueKey);  
}