Crudrepository How to find by last date between?
You're almost there. The slight tweak would be this if I read the documentation correctly:
Data data = dataRepository.findTopByUpdatedBetweenOrderByUpdatedDesc(start, stop);
Source: https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.limit-query-result
Get last records ordered by date on Spring Data
Turns out that the signature of the method was incorrect. The right one is:
findFirst5ByOrderByPublicationDateDesc()
Is a little confusing because in the official samples they have this:
List<User> findTop10ByLastname(String lastname, Pageable pageable);
As you can see there is only one By there, the usual one.
Get records for last 3 days via Spring JPA Repository
I suggest to split the logic from the actual queries. A service could handle all the intermediate things, e.g.:
@Service
public class MessageService {
private final MessageRepository repository;
@Autowired
public MessageService(MessageRepository repository) {
this.repository = repository;
}
List<Message> getLastThreeDays() {
// subtract 3 days from today
LocalDate threeDaysAgoDate = LocalDate.now().minusDays(3);
return this.repository.findAllWithDateAfter(threeDaysAgoDate);
}
}
and your repository stays nice and clean:
@Repository
public interface MessageRepository extends JpaRepository<Message, Long> {
Optional<Message> findByMessageId(Long id);
@Query("select m from Message m where date >= :threeDaysAgoDate")
List<Message> findAllWithDateAfter(@Param("threeDaysAgoDate") LocalDate threeDaysAgoDate);
}
How to combine multiple date-between searches with CrudRepository of Spring Data JPA?
The Between
keyword naturally binds two parameters. Thus after binding the from clause, the parameter list is exhausted and we don't know which parameters to use for the second criteria.
A manually defined query should do the trick:
interface BookRepository extends Repository<Book, Integer> {
@Query("select b from Book b " +
"where b.from between ?1 and ?2 and b.to between ?1 and ?2")
List<Book> findByDatesBetween(Date departure, Date arrival);
}
findby date between 2 dates
Considering campStartDate will always be less than campEndDate, and if you are trying to find records for a date which you pass a date which is falling in between, than I think you got your comparison wrong, you should change condition as below
public interface Bulk_repositoryRepository extends CrudRepository<Bulk_repository, Long> {
List<Bulk_repository>findAllByStatusAndCampTypeAndCampStartDateGreaterThanEqualAndCampEndDateLessThanEqual(int status, int campType,Date campStartDate,Date campEndDate);
Bulk_repository findById(long id);
}
Edit:
If you don't want to pass date then JPQL can help you, try changing method signature in repository something like
public interface Bulk_repositoryRepository extends CrudRepository<Bulk_repository, Long> {
@Query("SELECT b FROM Bulk_repository b WHERE b.status = :status and b.campStartDate >=CURRENT_DATE and b.campEndDate <= CURRENT_DATE)
List<Bulk_repository>findAllByStatusAndCampTypeAndCampStartDateGreaterThanEqualAndCampEndDateLessThanEqual(@Param("status") int status);
Bulk_repository findById(long id);
}
Related Topics
How to Mock a Generic Parameter for a Unit Test in Java
Selecting from Div Class Dropdown - Selenium
How to Change Read-Only Status of Files in Intellij
How to Get Values of All Elements from Xml String in Java
Mapstruct: Map List of Objects, When Object Is Mapped from Two Objects
How to Get Multiple Columns from Table Using Jpa
How to Update Thousands of Records into MySQL Db in Milliseconds
How to Valid @Requestheader in Spring Boot
Update Objects in One List Based on Values from Second One Using Streams
Requestmethod Post Not Supported || Spring Login
Java 8: How to Write Lambda Stream to Work With Jsonarray
Calling Method That Exists in Child Classes But Not in Parent Class
Spring Boot/Spring Kafka Ssl Configuration by Environment Variables Impossible
Spring Boot Could Not Resolve Placeholder in String
How to Check Type of Variable in Java
How to Update Each Element in a List in Java 8 Using Stream API