问题
I have two tables Ticket
and Flight
. One flight could have many tickets.
I want to show fields departure_date
, destination_date
from the table Flight
and name
, surname
from the table Ticket
. And show data only for the certain flight_id
. I use findBy method.
Entity Flight
@Entity
@Table(name = "flight")
public class Flight {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer flight_id;
@Column(name = "departureDate")
private Date departureDate;
@Column(name = "destinationDate")
private Date destinationDate;
@OneToMany(mappedBy = "flight")
@JsonManagedReference("flight")
private List<Ticket> tickets;
Entity Ticket
@Entity
@Table(name = "ticket")
public class Ticket {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int ticket_id;
@Column(name = "name")
private String name;
@Column(name = "surname")
private String surname;
@ManyToOne(targetEntity = Flight.class)
@JoinColumn(name = "flight_id")
@JsonBackReference("flight")
@Fetch(FetchMode.JOIN)
private Flight flight;
I've created file FlightsTicketDto with certain fields:
public class FlightTicketDto {
private Integer flight_id;
private Date departureDate;
private Date destinationDate;
private String name;
private String surname;
public FlightTicketDto() {
}
public FlightTicketDto(Integer flight_id, Date departureDate, Date destinationDate, String name, String surname) {
this.flight_id = flight_id;
this.departureDate = departureDate;
this.destinationDate = destinationDate;
this.name = name;
this.surname = surname;
}
FlightTicketRepository with my Query
public interface FlightTicketRepository extends JpaRepository<Ticket, Integer> {
@Query("SELECT new pl.edu.wat.dto.FlightTicketDto(f.flight_id, f.departureDate, f.destinationDate, t.name, t.surname) "
+ "FROM Flight f INNER JOIN f.tickets t")
List<FlightTicketDto> findByFlightId(Integer flight_id);
}
FlightTicketController
@CrossOrigin(origins = "http://localhost:4200")
@RestController
@RequestMapping("/api")
public class FlightTicketController {
@Autowired
FlightTicketRepository flightTicketRepository;
@GetMapping("/mytickets/{flight_id}")
public List fetchEmpDeptDataInnerJoin(@PathVariable Integer flight_id) {
return flightTicketRepository.findByFlightId(flight_id);
}
Actually whatever flight_id
(even not flight_id, but just another number) I write, I've got all my flights
For example I want to get result only for flight_id = 431
, result you can see on the picture. What's wrong?
回答1:
replace
@Query("SELECT new pl.edu.wat.dto.FlightTicketDto(f.flight_id, f.departureDate, f.destinationDate, t.name, t.surname) "
+ "FROM Flight f INNER JOIN f.tickets t")
List<FlightTicketDto> findByFlightId(Integer flight_id);
with
@Query("SELECT new pl.edu.wat.dto.FlightTicketDto(f.flight_id, f.departureDate, f.destinationDate, t.name, t.surname) "
+ "FROM Flight f INNER JOIN f.tickets t where f.flight_id = :flight_id")
List<FlightTicketDto> findByFlightId(@Param("flight_id") Integer flight_id);
来源:https://stackoverflow.com/questions/54101081/spring-boot-data-jpa-how-to-get-data-for-the-certain-id