Master/detail variant

Here a more pure implementation of the master detail pattern. This one looks better but probably performs not as good as the other implementation, because of the multiple database calls to get the details (the other implementation gets all the details out of the database in one swoop).

Notice the use of a wrapbean, a list thereof that gets assembled in the getAlbumList() method. Also notice that the getMasterList() and getDetailList() methods are declared to be implementation methods, ie. they are private and can only be used from within the service class.

Bean definition


bean MasterBean 
{
    String id;
    String artist;
    String album;
}

bean DetailBean 
{
    String masterId;
    int trackno;
    String title;
}

wrapbean Album
{
    String artist;
    String album;
    List<String> trackList;
}

Service definition


service MasterDetailVariantService
{
    implementation List<MasterBean> getMasterList()
    <<
        select id, artist, album
        from   t_master
    >>

    implementation List<DetailBean> getDetailList(String masterId)
    <<
        select master_id, trackno, title
        from   t_detail
        where  master_id=?
    >>


    List<Album> getAlbumList()
    {{
        List<Album> albumList= new ArrayList<Album>();

        for ( MasterBean m : getMasterList() ) 
        {
            Album a=new Album();

            // set master details
            a.setArtist(m.getArtist());
            a.setAlbum(m.getAlbum());

            // set the detail details
            List<String> tList=new ArrayList<String>();
            for (DetailBean d : getDetailList( m.getId() ) )
            {
               tList.add( d.getTitle() ) ; 
            }
            a.setTrackList(tList);

            albumList.add(a);
        } 

        return albumList;
    }} 

}

Full bean and service definition : MasterDetailVariant.mlg

The database tables

t_master

    id    |        artist         |      album       
----------+-----------------------+------------------
 2a087a04 | Frank Zappa           | Waka Jawaka
 8e0f950a | PK                    | Chishala
 f40d3812 | Johann Sebastian Bach | Suites for Cello

t_detail

 master_id | trackno |              title               
-----------+---------+----------------------------------
 2a087a04  |       1 | Big Swifty
 2a087a04  |       2 | Your Mouth
 2a087a04  |       3 | It Just Might Be a One-Shot Deal
 2a087a04  |       4 | Waka/Jawaka
 8e0f950a  |       1 | Nakufele
 8e0f950a  |       2 | Umwaume Walutuku
 8e0f950a  |       3 | Church Elder
..

The Main java class

MasterDetailVariantService service=new MasterDetailVariantServiceImpl();
    
// get the master list
List<Album> albumList=service.getAlbumList();
    
for (Album a : albumList ) 
{
    System.out.println( a.getArtist() + " / " + a.getAlbum() );
    
    for ( String track : a.getTrackList() ) 
    {
        System.out.println("- " + track );
    }
    System.out.println();
}

Output

Frank Zappa / Waka Jawaka
- Big Swifty
- Your Mouth
- It Just Might Be a One-Shot Deal
- Waka/Jawaka

PK / Chishala
- Nakufele
- Umwaume Walutuku
- Church Elder
- Minofu Ya Mbowa
- Impumpa Mukowa
- Kubwaiche
- Mutete
- Na Musonda
- Pali Iwe
- Umunandi

Johann Sebastian Bach / Suites for Cello
- Suite No. 1 in G major, BWV 1007 - I. Prelude (moderato)
- Suite No. 1 in G major, BWV 1007 - II. Allemande (molto moderato)
- Suite No. 1 in G major, BWV 1007 - III. Courante (allegro non troppo)
- Suite No. 1 in G major, BWV 1007 - IV. Sarabande (lento)
- Suite No. 1 in G major, BWV 1007 - V. Menuetto I & II (allegro moderato)
- Suite No. 1 in G major, BWV 1007 - VI. Gigue (vivace)
- Suite No. 2 in D minor, BWV 1008 - I. Praeludium
- Suite No. 2 in D minor, BWV 1008 - II. Allemande
- Suite No. 2 in D minor, BWV 1008 - III. Courante
- Suite No. 2 in D minor, BWV 1008 - IV. Sarabande
- Suite No. 2 in D minor, BWV 1008 - V. Menuetto I & II
- Suite No. 2 in D minor, BWV 1008 - VI. Gigue
- Suite No. 3 in C major, BWV 1009 - I. Praeludium
- Suite No. 3 in C major, BWV 1009 - II. Allemande
- Suite No. 3 in C major, BWV 1009 - III. Courante
- Suite No. 3 in C major, BWV 1009 - IV. Sarabande
- Suite No. 3 in C major, BWV 1009 - V. Bourree I & II
- Suite No. 3 in C major, BWV 1009 - VI. Gigue

All Files

SQL file(s):

Java file(s):

Mlg file(s):

© Willem Moors, 2009 - 2013