gpt4 book ai didi

java - Spring Data Native 查询 Lob 列的有趣错误

转载 作者:行者123 更新时间:2023-12-01 09:17:24 24 4
gpt4 key购买 nike

我有一个实体:

@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数据存储库正确搜索?

最佳答案

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

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

关于java - Spring Data Native 查询 Lob 列的有趣错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40444808/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com