> I'm now wondering if the problem is related to how I am persisting the
> data to the database.
[quoted text clipped - 3 lines]
> for each Album and it's tracks then the Track table is updated as I
> want.
Right, I imported this onto my bench, and I think you were quite close,
there were just a few of Hibernates 'quirks' in the mappings that were
throwing you. I believe I have the behaviour you're after now, with the
mappings:
// -----
<class name="Album" table="ALBUM">
<id name="id" type="long" column="ALBUM_ID">
<generator class="increment"/>
</id>
<property name="albumName" column="NAME"/>
<set name="tracks" cascade="save-update" lazy="true">
<key column="ALBUM_ID"/>
<one-to-many class="Track"/>
</set>
</class>
// -----
// -----
<class name="Track" table="TRACK">
<id name="id" type="long" column="TRACK_ID">
<generator class="increment"/>
</id>
<property name="trackName" column="NAME"/>
<many-to-one name="album" column="ALBUM_ID" class="Album"/>
</class>
// -----
This deviates from your original specification, in that a track can
belong to only one album, and at most once, but I think this is what
you're after?
With this relationship you must ensure you link tracks to their album,
as well as adding them to the set. I usually protect the bean getters
and setters and provide a public interface with something like:
public void addTrack(Track track) {
track.album = this;
tracks.add(track);
}
It is possible to have this done for you, but that kind of setup has
caused me some pain so now I keep it simple where I can...
A quick usage example (assumes constructors and the above method):
public makeSomeAlbums() {
Session session = Hibernate.currentSession();
Transaction tx = session.beginTransaction();
Album greatest = new Album("Greatest Hits");
Album worstest = new Album("Worstest Bits");
greatest.addTrack(new Track("Track one"));
greatest.addTrack(new Track("Track two"));
worstest.addTrack(new Track("Track one"));
worstest.addTrack(new Track("Track two"));
session.save(greatest);
session.save(worstest);
tx.commit();
Hibernate.closeSession();
}
Gives you an almost identical (save for the actual data) table to that
you originally posted.
Well, hope that helps :)

Signature
[Ross A. Bamford] [ross AT the.website.domain]
Roscopeco Open Tech ++ Open Source + Java + Apache + CMF
http://www.roscopec0.f9.co.uk/ + info@the.website.domain
Ross Bamford - 27 May 2005 00:13 GMT
> // -----
> <class name="Album" table="ALBUM">
[quoted text clipped - 10 lines]
> </class>
> // -----
The set in this mapping should be 'inverse=true'. Sorry.
Also, to address another point, Remember that with the Set you don't get
any ordering, and you don't have enough data in your legacy tables to
provide track ordering per album. Does that matter?

Signature
[Ross A. Bamford] [ross AT the.website.domain]
Roscopeco Open Tech ++ Open Source + Java + Apache + CMF
http://www.roscopec0.f9.co.uk/ + info@the.website.domain
kevkev - 27 May 2005 09:54 GMT
Ross, you are a star!
Reading you mail made me realise that I was trying to persist the same
track object to different albums and therefore was an update the to the
existing transient track object.
Of course I needed to made a new track each time. I feel so dumb, but
hey it's sorted now. I'm sure I'll have more questions in the future
though! :)
Thank you so much for all your help with this. You're a wonderful
example of the community spirit.
Regards,
Kevin