我正在将 Spring Roo 与存储库层一起使用。我有两节课:Document(带有文档标题)和 FilledDocument(包含相关文档)。像这样:

public class Document {

    /**
     */
    @NotNull
    private String titleDocument;
    ...
}

public class FilledDocument {

    /**
     */
    @OneToOne
    private Document relatedDocument;
    ...
}

我想为 FilledDocument 类创建一个与属性 relatedDocument 内的标题匹配的查找器。

我正在使用一个不优雅和高效的解决方案。在我的控制器中我有:

@RequestMapping(params = "find", produces = "text/html")
public String findFilledDocumentsByTitleDocumentContaining(@RequestParam(value = "title", required = true) String title, Model uiModel) {

    LinkedList<FilledDocument> allFilledDocuments = new LinkedList<FilledDocument>();
    allFilledDocuments.addAll(filledDocumentService.findAllFilledDocuments());

    ArrayList<FilledDocument> filledDocuments=new ArrayList<FilledDocument>();

    for( FilledDocument filledDocument : allFilledDocuments ) {
        if( filledDocument.getRelatedDocument().getTitleDocument().toLowerCase().contains(title.toLowerCase()) == true ) {
            filledDocuments.add(filledDocument);
        }
    }       

    uiModel.addAttribute("filleddocuments", filledDocuments);
    return "filleddocuments/list";
}

我正在读这个 Spring Data JPA - 参考文档 我正在尝试在存储库类中使用 @Query,如下所示:

@Query("select f from FilledDocument f where f.relatedDocument.titleDocument containing = ?1")
public ArrayList<FilledDocument> findFilledDocumentByTitleDocument(@Param("titleDocument") String titleDocument);

但它不起作用。有任何想法吗?

有帮助吗?

解决方案

如果您使用 Spring Data JPA 1.3.1 或更高版本,您可以编写:

@Query("select f from FilledDocument f where f.relatedDocument.titleDocument like %:title%")
List<FilledDocument> findFilledDocumentByTitleDocument(@Param("title") String titleDocument);

如果您坚持使用旧版本,那么您的代码将是:

@Query("select f from FilledDocument f where f.relatedDocument.titleDocument like :title")
List<FilledDocument> findFilledDocumentByTitleDocument(@Param("title") String titleDocument);

当你打电话给 findFilledDocumentByTitleDocument 你需要通过的方法

"%"+title+"%"

作为一个论点。

查看 完整故事和部分的博客文章 1.3.4文档.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top