AlbumTestpublic class AlbumTest extends Object Create sample album data, letting Hibernate persist it for us. |
Methods Summary |
---|
private static void | addAlbumTrack(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());
track.getArtists().add(artist);
// session.save(track);
album.getTracks().add(new AlbumTrack(disc, positionOnDisc, track));
| public static void | main(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.
config.addClass(Track.class).addClass(Artist.class);
config.addClass(Album.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();
Artist artist = CreateTest.getArtist("Martin L. Gore", true,
session);
List albumTracks = new ArrayList(5);
Album album = new Album("Counterfeit e.p.", 1, new Date(),
albumTracks, new HashSet(), new HashSet());
album.getArtists().add(artist);
session.save(album);
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);
System.out.println(album);
// We're done; make our changes permanent
tx.commit();
// Expermients discussed in "Lifecycle Associations"
//tx = session.beginTransaction();
//album.getTracks().remove(1);
//session.update(album);
//tx.commit();
//
//tx = session.beginTransaction();
//session.delete(album);
//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();
|
|