K
kevkev
I'm trying to work with an existing database schema and am having
difficulty with the mapping documents to allow me to persist duplicate
data to a table.
I have an ALBUM and a TRACK table. The business logic tells me that an
ALBUM can have many TRACK's and a track can be in many ALBUM's.
So what I would eventually like to have is something like
ALBUM Table
ALBUM_ID | ALBUM_NAME
------------------------------
1 | Album One
------------------------------
2 | Album Two
------------------------------
3 | Album Three
and
TRACK Table
TRACK_ID | NAME | ALBUM_ID
------------------------------------------
1 | Track One | 1
------------------------------------------
2 | Track Two | 1
------------------------------------------
3 | Track One | 2
------------------------------------------
4 | Track Two | 3
My mapping document is using a Set and looks as follows.
<class name="Album" table="ALBUM">
<id name="id" type="int" column="ALBUM_ID">
<meta attribute="scope-set">protected</meta>
<generator class="native"/>
</id>
<property name="albumName" column="NAME" type="string"/>
<set
name="albumTrack"
lazy="true"
cascade="save-update">
<key column="ALBUM_ID"/>
<one-to-many class="com.kbilly.hibernate.Track"/>
</set>
</class>
I know that a Set won't do though as it doesn't allow duplicates and
this is why I get.
TRACK Table
TRACK_ID | NAME | ALBUM_ID
------------------------------------------
1 | Track One | 3
------------------------------------------
2 | Track Two | 2
I've tried to use <idbag> but I can only get this working with an
association table, ALBUM_TRACK for instance. But the database schema
I'm working with doesn't have this join table.
So my question is how do I implement <idbag> to allow duplicates in the
the TRACK table without using a join table?
The other option I've tried is to have a bag in the Album.hbm.xml and
many-to-one in the Track.hbm.xml as below.
<class name="Track" table="TRACK">
<id name="id" type="int" column="TRACK_ID">
<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"
class="com.chello.mdr.mdrhibernate.Album"
not-null="true"/>
</class>
<class name="Album" table="ALBUM">
<id name="id" type="int" column="ALBUM_ID">
<meta attribute="scope-set">protected</meta>
<generator class="native"/>
</id>
<property name="albumName" column="NAME" type="string"/>
<bag
name="albumTrack"
inverse="true">
<key column="ALBUM_ID"/>
<many-to-many class="com.chello.mdr.mdrhibernate.Track"/>
</bag>
</class>
Even with this though I still get updates instead of inserts for the
duplicates.
Hibernate: insert into ALBUM (NAME) values (?)
Hibernate: insert into TRACK (NAME, ALBUM_ID) values (?, ?)
Hibernate: insert into TRACK (NAME, ALBUM_ID) values (?, ?)
Hibernate: insert into ALBUM (NAME) values (?)
Hibernate: update TRACK set NAME=?, ALBUM_ID=? where TRACK_ID=?
Hibernate: insert into ALBUM (NAME) values (?)
Hibernate: update TRACK set NAME=?, ALBUM_ID=? where TRACK_ID=?
And again this gives...
TRACK Table
TRACK_ID | NAME | ALBUM_ID
------------------------------------------
1 | Track One | 3
------------------------------------------
2 | Track Two | 2
Any help would be really appreciated with this.
Regards,
Kevin
difficulty with the mapping documents to allow me to persist duplicate
data to a table.
I have an ALBUM and a TRACK table. The business logic tells me that an
ALBUM can have many TRACK's and a track can be in many ALBUM's.
So what I would eventually like to have is something like
ALBUM Table
ALBUM_ID | ALBUM_NAME
------------------------------
1 | Album One
------------------------------
2 | Album Two
------------------------------
3 | Album Three
and
TRACK Table
TRACK_ID | NAME | ALBUM_ID
------------------------------------------
1 | Track One | 1
------------------------------------------
2 | Track Two | 1
------------------------------------------
3 | Track One | 2
------------------------------------------
4 | Track Two | 3
My mapping document is using a Set and looks as follows.
<class name="Album" table="ALBUM">
<id name="id" type="int" column="ALBUM_ID">
<meta attribute="scope-set">protected</meta>
<generator class="native"/>
</id>
<property name="albumName" column="NAME" type="string"/>
<set
name="albumTrack"
lazy="true"
cascade="save-update">
<key column="ALBUM_ID"/>
<one-to-many class="com.kbilly.hibernate.Track"/>
</set>
</class>
I know that a Set won't do though as it doesn't allow duplicates and
this is why I get.
TRACK Table
TRACK_ID | NAME | ALBUM_ID
------------------------------------------
1 | Track One | 3
------------------------------------------
2 | Track Two | 2
I've tried to use <idbag> but I can only get this working with an
association table, ALBUM_TRACK for instance. But the database schema
I'm working with doesn't have this join table.
So my question is how do I implement <idbag> to allow duplicates in the
the TRACK table without using a join table?
The other option I've tried is to have a bag in the Album.hbm.xml and
many-to-one in the Track.hbm.xml as below.
<class name="Track" table="TRACK">
<id name="id" type="int" column="TRACK_ID">
<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"
class="com.chello.mdr.mdrhibernate.Album"
not-null="true"/>
</class>
<class name="Album" table="ALBUM">
<id name="id" type="int" column="ALBUM_ID">
<meta attribute="scope-set">protected</meta>
<generator class="native"/>
</id>
<property name="albumName" column="NAME" type="string"/>
<bag
name="albumTrack"
inverse="true">
<key column="ALBUM_ID"/>
<many-to-many class="com.chello.mdr.mdrhibernate.Track"/>
</bag>
</class>
Even with this though I still get updates instead of inserts for the
duplicates.
Hibernate: insert into ALBUM (NAME) values (?)
Hibernate: insert into TRACK (NAME, ALBUM_ID) values (?, ?)
Hibernate: insert into TRACK (NAME, ALBUM_ID) values (?, ?)
Hibernate: insert into ALBUM (NAME) values (?)
Hibernate: update TRACK set NAME=?, ALBUM_ID=? where TRACK_ID=?
Hibernate: insert into ALBUM (NAME) values (?)
Hibernate: update TRACK set NAME=?, ALBUM_ID=? where TRACK_ID=?
And again this gives...
TRACK Table
TRACK_ID | NAME | ALBUM_ID
------------------------------------------
1 | Track One | 3
------------------------------------------
2 | Track Two | 2
Any help would be really appreciated with this.
Regards,
Kevin