On the database, I have these two tables:
- Destination:
- idDestination
- name
- Airport:
- idAirport
- idDestination // FK into Destination city
- name
where:
- 1 Destination(read: city) has many Airports
- 1 Airport belongs to 1 City
- Hence: 1-Many relation between Destination-Airports
My Java classes look like this:
class Destination{
private Integer idDestination;
private String name;
// getter and setters
}
class Airport{
private Integer idAirport;
private Destination city;
private String name;
}
// Separate class for airports in city, since city is being used in a lot of other places
// and I'd like to keep Destination class clean
class CityAirports{
private Destination City;
private Set<Airport> airports;
}
Hibernate Mappings: Airport.hbm.xml
<hibernate-mapping>
<class name="org.wah.dao.Airport" table="AIRPORT">
<id name="idAirport" type="java.lang.Integer">
<column name="IDAIRPORT" />
<generator class="identity" />
</id>
<property name="name" type="java.lang.String">
<column name="NAME" />
</property>
<many-to-one name="city" class="org.wah.dao.Destination">
<column name="IDCITY" />
</many-to-one>
</class>
</hibernate-mapping>
I need to define another mapping for CityAirports to
– retrieve all the airports within the city.
– add a new airport to the city.
I am not sure what the hibernate mapping would look like ? Can someone please guide me on how to do it?
for destination mapping,
for airport mapping,