-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Newbie question: many-to-many bidirectional mapping
PostPosted: Thu Apr 27, 2006 11:38 am 
Newbie

Joined: Wed Apr 19, 2006 7:54 am
Posts: 6
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]


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 28, 2006 12:46 am 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Wow, never noticed that before. Yes, that's an error in the docs. You need a collection, not a join: <set> is probably what you want.

I double-checked this with HIA, and it's correct in the book.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 28, 2006 6:19 am 
Newbie

Joined: Wed Apr 19, 2006 7:54 am
Posts: 6
That works fine now. I've just had to restrict the API so that Tracks can only have one parent album.

I see the documentation got fixed before I had the chance to send an e-mail :)

Cheers,

Dan.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.