Java Reference
In-Depth Information
public class ListMessages {
public static void main(String[] args)
{
SessionFactory factory =
new Configuration().configure().buildSessionFactory();
Session session = factory.openSession();
List messages = session.createQuery("from Message").list();
System.out.println("Found " + messages.size() + " message(s):");
Iterator i = messages.iterator();
while(i.hasNext()) {
Message msg = (Message)i.next();
System.out.println(msg.getMessage());
}
session.close();
}
}
The Ant target exportDDL will create an appropriate schema in the HSQLDB database files.
Running the populateMessages task will create a message entry (this can be invoked multiple
times). Running the listMessages task will list the messages that have been entered into the
database so far.
n Caution Because we have selected the drop="true" option for the hbm2ddl subtask of our
exportDDL target, running this script will effectively delete any data in the named tables. It is rarely a good
idea to run such a script from a machine that has database access to the production environment because
of the risk of accidentally deleting your production data!
The appropriate classpath entries have been set up in the Ant build script. To run a Hiber-
nate application, you need the hibernate.jar file from the root of the Hibernate distribution,
and a subset of the libraries provided in the lib subdirectory. The origin, purpose, and option-
ality of each of these libraries is explained in a README text file provided in the lib directory.
Most of the work required to get this example running is the sort of basic configuration
trivia that any application requires (writing Ant scripts, setting classpaths, and so on). The real
work consists of these steps:
1. Creating the Hibernate configuration file
2. Creating the mapping file
3. Writing the POJOs (introduced in Chapter 1)
Search WWH ::




Custom Search