Hibernate version:
3.1
Mapping documents:
Code:
<hibernate-mapping default-cascade="save-update">
<class name="<my-path>.Album" table="albums">
<id name="albumId" type="long">
<column name="album_id"/>
<generator class="sequence">
<param name="sequence">album_id</param>
</generator>
</id>
<!-- <SNIP> PROPERTIES -->
<list name="tracks" table="album_tracks" lazy="true" cascade="save-update,delete, delete-orphan">
<key column="album_id"/>
<list-index column="track_position"/>
<many-to-many class="<my-path>.Track" column="track_id"/>
</list>
</class>
</hibernate-mapping>
<hibernate-mapping>
<hibernate-mapping>
<class name="com.mediagraft.podsplice.domain.Track" table="tracks">
<id name="trackId" type="long">
<column name="track_id"/>
<generator class="sequence">
<param name="sequence">track_id</param>
</generator>
</id>
<join table="album_tracks"
inverse="true"
optional="true">
<key column="track_id"/>
<many-to-one name="album"
column="album_id"
not-null="true"/>
</join>
<!-- <SNIP> PROPERTIES -->
</class>
</hibernate-mapping>
Schema:
Code:
-- Albums
CREATE TABLE albums (
album_id BIGINT NOT NULL, -- Album ID generated from sequence
title VARCHAR(50), -- title of album
description VARCHAR(500), -- description of album
date TIMESTAMP, -- when album info was last updated
artist_id BIGINT NOT NULL, -- any artist info
composer_id BIGINT NOT NULL, -- any composer info
PRIMARY KEY(album_id),
CONSTRAINT albums_unique_album UNIQUE(title, artist_id),
CONSTRAINT albums_fkey_artist_id FOREIGN KEY(artist_id) REFERENCES artists ON DELETE CASCADE,
CONSTRAINT albums_fkey_composer_id FOREIGN KEY(composer_id) REFERENCES composers ON DELETE CASCADE
) WITHOUT OIDS;
-- Tracks
CREATE TABLE tracks (
track_id BIGINT NOT NULL, -- ID of music track generated from sequence
track_number SMALLINT NOT NULL, -- track number
title VARCHAR(50), -- title of music track
description VARCHAR(500), -- description of music track
duration BIGINT, -- duration of music track in seconds
genre_id BIGINT NOT NULL, -- id of genre
artist_id BIGINT NOT NULL, -- any artist info
composer_id BIGINT NOT NULL, -- any composer info
date TIMESTAMP, -- when track was last updated
file_location VARCHAR(300) NOT NULL, -- location of associated file
PRIMARY KEY(track_id),
CONSTRAINT tracks_fkey_genre_id FOREIGN KEY(genre_id) REFERENCES genres ON DELETE CASCADE,
CONSTRAINT tracks_fkey_artist_id FOREIGN KEY(artist_id) REFERENCES artists ON DELETE CASCADE,
CONSTRAINT tracks_fkey_composer_id FOREIGN KEY(composer_id) REFERENCES composers ON DELETE CASCADE
) WITHOUT OIDS;
-- Albums to tracks
CREATE TABLE album_tracks (
album_id BIGINT NOT NULL, -- album ID
track_id BIGINT NOT NULL, -- track ID
track_position INTEGER NOT NULL, -- position of track on album
UNIQUE(track_id), -- ensure tracks can only belong to one album
CONSTRAINT album_tracks_fkey_album_id FOREIGN KEY(album_id) REFERENCES albums ON DELETE CASCADE,
CONSTRAINT album_tracks_fkey_track_id FOREIGN KEY(track_id) REFERENCES tracks ON DELETE CASCADE
) WITHOUT OIDS;
Name and version of the database you are using:PostgreSQL 8.1
Hey,
I'm fairly new to Hibernate so please be patient.
I'm doing a bidirectional mapping between a table of albums and tracks where the tracks can also be individual tracks which are not associated with any tracks (we don't want to use NULL foreign keys in the schema).
I've tried using the mapping suggested in the hibernate 3 docs 7.5.1:
Code:
<class name="Person">
<id name="id" column="personId">
<generator class="native"/>
</id>
<set name="addresses"
table="PersonAddress">
<key column="personId"/>
<many-to-many column="addressId"
unique="true"
class="Address"/>
</set>
</class>
<class name="Address">
<id name="id" column="addressId">
<generator class="native"/>
</id>
<join table="PersonAddress"
inverse="true"
optional="true">
<key column="addressId"/>
<many-to-one name="person"
column="personId"
not-null="true"/>
</join>
</class>
create table Person ( personId bigint not null primary key )
create table PersonAddress ( personId bigint not null, addressId bigint not null primary key )
create table Address ( addressId bigint not null primary key )
With a join back to the parent person. However using the join element throws a SAX exception on the main class element - is there an error in the documentation? I've checked the DTD and it seems okay.
I've placed my mappings at the top. Any help would be great,
Thanks,
Dan.
[/code]