Master/detail example
This example shows you how to handle master detail records in a simple, elegant way: - load the masterbeans into a list: List<MasterBean>
- and the detail-beans into a map of a string to lists: Map<String,List<DetailBean>>
The key that links them is the id of the master-record. The data used in this example is a list of albums (the master data) and the tracks they contain (the detail-data). This Map<String,List<>> is treated as a special case: have a look at the resulting java code for getDetailMap() in the implementation class generated/MasterDetailServiceImpl.java.
Bean definition
bean MasterBean
{
String id;
String artist;
String album;
}
bean DetailBean
{
String masterId;
int trackno;
String title;
}
Service definition
service MasterDetailService
{
List<MasterBean> getMasterList()
<<
select id, artist, album
from t_master
>>
Map<String,List<DetailBean>> getDetailMap()
<<
select master_id, trackno, title
from t_detail
>>
}
See the complete code for the bean and service definition : MasterDetail.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
Excerpt of the code in the Main class, using the MasterDetailService to print out the master and corresponding detail records.
MasterDetailService service=new MasterDetailServiceImpl();
// get the master list
List<MasterBean> masterBeanList=service.getMasterList();
// get the map to the detail lists
Map<String,List<DetailBean>> detailMap=service.getDetailMap();
for (MasterBean m : masterBeanList)
{
System.out.println( m.getArtist() + " / " + m.getAlbum() );
for ( DetailBean d : detailMap.get(m.getId()) )
{
System.out.println(d.getTrackno() + ": " + d.getTitle() );
}
System.out.println();
}
Output
When you run the above, this is the output:
Frank Zappa / Waka Jawaka
1: Big Swifty
2: Your Mouth
3: It Just Might Be a One-Shot Deal
4: Waka/Jawaka
PK / Chishala
1: Nakufele
2: Umwaume Walutuku
3: Church Elder
4: Minofu Ya Mbowa
5: Impumpa Mukowa
6: Kubwaiche
7: Mutete
8: Na Musonda
9: Pali Iwe
10: Umunandi
Johann Sebastian Bach / Suites for Cello
1: Suite No. 1 in G major, BWV 1007 - I. Prelude (moderato)
2: Suite No. 1 in G major, BWV 1007 - II. Allemande (molto moderato)
3: Suite No. 1 in G major, BWV 1007 - III. Courante (allegro non troppo)
4: Suite No. 1 in G major, BWV 1007 - IV. Sarabande (lento)
5: Suite No. 1 in G major, BWV 1007 - V. Menuetto I & II (allegro moderato)
6: Suite No. 1 in G major, BWV 1007 - VI. Gigue (vivace)
7: Suite No. 2 in D minor, BWV 1008 - I. Praeludium
8: Suite No. 2 in D minor, BWV 1008 - II. Allemande
9: Suite No. 2 in D minor, BWV 1008 - III. Courante
10: Suite No. 2 in D minor, BWV 1008 - IV. Sarabande
11: Suite No. 2 in D minor, BWV 1008 - V. Menuetto I & II
12: Suite No. 2 in D minor, BWV 1008 - VI. Gigue
13: Suite No. 3 in C major, BWV 1009 - I. Praeludium
14: Suite No. 3 in C major, BWV 1009 - II. Allemande
15: Suite No. 3 in C major, BWV 1009 - III. Courante
16: Suite No. 3 in C major, BWV 1009 - IV. Sarabande
17: Suite No. 3 in C major, BWV 1009 - V. Bourree I & II
18: Suite No. 3 in C major, BWV 1009 - VI. Gigue
All Files
SQL file(s): Java file(s): Mlg file: |