CreateTest.javaAPI DocExample2293Fri Feb 27 23:06:02 GMT 2004com.oreilly.hh


public class CreateTest extends Object
Create sample data, letting Hibernate persist it for us.

Fields Summary
Constructors Summary
Methods Summary
public static voidmain(java.lang.String[] args)

        // Create a configuration based on the properties file we've put
        // in the standard place.
        Configuration config = new Configuration();

        // Tell it about the classes we want mapped, taking advantage of
        // the way we've named their mapping documents.

        // Get the session factory we can use for persistence
        SessionFactory sessionFactory = config.buildSessionFactory();

        // Ask for a session using the JDBC information we've configured
        Session session = sessionFactory.openSession();
        Transaction tx = null;
        try {
            // Create some data and persist it
            tx = session.beginTransaction();

            Track track = new Track("Russian Trance",
                                    Time.valueOf("00:03:30"), new Date(),

            track = new Track("Video Killed the Radio Star",
                              Time.valueOf("00:03:49"), new Date(),

             track = new Track("Gravity's Angel",
                               Time.valueOf("00:06:06"), new Date(),

            // We're done; make our changes permanent

        } catch (Exception e) {
            if (tx != null) {
                // Something went wrong; discard all partial changes
            throw e;
        } finally {
            // No matter what, close the session

        // Clean up after ourselves