AlbumTest.javaAPI DocExample4044Fri Feb 27 23:06:42 GMT 2004com.oreilly.hh


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

Fields Summary
Constructors Summary
Methods Summary
private static voidaddAlbumTrack(Album album, java.lang.String title, java.lang.String file, java.sql.Time length, Artist artist, int disc, int positionOnDisc, Session session)
Quick and dirty helper method to handle repetitive portion of creating album tracks. A real implementation would have much more flexibility.

        Track track = new Track(title, file, length, new Date(), (short)0,
                                new HashSet(), new HashSet());
        album.getTracks().add(new AlbumTrack(disc, positionOnDisc, track));
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.

        // 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();

            Artist artist = CreateTest.getArtist("Martin L. Gore", true,
            List albumTracks = new ArrayList(5);
            Album album = new Album("Counterfeit e.p.", 1, new Date(),
                                    albumTracks, new HashSet(), new HashSet());

            addAlbumTrack(album, "Compulsion", "vol1/album83/track01.mp3",
                          Time.valueOf("00:05:29"), artist, 1, 1, session);
            addAlbumTrack(album, "In a Manner of Speaking",
                          "vol1/album83/track02.mp3", Time.valueOf("00:04:21"),
                          artist, 1, 2, session);
            addAlbumTrack(album, "Smile in the Crowd",
                          "vol1/album83/track03.mp3", Time.valueOf("00:05:06"),
                          artist, 1, 3, session);
            addAlbumTrack(album, "Gone", "vol1/album83/track04.mp3",
                          Time.valueOf("00:03:32"), artist, 1, 4, session);
            addAlbumTrack(album, "Never Turn Your Back on Mother Earth",
                          "vol1/album83/track05.mp3", Time.valueOf("00:03:07"),
                          artist, 1, 5, session);
            addAlbumTrack(album, "Motherless Child", "vol1/album83/track06.mp3",
                          Time.valueOf("00:03:32"), artist, 1, 6, session);


            // We're done; make our changes permanent

            // Expermients discussed in "Lifecycle Associations"
            //tx = session.beginTransaction();
            //tx = session.beginTransaction();

        } 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