Hi,
I have a problem which I believe is related to how I’m persisting data to the database.
I’ve got a one-to-many relationship with the mapping documents below. Now when I persist the data by running the test class three times, once for each Album and it’s tracks then the Track table is updated as I want.
TRACK_ID | NAME | ALBUM_ID
------------------------------------------
1 | Track One | 1
------------------------------------------
2 | Track Two | 1
------------------------------------------
3 | Track One | 2
------------------------------------------
4 | Track Two | 3
But when I persist all the albums and their tracks in one go then the TRACK table is updated rather than having a row inserted and I get the following which I don’t want.
TRACK Table
TRACK_ID | NAME | ALBUM_ID
------------------------------------------
1 | Track One | 2
------------------------------------------
2 | Track Two | 3
Now I understand that the data only gets persisted once the connection is closed so how do I get Hibernate to either persist the data straight away/or not to update the data and perform a new insert.
Any help would be really appreciated with this.
Regards,
Kevin
Code:
<class name="Album" table="ALBUM">
<id name="id" type="int" column="ALBUM_ID" unsaved-value="any">
<meta attribute="scope-set">protected</meta>
<generator class="native"/>
</id>
<property name="albumName" column="NAME" type="string"/>
<set name="tracks" cascade="all" inverse="true" lazy="true">
<key column="ALBUM_ID"/>
<one-to-many class="com.kbilly.mdr.mdrhibernate.Track"/>
</set>
</class>
And
Code:
<class name="Track" table="TRACK">
<id name="id" type="int" column="TRACK_ID" unsaved-value="any">
<meta attribute="scope-set">protected</meta>
<generator class="native"/>
</id>
<property name="trackName" column="NAME" type="string"/>
<many-to-one
name="album"
column="ALBUM_ID"
cascade="all"
class="com.kbilly.mdr.mdrhibernate.Album"/>
</class>
The code I have to persist data is as follows.
Code:
public class TestAlbums {
public static void main(String[] args) {
Track t1 = new Track();
t1.setTrackName("Track One");
Track t2 = new Track();
t2.setTrackName("Track Two");
Track t3 = new Track();
t3.setTrackName("Track Three");
Album one = new Album();
one.setAlbumName("Album One");
Album two = new Album();
two.setAlbumName("Album Two");
Album three = new Album();
three.setAlbumName("Album Three");
Vector list1 = new Vector();
list1.add(t1);
list1.add(t2);
Vector list2 = new Vector();
list2.add(t1);
Vector list3 = new Vector();
list3.add(t2);
Connection conn = new Connection();
AlbumDAO album = new AlbumDAO();
try {
conn.create();
album.setAssociations(one, list1);
album.setAssociations(two, list2);
album.setAssociations(three, list3);
conn.close();
} catch (HibernateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public class AlbumDAO extends BaseAlbumDAO {
public void setAssociations(Album album, List tracks ) throws HibernateException{
saveAlbum(album);
for(int i = 0; i <tracks.size(); i++){
Track track = (Track) tracks.get(i);
track.setAlbum(album);
saveTrack(track);
}
}
public void saveAlbum(Album album) throws HibernateException{
Session session = Connection.getSession();
Transaction tx = session.beginTransaction();
save(album, session);
session.flush();
tx.commit();
}
public void saveTrack(Track track) throws HibernateException{
Session session = Connection.getSession();
Transaction tx = session.beginTransaction();
save(track, session);
session.flush();
tx.commit();
}
}