Java Reference
In-Depth Information
Iterator ci = categories.iterator();
while(ci.hasNext()) {
Category category = (Category)ci.next();
System.out.println("Category: " + category.getTitle());
System.out.println();
Iterator ai = category.getAdverts().iterator();
while(ai.hasNext()) {
Advert advert = (Advert)ai.next();
System.out.println();
System.out.println("Title: " + advert.getTitle());
System.out.println(advert.getMessage());
System.out.println(" posted by " + advert.getUser().getName());
}
}
DAO.close();
} catch( AdException e ) {
System.out.println(e.getMessage());
}
}
}
A large part of the logic here is either output information, or concerned with accessing the
collections themselves. Java 5 devotees will see an obvious opportunity to make use of gener-
ics and enhanced for loops in this example. A quick taste of the simplified version of this code
might look like Listing 3-27.
Listing 3-27. Enhancing Your DAOs with Java 5 Features
List<Category> categories = new CategoryDAO().list();
for( Category category : categories ) {
// ...
for( Advert advert : category.getAdverts() ) {
// ...
}
}
DAO.close();
When you run the example applications, you will see a considerable amount of “chatter”
from the logging API, and from the Ant tool when you run these tasks, much of which can be
controlled or eliminated in a production application.
You will also notice that because you are starting each of these applications as new tasks
(several times in the case of the tasks to create data), the tasks proceed relatively slowly. This
is an artifact of the repeated creation of SessionFactory —a heavyweight object—from each
invocation of the JVM from the Ant java task, and is not a problem in “real” applications.
Search WWH ::




Custom Search