Posts

Showing posts with the label Hibernate Mapping

Can Someone Explain MappedBy In JPA And Hibernate?

Answer : MappedBy signals hibernate that the key for the relationship is on the other side. This means that although you link 2 tables together, only 1 of those tables has a foreign key constraint to the other one. MappedBy allows you to still link from the table not containing the constraint to the other table. By specifying the @JoinColumn on both models you don't have a two way relationship. You have two one way relationships, and a very confusing mapping of it at that. You're telling both models that they "own" the IDAIRLINE column. Really only one of them actually should! The 'normal' thing is to take the @JoinColumn off of the @OneToMany side entirely, and instead add mappedBy to the @OneToMany . @OneToMany(cascade = CascadeType.ALL, mappedBy="airline") public Set<AirlineFlight> getAirlineFlights() { return airlineFlights; } That tells Hibernate "Go look over on the bean property named 'airline' on the th...