4.5 Working with Simple Collections

     

The collections we've been looking at so far have all contained associations to other objects, which is appropriate for a chapter titled 'Collections and Associations,' but isn't the only kind you can use with Hibernate. You can also define mappings for collections of simple values, like strings, numbers , and nonpersistent value classes.

4.5.1 How do I do that?

Suppose we want to be able to record some number of comments about each track in the database. We want a new property called comments to contain the String values of each associated comment. The new mapping in Tracks.hbm.xml looks a lot like what we did for artists , only a bit simpler:

 <set name="comments" table="TRACK_COMMENTS">    <key column="TRACK_ID"/>    <element column="COMMENT" type="string"/> </set> 

Since we're able to store an arbitrary number of comments for each Track , we're going to need a new table to put them in. Each comment will be linked to the proper Track through the track's id property.

Rebuilding the databases with ant schema shows how this gets built in the database:

 [schemaexport] create table TRACK_COMMENTS ( [schemaexport]    TRACK_ID INTEGER not null, [schemaexport]    COMMENT VARCHAR(255) [schemaexport] ) [schemaexport] alter table TRACK_COMMENTS add constraint FK105B26884C5F92B foreign key (TRACK_ID) references TRACK 

NOTE

Data modeling junkies will recognize this as a 'one-to-many' relationship.

After updating the Track class via ant codegen , we need to add another Set at the end of each constructor invocation in CreateTest.java , for the comments. For example:

 track = new Track("Test Tone 1",                   "vol2/singles/test01.mp3",                   Time.valueOf("00:00:10"), new Date(),                   (short)0, new HashSet()  , new HashSet()  ); 

Then we can assign a comment on the following line:

 track.getComments().add("Pink noise to test equalization"); 

A quick ant ctest will compile and run this (making sure you've not forgotten to add the second HashSet to any tracks), and you can check data/music.script to see how it's stored in the database. Or add another loop after the track println() in QueryTest.java to print the comments for the track that was just displayed:

 for (Iterator comIter = aTrack.getComments().iterator() ;      comIter.hasNext() ; ) {     System.out.println(" Comment: " + comIter.next()); } 

Then ant qtest will give you output like this:

 ... [java] Track: "Test Tone 1" 00:00:10 [java]   Comment: Pink noise to test equalization 

It's nice when tools make simple things easier. In the next chapter we'll see that more complex things are possible too.



Hibernate. A Developer's Notebook
Hibernate: A Developers Notebook
ISBN: 0596006969
EAN: 2147483647
Year: 2003
Pages: 65
Authors: James Elliott

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net