I am creating a flight booking application using spring boot. It has two entities: User and Flight
User entity:
@Entity
@Table(name = "User", uniqueConstraints = @UniqueConstraint(columnNames = "username"))
@Getter
@Setter
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column
private String username;
@Column
private String email;
@Column
private String password;
public User() {
super();
}
public User(Long id, String username, String email, String password) {
super();
this.id = id;
this.username = username;
this.email = email;
this.password = password;
}
}
Flight entity:
@Entity
@Table(name = "Flight" , uniqueConstraints = @UniqueConstraint(columnNames = "flightNumber"))
@Getter
@Setter
public class Flight {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long flightId;
@Column
private String flightNumber;
@Column
private String airlineName;
@Column
private String flightType;
@Column
private String origin;
@Column
private String destination;
@Column
private LocalTime departure;
@Column
private LocalTime arrival;
@Column
private Date date;
@Column
private Integer AvailableSeats;
@Column
private String price;
public Flight() {
super();
}
public Flight(Long flightId, String flightNumber, String airlineName, String flightType, String origin, String destination, LocalTime departure, LocalTime arrival, Date date, Integer availableSeats, String price) {
super();
this.flightId = flightId;
this.flightNumber = flightNumber;
this.airlineName = airlineName;
this.flightType = flightType;
this.origin = origin;
this.destination = destination;
this.departure = departure;
this.arrival = arrival;
this.date = date;
AvailableSeats = availableSeats;
this.price = price;
}
}
I want to create another entity called ‘Booking’ to store the booking records of users. It will have two columns, username (from User table) and flightNumber (from Flight table).
From what I understood, I have to do many-to-many mapping of User and Flight. How to achieve this in spring boot?
Hisham Gundagi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.