javaspring-datalob

spring data native query interesting bug with Lob column


I have an entity:

@Entity public class KnowledgeBase {

    private Long id;
    private String link;
    private String content;

    @Id
    @SequenceGenerator(name = "knowledgebase_id_generator", sequenceName = "knowledgebase_id_sequence", allocationSize = 1)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "knowledgebase_id_generator")
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getLink() {
        return link;
    }

    public void setLink(String link) {
        this.link = link;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }
}

And I have a spring data repository

@Repository public interface KnowledgeBaseRepository
        extends AbstractRepository<KnowledgeBase, Long> {

    @Query(value = "SELECT c.id as id,c.link as link, c.content as content"
            + " from knowledgebase c where content=?1", nativeQuery = true)
    List<KnowledgeBase> findRelevantRecords(String searchString);
}

Please note that

where content=?1

is just a sample, where clause was different for testing.

The issue is if I run this repository method, everything just fine, but content column contains large text amount, I want it to be lazy loaded. If I do that I get error that value is wrong for Long: ''. So my entity is:

@Lob @Basic(fetch = LAZY) String content;

If I remove this, everything just fine. How to prevent content column from being loaded every time and have spring data repository search properly?


Solution

  • try this: Create a constructor in your entity which accepts only the required fields

    public class KnowledgeBase{
    
    //default constructor
    public KnowledgeBase(){}
    
    public KnowledgeBase(Long id,String link){
    this.id=id;
    this.link=link;
    }
    
    }
    

    and use this constructor signature in your query in your repository

     @Query(value = "SELECT new #{#entityName} (c.id as id,c.link as link) from #{#entityName} c "
                + " from knowledgebase c where content=?1", nativeQuery = true)
        List<KnowledgeBase> findRelevantRecordsWithoutContent(String searchString);