【问题标题】:spring data native query interesting bug with Lob column带有 Lob 列的 spring 数据本机查询有趣的错误
【发布时间】:2016-11-06 00:33:27
【问题描述】:

我有一个实体:

@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;
    }
}

我有一个 spring 数据存储库

@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);
}

请注意

where content=?1

只是一个示例,where 子句在测试时有所不同。

问题是如果我运行这个存储库方法,一切都很好,但是内容列包含大量文本,我希望它是延迟加载的。如果我这样做,我会收到错误,即 Long:'' 的值是错误的。所以我的实体是:

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

如果我删除它,一切都会好起来的。 如何防止每次都加载内容列并正确进行spring数据存储库搜索?

【问题讨论】:

  • 我很困惑。哪个版本导致异常?还请包括实际的例外情况。

标签: java spring-data lob


【解决方案1】:

试试这个: 在您的实体中创建一个仅接受必填字段的构造函数

public class KnowledgeBase{

//default constructor
public KnowledgeBase(){}

public KnowledgeBase(Long id,String link){
this.id=id;
this.link=link;
}

}

并在存储库中的查询中使用此构造函数签名

 @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);

【讨论】:

    猜你喜欢
    • 2021-09-23
    • 2019-04-15
    • 2019-10-09
    • 2018-12-09
    • 2017-04-21
    • 2020-12-10
    • 2018-09-05
    • 2018-12-05
    • 2021-05-27
    相关资源
    最近更新 更多