I'm trying to make a little spring boot service that receives a parameter and then the query will apply a like to each word in the sentence. I want to know why this works:
@GetMapping("/getAll")
//@CrossOrigin(origins = "http://localhost:4200")
private List<PersonaNegativa> getAll(){
List<PersonaNegativa> listneg = new ArrayList<PersonaNegativa>();
try {
listneg = entityManager
.createQuery("select p from PersonaNegativa p where p.NombreCompletoFonetico like '%ARKRP%' "
+ "or p.NombreCompletoFonetico like '%ARLNS%'",
PersonaNegativa.class).getResultList();
} catch (Exception e) {
System.out.println(e.getMessage());
}
return listneg;
}
But this doesn't.
@GetMapping("/getNombre/{nom}")
private List<PersonaNegativa> getByNombre(@PathVariable String nom){
DoubleMetaphone dm = new DoubleMetaphone();
dm.setMaxCodeLen(5);
String[] nomarray = nom.split(" ");
String nomconvertido = "";
for(int i = 0; i<=nomarray.length-1; i++) {
if(i< nomarray.length-1) {
nomconvertido += "%" + dm.doubleMetaphone(nomarray[i])+ "% or p.NombreCompletoFonetico like ";
}
else {
nomconvertido += "%" +dm.doubleMetaphone(nomarray[i]) + "%";
}
}
System.out.println(nomconvertido);
List<PersonaNegativa> listneg = new ArrayList<PersonaNegativa>();
try {
listneg = entityManager
.createQuery("select p from PersonaNegativa p where p.NombreCompletoFonetico like :nom",
PersonaNegativa.class)
.setParameter("nom", nomconvertido).getResultList();
} catch (Exception e) {
System.out.println(e.getMessage());
}
return listneg;
}
}
when printing 'nomconvertido' I get this: "%ARKRP% or p.NombreCompletoFonetico like %ARLNS%"
which should give the same result as the test above, so i'm wondering why is it different.