Reputation:
Good day,
How do I have to change my code that the 2 conditions work? I already tried a few examples how I could change my query but none of them worked.
Additionally I added my code:
Repository:
@Query("From PERSON where exdate > current_date - to_char(30) and insnr like :insnr%")
Stream<PERSON> findAllWithSearchParams(@Param("insnr") String insnr);
Controller:
@Autowired
private PersonService personService;
@RequestMapping(value="/person/list/**")
public List<Person> loadPersonList(
@RequestParam(value = "insnr" ,required=false) String insnr) throws ParseException {
mysearch.setInsnr(insnr);
return personService.all(mysearch).collect(Collectors.toList());
}
Service:
@Service
public class PersonService {
@Autowired
private PersonRepository personRepository;
public Stream<Person> all(Person mysearch){
return personRepository
.findAll(Example.of(mysearch))
.stream()
.map(Person::fromPerson);
}
}
Class Person:
public class Person {
public Integer index;
public String firstname;
public String lastname;
@JsonFormat(pattern="dd.MM.yyyy")
public Date exdate;
public String insnr;
private Person(Integer index, String firstname, String lastname, Date exdate, String insnr){
this.index=index;
this.firstname=firstname;
this.lastname=lastname;
this.exdate=exdate;
this.insnr=insnr;
}
public static Person fromPerson(Person person){
return person == null ? null : new Person(person.getIndex(), person.getFirstname(), person.getLastname(), person.getExdate(), person.getInsnr());
}
}
Upvotes: 0
Views: 90
Reputation: 271
I think it would be much easier if you use Keyword Like and After in your repository method for example
List<Person> findByExdateAfterAndInsnrLike(Date date,String
like);
this may help https://stackoverflow.com/a/45692717/2494123 and Supported keywords inside method names Keyword Sample
Upvotes: 0
Reputation: 3213
I used the like condition in this way:
@Query("select ....... where codition and date like CONCAT('%','-',:m,'-',:d)")
So in your case should be:
@Query("From PERSON where exdate > current_date - to_char(30) and insnr like CONCAT(:insnr,'%'))
Upvotes: 1
Reputation: 377
You can change your repository with
List<Person> findByExdateAfterAndInsnrStartingWith(Date exdate, String insnr);
And you can call that method with the date you want and with "1" as insnr
Upvotes: 1