How to use two date parameters for two Betweens without repeating the parameters using Spring Boots?

1

I'm starting with Spring Boot and I'm trying to use it for a query where I want to check if two date columns are between a given period of parameters. The code below works, however for this I have to repeat the parameters (start date and end date for comparison).

@Transactional(readOnly = true)
Collection<MyClass> findByDateBeginBetweenOrStartedWorkBetween (Instant firstDate, Instant lastDate, Instant firstDateAgain, Instant lastDateAgain);

I have not found any similar example so far. I figured it might look something like this:

@Transactional(readOnly = true)
Collection<MyClass> findByDateBeginOrStartedWorkBetween (Instant firstDate, Instant lastDate);

But when I compile the project, I get the error below:

  

Caused by: java.lang.IllegalArgumentException: Failed to create query   for method public abstract java.util.Collection   with ... MyClass.findByDateBeginOrStartedWorkBetween   (java.time.Instant, java.time.Instant)! No parameter available for part   startedWork BETWEEN (2): [IsBetween, Between] NEVER.

Can I (and if so, how can I) use my comparison parameters only once using Spring Boot?

    
asked by anonymous 02.10.2018 / 17:22

1 answer

1

between connects two parameters, which are not reusable after the internal assembly of query by Spring. The error message already gives this tip to you:

  

No parameter available for part startedWork

When the first part of query is built, the two parameters you passed are consumed, so there are no more parameters left for the second part of query , hence the error.

For your problem, the use of @Query must solve (a generic example, apply it to your specific case):

Transactional(readOnly = true)
@Query(select x from MyClass x where x.firstDate between ?1 and ?2 and x.lastDate between ?1 and ?2)
Collection<MyClass> findByDatesBetween(Instant firstDate, Instant lastDate);
    
02.10.2018 / 18:09