Commit 9e57a1e5 authored by Vasil Merkul's avatar Vasil Merkul
Browse files

- many to many

parent 3b55803a
......@@ -35,6 +35,12 @@ public class PassengerController {
@GetMapping(value="/getAllPassengers")
public List<PassengerEntity> getAllPassengers(){
// service.getAllPassengers().forEach(passengerEntity -> System.out.println("FLIGHTS: " + passengerEntity.getFlights()));
return service.getAllPassengers();
}
......
......@@ -2,6 +2,7 @@ package server.dto;
import org.modelmapper.ModelMapper;
import server.model.FlightEntity;
import server.model.PassengerEntity;
import server.model.TicketEntity;
import javax.validation.constraints.NotNull;
......@@ -27,13 +28,21 @@ public class FlightDto {
private int distance;
@NotNull
private int terminalid;
// @NotNull
// private AirlineEntity airlineByAirlineid;
// @NotNull
// private TerminalEntity terminalByTerminalid;
@NotNull
private List<TicketEntity> ticketsById;
private List<PassengerEntity> passengerEntitiesById;
public List<PassengerEntity> getPassengerEntitiesById() {
return passengerEntitiesById;
}
public void setPassengerEntitiesById(List<PassengerEntity> passengerEntitiesById) {
this.passengerEntitiesById = passengerEntitiesById;
}
public int getId() {
return id;
}
......@@ -106,21 +115,6 @@ public class FlightDto {
this.terminalid = terminalid;
}
// public AirlineEntity getAirlineByAirlineid() {
// return airlineByAirlineid;
// }
//
// public void setAirlineByAirlineid(AirlineEntity airlineByAirlineid) {
// this.airlineByAirlineid = airlineByAirlineid;
// }
//
// public TerminalEntity getTerminalByTerminalid() {
// return terminalByTerminalid;
// }
//
// public void setTerminalByTerminalid(TerminalEntity terminalByTerminalid) {
// this.terminalByTerminalid = terminalByTerminalid;
// }
public List<TicketEntity> getTicketsById() {
return ticketsById;
......
package server.dto;
import org.modelmapper.ModelMapper;
import server.model.FlightEntity;
import server.model.PassengerEntity;
import javax.validation.constraints.NotNull;
import java.util.List;
public class PassengerDto {
......@@ -21,6 +23,17 @@ public class PassengerDto {
@NotNull
private int phonenumber;
private List<FlightEntity> flightEntitiesById;
public List<FlightEntity> getFlightEntitiesById() {
return flightEntitiesById;
}
public void setFlightEntitiesById(List<FlightEntity> flightEntitiesById) {
this.flightEntitiesById = flightEntitiesById;
}
public int getId() {
return id;
}
......
......@@ -8,6 +8,7 @@ import javax.persistence.*;
import java.sql.Timestamp;
import java.util.List;
import java.util.Objects;
import java.util.Set;
@Entity
@Table(name = "flight", schema = "airbook", catalog = "ear2018zs_22")
......@@ -25,6 +26,7 @@ public class FlightEntity {
private TerminalEntity terminalByTerminalid;
private List<TicketEntity> ticketsById;
@Id
@Column(name = "id")
public int getId() {
......@@ -150,6 +152,21 @@ public class FlightEntity {
this.airlineByAirlineid = airlineByAirlineid;
}
private Set<PassengerEntity> passengers;
@ManyToMany(cascade = CascadeType.PERSIST, fetch = FetchType.EAGER)
@JoinTable(name = "ticket", joinColumns = @JoinColumn(name = "flightid"),
inverseJoinColumns = @JoinColumn(name = "passengerid"))
public Set<PassengerEntity> getPassengers() {
return passengers;
}
public void setPassengers(Set<PassengerEntity> passengers) {
this.passengers = passengers;
}
@JsonIgnore
@ManyToOne(cascade = {CascadeType.PERSIST})
@JoinColumn(name = "terminalid", referencedColumnName = "id", nullable = false, insertable = false, updatable = false)
......
package server.model;
import org.hibernate.annotations.Target;
import javax.persistence.*;
import java.util.List;
import java.util.Objects;
import java.util.Set;
@Entity
@Table(name = "passenger", schema = "airbook", catalog = "ear2018zs_22")
......@@ -15,6 +18,15 @@ public class PassengerEntity {
private int phonenumber;
private List<TicketEntity> ticketsById;
@Id
@Column(name = "id")
public int getId() {
......@@ -94,6 +106,7 @@ public class PassengerEntity {
return Objects.hash(id, email, name, surname, password, phonenumber);
}
@OneToMany(cascade = {CascadeType.ALL}, orphanRemoval = true, mappedBy = "passengerByPassengerid")
public List<TicketEntity> getTicketsById() {
return ticketsById;
......@@ -102,4 +115,29 @@ public class PassengerEntity {
public void setTicketsById(List<TicketEntity> ticketsById) {
this.ticketsById = ticketsById;
}
private Set<FlightEntity> flights;
@ManyToMany(mappedBy = "passengers")
public Set<FlightEntity> getFlights() {
return flights;
}
public void setFlights(Set<FlightEntity> flights) {
this.flights = flights;
}
@Override
public String toString() {
return "PassengerEntity{" +
"id=" + id +
", email='" + email + '\'' +
", name='" + name + '\'' +
", surname='" + surname + '\'' +
", password='" + password + '\'' +
", phonenumber=" + phonenumber +
", ticketsById=" + ticketsById +
", flights=" + flights +
'}';
}
}
......@@ -8,4 +8,6 @@ import server.model.PassengerEntity;
public interface PassengerRepository extends JpaRepository<PassengerEntity, Integer> {
PassengerEntity findOneByName(String name);
}
......@@ -6,8 +6,10 @@ import org.springframework.stereotype.Service;
import server.dto.FlightDto;
import server.model.FlightEntity;
import server.repositories.FlightRepository;
import server.repositories.PassengerRepository;
import server.service.Interfaces.FlightServiceInterface;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
......@@ -15,11 +17,14 @@ import java.util.Optional;
public class FlightService implements FlightServiceInterface {
@Autowired
private FlightRepository repository;
private FlightRepository flightRepository;
@Autowired
private PassengerRepository passengerRepository;
@Autowired
public FlightService(FlightRepository repository) {
this.repository = repository;
this.flightRepository = repository;
}
public FlightService() {
......@@ -27,37 +32,39 @@ public class FlightService implements FlightServiceInterface {
@Override
public FlightEntity create(FlightDto flightDto) {
return repository.saveAndFlush(flightDto.toEntity());
return flightRepository.saveAndFlush(flightDto.toEntity());
}
@Override
public List<FlightEntity> getAllFlight() {
return repository.findAll();
// List<FlightEntity> flightEntities = new ArrayList<>();
// flightEntities = flightRepository.findAll();
return flightRepository.findAll();
}
@Override
public Optional<FlightEntity> getFlightByID(int id) {
return repository.findById(id);
return flightRepository.findById(id);
}
@Override
public void update(FlightDto flightDto) {
repository.saveAndFlush(flightDto.toEntity());
flightRepository.saveAndFlush(flightDto.toEntity());
}
public void updatePlane(Integer id, Integer newPlane){
FlightEntity passenger = repository.findById(id).orElseThrow(() -> new ResourceNotFoundException(id.toString()));
FlightEntity passenger = flightRepository.findById(id).orElseThrow(() -> new ResourceNotFoundException(id.toString()));
passenger.setPlaneid(newPlane);
repository.saveAndFlush(passenger);
flightRepository.saveAndFlush(passenger);
}
@Override
public void delete(FlightEntity flight) {
repository.saveAndFlush(flight);
flightRepository.saveAndFlush(flight);
}
@Override
public void deleteById(int id) {
repository.deleteById(id);
flightRepository.deleteById(id);
}
}
\ No newline at end of file
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://kbss.felk.cvut.cz:5433/ear2018zs_22?username=ear2018zs_22&password=Seledka14
spring.datasource.url=jdbc:postgresql://kbss.felk.cvut.cz:5433/ear2018zs_22?username=ear2018zs_22&password=Seledka14&searchpath=airbook
spring.datasource.username=ear2018zs_22
spring.datasource.hikari.maximum-pool-size=1
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment