英文:
Spring Data Jpa query by method name do not work correctly with BETWEEN for LocalDate
问题
我有两个表格:“DailyStatistic”与“Country”的ManyToOne关系。
public class DailyStatistic {
@Id
@GeneratedValue
private Long id;
@Column(columnDefinition = "DATE")
private LocalDate date;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "country_id")
private Country country;
///
}
public class Country {
@Id
@GeneratedValue
private Long id;
private String name;
}
当我调用:
public interface DailyStatRepository extends JpaRepository<DailyStatistic, Long> {
...
List<DailyStatistic> findAllByCountryAndDateBetween(Country country, LocalDate from, LocalDate to);
...
}
我得到了奇怪的结果:
2020-03-29
2020-03-28
2020-03-27
2020-03-30
2020-03-29
Hibernate为对象设置了错误的日期,我得到了两个具有相同日期的不同对象。
请帮助解决这个问题。
调用这个方法的代码段:
public class DataProvider {
private final ForeignDataSource foreignDataSource;
private final DailyStatRepository repository;
private final CountryRepository countryRepository;
@Autowired
public DataProvider(ForeignDataSource foreignDataSource,
DailyStatRepository repository,
CountryRepository countryRepository) {
this.foreignDataSource = foreignDataSource;
this.repository = repository;
this.countryRepository = countryRepository;
}
public List<DailyStatistic> getCountryStatFromToDate(Long countryId,
LocalDate from,
LocalDate to) throws NoDataException {
Country country = countryRepository.findById(countryId).orElseThrow(NoDataException::new);
List<DailyStatistic> dailyStatisticList = repository.findAllByCountryAndDateBetween(country, from, to);
for (DailyStatistic ds : dailyStatisticList) {
System.out.println(ds.getDate());
}
}
}
英文:
I have two tables: "DailyStatistic" with ManyToOne to "Country".
public class DailyStatistic {
@Id
@GeneratedValue
private Long id;
@Column(columnDefinition = "DATE")
private LocalDate date;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "country_id")
private Country country;
///
public class Country {
@Id
@GeneratedValue
private Long id;
private String name;
When I call:
public interface DailyStatRepository extends JpaRepository<DailyStatistic, Long> {
...
List<DailyStatistic> findAllByCountryAndDateBetween(Country country, LocalDate from, LocalDate to);
...
}
I get strange result:
2020-03-29
2020-03-28
2020-03-27
2020-03-30
2020-03-29
Hibernate sets to object a wrong date and I get two different objects with the same date.
Please, help to solve the problem.
The code where I call this method:
public class DataProvider {
private final ForeignDataSource foreignDataSource;
private final DailyStatRepository repository;
private final CountryRepository countryRepository;
@Autowired
public DataProvider(ForeignDataSource foreignDataSource,
DailyStatRepository repository,
CountryRepository countryRepository) {
this.foreignDataSource = foreignDataSource;
this.repository = repository;
this.countryRepository = countryRepository;
}
public List<DailyStatistic> getCountryStatFromToDate(Long countryId,
LocalDate from,
LocalDate to) throws NoDataException {
Country country = countryRepository.findById(countryId).orElseThrow(NoDataException::new);
List<DailyStatistic> dailyStatisticList = repository.findAllByCountryAndDateBetween(country, from, to);
for (DailyStatistic ds : dailyStatisticList) {
System.out.println(ds.getDate());
}
答案1
得分: 0
你可以尝试使用以下 JPA 方法命名来根据国家名称和日期筛选 DailyStatistic
:
repository.findAllByCountryNameAndDateBetweenOrderByDateDesc(country.name, from, to);
英文:
You can try this jpa method naming for DailyStatistic
filter by country name and date
repository.findAllByCountryNameAndDateBetweenOrderByDateDesc(country.name,from, to);
答案2
得分: 0
我发现这是因为应用程序端和数据库端有不同的时区,加上4月29日发生的夏令时变更。所以如果有人遇到类似的问题,请首先检查时区差异。
英文:
How I figure out it happens because of a different timezone on app side and db side + daylight saving time that happened on April 29. So if someone get similar issue - check a timizone difference at first.
专注分享java语言的经验与见解,让所有开发者获益!
评论