Spring Repository를 사용하여 사용자 정의 찾기를 만듭니다
-
21-12-2019 - |
문제
저장소 층으로 Spring Roo를 사용하고 있습니다.나는 두 개의 클래스가 있습니다 : 문서 (문서 제목 포함) 및 gluewDocument (관련 문서가 포함 된).다음과 같이 :
public class Document {
/**
*/
@NotNull
private String titleDocument;
...
}
public class FilledDocument {
/**
*/
@OneToOne
private Document relatedDocument;
...
}
.
Atribute retenaDDocument 내부의 제목과 일치하는 클래스 glookdocument에 대한 Finder를 만들고 싶습니다.
나는 우아하고 효율적이고 효율적인 솔루션을 사용하고 있습니다.내 컨트롤러에서는 다음과 같습니다.
@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+"%"
인수로.
제휴하지 않습니다 StackOverflow