4 回答

TA貢獻1830條經驗 獲得超3個贊
問題發(fā)生是因為數(shù)據(jù)轉換
com.mysql.cj.jdbc.ClientPreparedStatement:選擇 flight0_.id 作為 id1_0_,flight0_.arrival_city 作為 arrival_2_0_,flight0_.date_of_departure 作為 date_of_3_0_,flight0_.departure_city 作為 departur4_0_,flight0_.estimated_departure_time 作為 estimate5_0_,flight0_.flight_number 作為 flight_n6_ 0_, flight0_.operating_airlines作為 operatin7_0_ 來自航班 flight0_,其中 flight0_.departure_city='AUS' and flight0_.arrival_city='NYC' and flight0_.date_of_departure='2018-02-04 18:30:00.0'
如您所見,日期未轉換為正確的格式,因此我們推出了自己的轉換器來解決此問題。
flightreservationapp/converters/LocalDateAttributeConverter.java
package com.dgs.flightreservationapp.converters;
import javax.persistence.AttributeConverter;
import javax.persistence.Converter;
import java.time.LocalDate;
@Converter(autoApply = true)
public class LocalDateAttributeConverter implements AttributeConverter<LocalDate, String> {
@Override
public String convertToDatabaseColumn(LocalDate locDate) {
return locDate == null ? null : locDate.toString();
}
@Override
public LocalDate convertToEntityAttribute(String sqlDate) {
return sqlDate == null ? null : LocalDate.parse(sqlDate);
}
}
添加此文件后,您將開始獲得結果
您還有其他選擇。為 JPA 方法添加注解
List<Flight> findByDepartureCityAndArrivalCityAndDateOfDeparture(String from, String to,
@DateTimeFormat(iso= DateTimeFormat.ISO.DATE)
LocalDate departureDate);
然后您需要確保將時區(qū)也設置為 UTC
public class FlightReservationAppApplication {
public static void main(String[] args) {
SpringApplication.run(FlightReservationAppApplication.class, args);
}
@PostConstruct
void init() {
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
}
}
另一種選擇是在你的程序中使用更高版本的 hibernatepom.xml
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-java8</artifactId>
<version>5.1.0.Final</version>
</dependency>
并設置時區(qū)
public class FlightReservationAppApplication {
public static void main(String[] args) {
SpringApplication.run(FlightReservationAppApplication.class, args);
}
@PostConstruct
void init() {
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
}
}

TA貢獻1827條經驗 獲得超8個贊
我認為您需要在實體中注釋日期字段@Temporal(TemporalType.DATE)
喜歡
@Entity
public class Flight extends AbstractEntity {
private String flightNumber;
private String operatingAirlines;
private String departureCity;
private String arrivalCity;
@Temporal(TemporalType.DATE)
private Date dateOfDeparture;
private Timestamp estimatedDepartureTime;
}

TA貢獻1155條經驗 獲得超0個贊
在你的非工作存儲庫中,你有這個額外的配置application.properties
:
spring.datasource.url=jdbc:mysql://localhost:3306/reservation?useSSL=false&serverTimezone=UTC
該部分serverTimezone=UTC
導致您輸入的日期和數(shù)據(jù)庫中的日期不匹配=>錯誤的結果
所以現(xiàn)在,要么將兩者設置為相同的時區(qū),要么避免在 Java 中使用 Local*** 類

TA貢獻1798條經驗 獲得超7個贊
MySqlDate類型不包含任何時間部分,因此Date您在實體上使用的類型Flight及其時間部分可能會破壞您的查詢。嘗試Date用LocalDateJava8 Date/Time API 替換,它也只包含沒有任何時間信息的日期。
private LocalDate dateOfDeparture;
&
@Param("dateOfDeparture") LocalDate departureDate
最重要的是,您不需要@Query為這樣一個簡單的邏輯使用 a 。您可以使用方法名稱作為查詢,這是 Spring Data JPA 的一個很好的特性。
public interface FlightRepository extends JpaRepository<Flight, Long> {
List<Flight> findByDepartureCityAndArrivalCityAndDateOfDeparture(String from, String to, LocalDate departureDate);
}
并從您的控制器中調用它;
@PostMapping("/processFlights")
public String processFlights(..) {
List<Flight> flights = flightRepository.findByDepartureCityAndArrivalCityAndDateOfDeparture(from, to, departureDate);
modelMap.addAttribute("flights", flights);
return "displayFlights";
}
雖然它看起來很糟糕,因為名字變得太長了。您可以添加一個默認方法來包裝它以隱藏丑陋的命名。
public interface FlightRepository extends JpaRepository<Flight, Long> {
List<Flight> findByDepartureCityAndArrivalCityAndDateOfDeparture(String from, String to, LocalDate departureDate);
// you can use this from your controller
default List<Flight> findFlights(String from, String to, LocalDate departureDate) {
return findByDepartureCityAndArrivalCityAndDateOfDeparture(from, to, departureDate);
}
}
這樣做將隱藏由于在其中使用@Query注釋和硬編碼 JPQL 而導致的復雜性和可能的錯誤,除非對于更高級的情況絕對必要。
我在你的中添加了以下代碼flightreservationapp;
@SpringBootApplication
public class FlightReservationAppApplication {
public static void main(String[] args) {
ConfigurableApplicationContext run = SpringApplication.run(FlightReservationAppApplication.class, args);
FlightRepository flightRepository = run.getBean(FlightRepository.class);
Flight flight = new Flight();
flight.setDepartureCity("AUS");
flight.setArrivalCity("NYC");
flight.setDateOfDeparture(LocalDate.of(2018, 1, 5));
flightRepository.save(flight);
List<Flight> list = flightRepository.findByDepartureCityAndArrivalCityAndDateOfDeparture("AUS", "NYC", LocalDate.of(2018, 1, 5));
System.out.println(list.size()); // prints out 1
}
}
存儲庫有效,我無法運行其他端點等,但查詢/模式等沒有問題。
添加回答
舉報