简体   繁体   中英

I am not able to retrieve the list of data from the dataBase

My Controller

@RequestMapping("findFlights")
public String findFlights(@RequestParam("from") String from, @RequestParam("to") String to,
        @RequestParam("departureDate") @DateTimeFormat(pattern ="MM-dd-yyyy") Date departureDate,
        ModelMap modelMap) {
    List<Flight> flights = flightRepository.findFlights(from, to, departureDate);

    modelMap.addAttribute("flights", flights);
    return "displayFlights";

}

}

MyRepository

public interface FlightRepository extends JpaRepository<Flight, Long> {

@Query("from Flight where departureCity=:departureCity and arrivalCity=:arrivalCity and dateOfDeparture=:dateOfDeparture")

List<Flight> findFlights(@Param("departureCity") String from, @Param("arrivalCity") String to,
        @Param("dateOfDeparture") Date departureDate);

}

MyEntity

@Entity
public class Flight extends AbstractEntity{
private String flightNumber;
private String operatingAirlines;
private String departureCity;
private String arrivalCity;
private Date dateOfDeparture;
private Timestamp estimatedDepartureTime;
// Getters and Setters

Issue I am not able to get the list of flights from the DB even when my DB has the required combination of data for: List<Flight> flights = flightRepository.findFlights(from, to, departureDate);

but flights list is always blank (flights[])

this method will replace the above query

 List<Flight> findByDepartureCityAndArrivalCityAndDateOfDeparture(String from,String to,Date departureDate)

or

try using Like or % in your Hql query.

For custom query you can use jpql

@Query("SELECT f FROM Flight f WHERE f.departureCity = ?1 and f.arrivalCity = ?2 and f.dateOfDeparture = ?3")
List<Flight> findFlight(String from, String to, Date departureDate);

You should annotate Entity class with @Column annotation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM