FileDocCategorySizeDatePackage
CreateTest.javaAPI DocExample5826Fri Feb 27 23:06:50 GMT 2004com.oreilly.hh

CreateTest

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

Fields Summary
Constructors Summary
Methods Summary
private static voidaddTrackArtist(Track track, Artist artist)
Utility method to associate an artist with a track

        track.getArtists().add(artist);
    
public static ArtistgetArtist(java.lang.String name, boolean create, Session session)
Look up an artist record given a name.

param
name the name of the artist desired.
param
create controls whether a new record should be created if the specified artist is not yet in the database.
param
session the Hibernate session that can retrieve data
return
the artist with the specified name, or null if no such artist exists and create is false.
throws
HibernateException if there is a problem.

        Query query = session.getNamedQuery(
                          "com.oreilly.hh.artistByName");
        query.setString("name", name);
        Artist found = (Artist)query.uniqueResult();
        if (found == null && create) {
            found = new Artist(name, null, new HashSet());
            session.save(found);
        }
        if (found != null && found.getActualArtist() != null) {
            return found.getActualArtist();
        }
        return found;
    
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.
        config.addClass(Track.class).addClass(Artist.class);

        // 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",
                                    "vol2/album610/track02.mp3",
                                    Time.valueOf("00:03:30"), new Date(),
                                    (short)0, new HashSet(), new HashSet());
            addTrackArtist(track, getArtist("PPK", true, session));
            session.save(track);

            track = new Track("Video Killed the Radio Star",
                              "vol2/album611/track12.mp3",
                              Time.valueOf("00:03:49"), new Date(),
                              (short)0, new HashSet(), new HashSet());
            addTrackArtist(track, getArtist("The Buggles", true, session));
            session.save(track);

            
            track = new Track("Gravity's Angel",
                              "vol2/album175/track03.mp3",
                              Time.valueOf("00:06:06"), new Date(),
                              (short)0, new HashSet(), new HashSet());
            addTrackArtist(track, getArtist("Laurie Anderson", true, session));
            session.save(track);

            track = new Track("Adagio for Strings (Ferry Corsten Remix)",
                              "vol2/album972/track01.mp3",
                              Time.valueOf("00:06:35"), new Date(),
                              (short)0, new HashSet(), new HashSet());
            addTrackArtist(track, getArtist("William Orbit", true, session));
            addTrackArtist(track, getArtist("Ferry Corsten", true, session));
            addTrackArtist(track, getArtist("Samuel Barber", true, session));
            session.save(track);

            track = new Track("Adagio for Strings (ATB Remix)",
                              "vol2/album972/track02.mp3",
                              Time.valueOf("00:07:39"), new Date(),
                              (short)0, new HashSet(), new HashSet());
            addTrackArtist(track, getArtist("William Orbit", true, session));
            addTrackArtist(track, getArtist("ATB", true, session));
            addTrackArtist(track, getArtist("Samuel Barber", true, session));
            session.save(track);

            track = new Track("The World '99",
                              "vol2/singles/pvw99.mp3",
                              Time.valueOf("00:07:05"), new Date(),
                              (short)0, new HashSet(), new HashSet());
            addTrackArtist(track, getArtist("Pulp Victim", true, session));
            addTrackArtist(track, getArtist("Ferry Corsten", true, session));
            session.save(track);

            track = new Track("Test Tone 1",
                              "vol2/singles/test01.mp3",
                              Time.valueOf("00:00:10"), new Date(),
                              (short)0, new HashSet(), new HashSet());
            track.getComments().add("Pink noise to test equalization");
            session.save(track);

            // We're done; make our changes permanent
            tx.commit();

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

        // Clean up after ourselves
        sessionFactory.close();