I have created a number of entity classes from the database using the JPA.
These are the classes:
- Flights
- Passengers
- PassengersFlights (containing primary key of passengers and flights)
- Users
In the database, there are two foreign keys in the PassengersFlights table - one to Flights and one to Passengers.
The only problem I have is that the primary keys of passengers and flights are of data type Passengers and Flights in the PassengersFlights entity class. However, the data types of these in their respective entity classes are String and int, as defined in the database.
I have the following code to update the PassengersFlights table:
// Updating the Passengers_Flights table
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/flights_db", "root", "hello");
EntityManagerFactory emFactory = Persistence.createEntityManagerFactory("Flights_AssignmentPU");
EntityManager em = emFactory.createEntityManager();
PassengersFlights booking = new PassengersFlights();
booking.setPassportNum(passport);
booking.setFlightId(flight_id);
em.getTransaction().begin();
em.persist(booking);
em.getTransaction().commit();
em.close();
}
catch (Exception e)
{
response.sendRedirect("ErrorPage.html");
}
The problem I am having is when setting the passport number and the flight id. In the project, they are defined as string and int. However, it is expecting Passenger and Flights data types. How can I solve this problem please?