Issue
These are the tables I intend to have in my database.
href="https://i.stack.imgur.com/PJoiO.png" rel="nofollow noreferrer">Third table
Solution
@Entity
@Table(name = "STATIONS")
public class Station {
@Id
private Long stnCode;
}
@Entity
@Table(name = "TRAINS")
public class Train {
@Id
private String trainNo;
}
@Entity
@Table(name = "STOPS")
public class Stop {
@EmbeddedId
private StopId stopId;
}
@Embeddable
class StopId implements Serializable {
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "TRAIN_NO")
private Train trainNo;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "STN_CODE")
private Station stnCode;
}
Hibernate: create table STATIONS (stnCode bigint not null, primary key (stnCode))
Hibernate: create table STOPS (STN_CODE bigint not null, TRAIN_NO varchar(255) not null, primary key (STN_CODE, TRAIN_NO))
Hibernate: create table TRAINS (trainNo varchar(255) not null, primary key (trainNo))
Hibernate: alter table STOPS add constraint FK1431wv4cp0qef8drx65we3m03 foreign key (STN_CODE) references STATIONS
Hibernate: alter table STOPS add constraint FK5omkrq9vt17i1w6qose6njvhw foreign key (TRAIN_NO) references TRAINS
Answered By - Ivan Zbykovskyi
Answer Checked By - Terry (JavaFixing Volunteer)