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

📄 querytest.java

📁 Hiberante程序高手秘籍的源代码
💻 JAVA
字号:
package com.oreilly.hh;import net.sf.hibernate.*;import net.sf.hibernate.cfg.Configuration;import net.sf.hibernate.expression.*;import java.sql.Time;import java.util.*;/** * Retrieve data as objects */public class QueryTest {    /**     * Retrieve any tracks that fit in the specified amount of time.     *     * @param length the maximum playing time for tracks to be returned.     * @param session the Hibernate session that can retrieve data.     * @return a list of {@link Track}s meeting the length restriction.     * @throws HibernateException if there is a problem.     */    public static List tracksNoLongerThan(Time length, Session session)        throws HibernateException    {        Criteria criteria = session.createCriteria(Track.class);        criteria.add(Expression.le("playTime", length));        criteria.addOrder(Order.asc("title"));        return criteria.list();    }       /**     * Retrieve any tracks associated with artists whose name matches a     * SQL string pattern.     *     * @param namePattern the pattern which an artist's name must match     * @param session the Hibernate session that can retrieve data.     * @return a list of {@link Track}s meeting the artist name restriction.     * @throws HibernateException if there is a problem.     */    public static List tracksWithArtistLike(String namePattern, Session session)        throws HibernateException    {        Criteria criteria = session.createCriteria(Track.class);        Example example = Example.create(new Artist(namePattern, null, null));        criteria.createCriteria("artists").add(example.enableLike());        criteria.addOrder(Order.asc("title"));        return criteria.list();    }       /**     * Retrieve any tracks that were obtained from a particular source     * media type.     *     * @param sourceMedia the media type of interest.     * @param session the Hibernate session that can retrieve data.     * @return a list of {@link Track}s meeting the media restriction.     * @throws HibernateException if there is a problem.     */    public static List tracksFromMedia(SourceMedia media, Session session)        throws HibernateException    {        Track track = new Track();        track.setSourceMedia(media);        Example example = Example.create(track);        Criteria criteria = session.createCriteria(Track.class);        criteria.add(example);        criteria.addOrder(Order.asc("title"));        return criteria.list();    }           /**     * Build a parenthetical, comma-separated list of artist names.     * @param artists the artists whose names are to be displayed.     * @return formatted list, or an empty string if the set was empty.     */    public static String listArtistNames(Set artists) {        StringBuffer result = new StringBuffer();        for (Iterator iter = artists.iterator(); iter.hasNext(); ) {            Artist artist = (Artist)iter.next();            result.append((result.length() == 0) ? "(" : ", ");            result.append(artist.getName());        }        if (result.length() > 0) {            result.append(") ");        }        return result.toString();    }    /**     * Look up and print some tracks when invoked from the command line.     */    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, 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();        try {            // Print the tracks that will fit in seven minutes            //List tracks = tracksNoLongerThan(Time.valueOf("00:07:00"),            //                                 session);            // Print tracks that came from CDs            // List tracks = tracksFromMedia(SourceMedia.CD, session);            // Print tracks associated with an artist whose name ends with "n"            List tracks = tracksWithArtistLike("%n", session);            for (ListIterator iter = tracks.listIterator() ;                 iter.hasNext() ; ) {                Track aTrack = (Track)iter.next();                String mediaInfo = "";                if (aTrack.getSourceMedia() != null) {                    mediaInfo = ", from " +                        aTrack.getSourceMedia().getDescription();                }                System.out.println("Track: \"" + aTrack.getTitle() + "\" " +                                   listArtistNames(aTrack.getArtists()) +                                   aTrack.getPlayTime() + mediaInfo);                for (Iterator comIter = aTrack.getComments().iterator() ;                     comIter.hasNext() ; ) {                    System.out.println("  Comment: " + comIter.next());                }            }        } 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 + -