2016-11-06 53 views
0

我有一个实体:春天数据本地查询有趣的错误与LOB列

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

而且我有一个春天的数据仓库

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

如果我删除这个,一切都很好。 如何防止每次加载内容列并正确地搜索弹簧数据存储库?

+0

我很困惑在查询中使用此构造函数签名。哪个版本导致异常?也请包括实际的例外。 –

回答

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