Persist Some Objects

It's time to create some objects in Java and persist them to the database, so we can see how they turn into rows and columns for us. Because of the way we’ve organized our mapping document and properties file, it’s extremely easy to configure the Hibernate session factory and get things rolling.

This discussion assumes you’ve created the schema and generated Java code by following the preceding examples. If you haven’t, you can start by downloading the examples archive and copying in the third-party libraries as instructed in “Set Up a Project Hierarchy,” earlier. Once you’ve done that, use the commands ant codegen followed by ant schema to set up the generated Java code and database schema on which this example is based. (As with the other examples, these commands should be issued in a shell whose current working directory is the top of your project tree, containing Ant’s build.xml file.)

If, on the other hand, you have been working through the examples, there is one line to be added to the hibernate.properties file in the src directory. As things stand, when we start telling Hibernate to perform actual persistence operations for us, it is going to warn us that we haven't properly configured its second-level caching systems. For a simple application like this, we don't need any at all, so we can turn that feature off. Add this line at the end of hibernate.properties:

hibernate.cache.provider_class=org.hibernate.cache.NoCacheProvider

We’ll start with a simple example ...

Get Getting Started with Hibernate 3 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.