After the user has selected departure, she has to select destination Airport. This is similar to selecting departure, but only Airports that are connected via Flights from the departure Airport are available. Also, the user must select a departure Airport first.
We will use the same objects as before:

We will test that the user selects OSL as departure,
and gets 1 destination available: ARL.
Create a method void testListDestinations()
and implement it as follows:
public void testListDestinations() throws Exception {
orderTicket.setDeparture(osl);
java.util.List<Airport> destinations = orderTicket.listDestinations();
String[] expected = { "ARL" };
checkAirports(expected, destinations);
}
private void checkAirports(String[] expected, java.util.List<Airport> airports) {
java.util.HashSet<String> airportNames = new java.util.HashSet<String>();
for(Airport a : airports) {
airportNames.add(a.getCode());
}
assertEquals(
new java.util.HashSet<String>(java.util.Arrays.asList(expected)),
airportNames);
}
We need a query that will select flights from the current departure.
But instead of using the member variable departure as parameter in the query,
we will create a separate method that takes an Airport parameter.
We could place that query in the OrderTicket class, but it doesn't really belong there. We will place all queries in Data Access Objects.
A DAO is responsible for all interaction with the database. It hides the fact that we use Hibernate from other classes. If we replace Hibernate with another technology, only the DAO classes should need to be changed.
We will create a DAO object for the Airport class. OrderTicket will will use this and possibly other DAO's.
HiberObject can help creating DAO's for all <<Persistent>> classes. Right-click on the Airport class and select New -> DAO. This will create a class with standard methods to create, read, update and delete Airport objects in the database.
Add methods listAll() and listDestinations() in the DAO:

Modify OrderTicket listDepartures() to call the DAO:
public class OrderTicket {
private AirportDAO airportDAO = new AirportDAO();
public java.util.List<Airport> listDepartures() {
return airportDAO.listAll();
}
@generated tag on the method.
Otherwise, HiberObjects will overwrite the Java code when the class is saved.
An alternative is to delete the method and create it again.
Design the same query in AirportDAO.listAll() as previously in OrderTicket.listDestinations():
select a from Airport a
Implement OrderTicket listDestinations() :
public java.util.List<Airport> listDestinations()
{
return airportDAO.listDestinations(departure);
}Right-click on getDestinations in AirportDAO and select Edit Query. Then, type the following query:
select a from Airport a
where a.departures = :departureLet's test it in JUnit:

We cannot just pass an object into a HQL query. Let's modify the query to take an Airport id instead of an Airport:

select a from Airport a
where :departureId in a.departures.id
public java.util.List<Airport> getDestinations() {
return airportDAO.listDestinations(departure.getId());
}

Let's change the query to a join instead. It can be useful to have a look at the object diagram we are testing to figure it out.
select a from Airport a, Flight f
where f.from.id = :departureId
and f.to = a

There are still some methods to implement the whole use case, but I'll leave this as an exercise to the reader ;-)