⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 albumtest.java

📁 Hiberante程序高手秘籍的源代码
💻 JAVA
字号:
package com.oreilly.hh;import net.sf.hibernate.*;import net.sf.hibernate.cfg.Configuration;import java.sql.Time;import java.util.*;/** * Create sample album data, letting Hibernate persist it for us. */public class AlbumTest {    /**     * Quick and dirty helper method to handle repetitive portion of creating     * album tracks. A real implementation would have much more flexibility.     */    private static void addAlbumTrack(Album album, String title, String file,                                      Time length, Artist artist, int disc,                                      int positionOnDisc, Session session)        throws HibernateException    {        Track track = new Track(title, file, length, new Date(), (short)0,                                SourceMedia.CD, new HashSet(), new HashSet());        track.getArtists().add(artist);        //        session.save(track);        album.getTracks().add(new AlbumTrack(disc, positionOnDisc, track));    }    public static void main(String args[]) throws Exception {        // 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();    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -