Crea cerniere personalizzato con repository di primavera
-
21-12-2019 - |
Domanda
Sto usando il roo di primavera con strati del repository.Ho due classi: Documento (con il titolo del documento) e Scornari (che contiene il documento correlato).In questo modo:
public class Document {
/**
*/
@NotNull
private String titleDocument;
...
}
public class FilledDocument {
/**
*/
@OneToOne
private Document relatedDocument;
...
}
.
vorrei creare un cercatore per il flocco di classe che corrisponda al titolo all'interno del programma relativo a Atribute.
Sto usando una soluzione che non è elegante ed efficiente.Nel mio controller ho:
@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";
}
.
Sto leggendo questo DATA DATA SPRING JPA - Documentazione di riferimento e sto provando con @query nella classe del repository, come questa:
@Query("select f from FilledDocument f where f.relatedDocument.titleDocument containing = ?1")
public ArrayList<FilledDocument> findFilledDocumentByTitleDocument(@Param("titleDocument") String titleDocument);
.
Ma non funziona.Qualche idea?
Soluzione
Se si utilizza DATA SPRING JPA 1.3.1 o più recente che puoi semplicemente scrivere:
@Query("select f from FilledDocument f where f.relatedDocument.titleDocument like %:title%")
List<FilledDocument> findFilledDocumentByTitleDocument(@Param("title") String titleDocument);
.
Se sei bloccato su una versione precedente, il tuo codice sarebbe:
@Query("select f from FilledDocument f where f.relatedDocument.titleDocument like :title")
List<FilledDocument> findFilledDocumentByTitleDocument(@Param("title") String titleDocument);
.
E quando chiami il metodo findFilledDocumentByTitleDocument
devi passare
"%"+title+"%"
come argomento.
Check out Questo Blog Post per la storia completa e la sezione 1.3.4
del Documentazione .