Hi All,
I'm having a problem with an ordered list and would greatly appreciate any help.
Configuration:
An AlbumGroup contains a list of Albums. It is not a bi-directional relationship.
Code:
<list name="albums" cascade="all" lazy="false" inverse="false">
<key column="ALBUMGROUP_ID" not-null="true"/>
<list-index column="ALBUM_POSITION"/>
<one-to-many class="com.nim.domain.Album"/>
</list>
The problem:
I add a new Album to a new AlbumGroup and saved the AlbumGroup. After saving the AlbumGroup, the album's position (list-index) was 4. I.e. there were 4 elements in the album List: null, null, null, myAlbum. My debug statements prior to saving the AlbumGroup using hibernate shows that there was only 1 Album in the albums list.
The sql trace shows that the Album is saved correctly then the index is updated 3 times:
Code:
Hibernate: update ALBUM set ALBUMGROUP_ID=?, ALBUM_POSITION=? where ALBUM_ID=?
TRACE - LongType - binding '1' to parameter: 1
TRACE - IntegerType - binding '0' to parameter: 2
TRACE - LongType - binding '1' to parameter: 3
Hibernate: update ALBUM set ALBUMGROUP_ID=?, ALBUM_POSITION=? where ALBUM_ID=?
TRACE - LongType - binding '1' to parameter: 1
TRACE - IntegerType - binding '1' to parameter: 2
TRACE - LongType - binding '1' to parameter: 3
Hibernate: update ALBUM set ALBUMGROUP_ID=?, ALBUM_POSITION=? where ALBUM_ID=?
TRACE - LongType - binding '1' to parameter: 1
TRACE - IntegerType - binding '2' to parameter: 2
TRACE - LongType - binding '1' to parameter: 3
Hibernate: update ALBUM set ALBUMGROUP_ID=?, ALBUM_POSITION=? where ALBUM_ID=?
TRACE - LongType - binding '1' to parameter: 1
TRACE - IntegerType - binding '3' to parameter: 2
TRACE - LongType - binding '1' to parameter: 3
My test code:
Code:
albumGroup = createAlbumGroup();
albumGroupDao.save(albumGroup);
assertTrue("albums size:" + albumGroup.getAlbums().size(), albumGroup.getAlbums().isEmpty());
album = createAlbum();
albumGroup.getAlbums().add(album);
assertTrue("albums size:" + albumGroup.getAlbums().size(), albumGroup.getAlbums().size() == 1);
albumGroupDao.save(albumGroup);
I am using Hibernate version 3.2.7.ga and hsqldb.
Thanks in advance for your help.
Nim