I'm trying to retrieve Today's records from mysql database.
I tried using springboot data-jpa and the findByDate(Date date, Pageable pageable) method from jpa-repository and it always return empty results.
Here's the created Entity code :
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="id_synop")
private Long id;
@Column(name="contenu")
private String contenu;
@Column(name="date", columnDefinition="TIMESTAMP(0)")
@Temporal(TemporalType.TIMESTAMP)
private Date date;
@ManyToOne(cascade= {CascadeType.DETACH,CascadeType.MERGE,CascadeType.PERSIST,CascadeType.REFRESH},
fetch=FetchType.LAZY)
@JoinColumn(name="id_station")
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
private Station station;
and here's the Mysql table created with Mysql WorkBench :
CREATE TABLE `synop` (
`id_synop` bigint(20) NOT NULL AUTO_INCREMENT,
`id_station` bigint(20) NOT NULL,
`contenu` varchar(255) NOT NULL,
`date` timestamp NOT NULL,
PRIMARY KEY (`id_synop`),
UNIQUE KEY `idmsgsynop_UNIQUE` (`id_synop`),
KEY `fk_synope_station_idx` (`id_station`),
CONSTRAINT `fk_synope_station` FOREIGN KEY (`id_station`) REFERENCES `station` (`id_station`)
) ENGINE=InnoDB AUTO_INCREMENT=25 DEFAULT CHARSET=utf8;
The jpa repository Code :
import java.util.Date;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
public interface SynopRepository extends JpaRepository<Synop, Long> {
public Page<Synop> findByStationId(Long stationId, Pageable pageable);
public Page<Synop> findByDate(Date date, Pageable pageable);
}
and the controller code :
@GetMapping("/synops-today")
public Page<Synop> findToday(@RequestParam(defaultValue="1") int page,Pageable pageable) {
return synopRepository.findByDate(new Date(),PageRequest.of(page-1, 10, Sort.by("date").ascending()));
}
I expect to get today's records by using the findByDate(...) method, but it doesn't seem to work. I also noticed a little problem when viewing data from MysqlWorkbench : Dates seem to be 2h in advance (ex : now it's 11:57 but in mysql workbench software it shows 9:57, when i query the server time it shows the irght tme though...) I actually don't care about the hours/mins/secs i just want to retrieve today's records.