-->
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: updating join table in many-to-many relationships
PostPosted: Sat Jul 30, 2005 6:32 am 
Newbie

Joined: Sat Jul 30, 2005 6:27 am
Posts: 2
Hi,

I have a many-to-many relationship between playlist and movie, through a join table playlist_movie. If want to create a new playlist from existing movies, do I need to create a new class PlaylistMovies along with a new hbm table playlistMove.hbm.xml? This class allows me to updated the join table directly without affecting movies.

Is this necessary or is there a better way?

Thanks!

Here's the hbm files of playlist and movies:

<!-- -------------- playlist.hbm.xml -------------- -->
<hibernate-mapping>
<class name="com.magicbillboard.movie.Playlist" table="PLAYLIST">

<id name="id" type="long" column="playlist_id">
<generator class="identity" />
</id>

<!-- 2 association: UserGroup and Movie(s) -->
<many-to-one name="userGroup" outer-join="true" column="usergroup_id" not-null="true"/>

<set name="movies" table="PLAYLIST_MOVIE" lazy="false" cascade="none">
<key column="playlist_id" />
<many-to-many class="com.magicbillboard.movie.Movie" column="movie_id" />
</set>


<!-- properties -->
<property name="name" column="name"/>
<property name="ownershipFlag" column="ownership_flag" type="yes_no"/>
<property name="privateFlag" column="private_flag" type="yes_no"/>
<property name="leafFlag" column="leaf_flag" type="yes_no"/>
<property name="createDate" column="create_dt"/>
<property name="createdBy" column="created_by"/>

</class>
</hibernate-mapping>





<!-- -------------- movie.hbm.xml -------------- -->
<hibernate-mapping>

<class name="com.magicbillboard.movie.Movie" table="MOVIE">

<id name="id" type="long" column="movie_id">
<generator class="identity" />
</id>

<!-- 3 associations: movie metadata, playlists, segments -->
<many-to-one name="movieMetadata" column="movie_metadata_id"/>

<set name="playlists" table="PLAYLIST_MOVIE" lazy="false" inverse="true">
<key column="movie_id" />
<many-to-many class="com.magicbillboard.movie.Playlist" column="playlist_id" />
</set>

<set name="segments" table="SEGMENT" inverse="true" lazy="false" cascade="all">
<key column="movie_id" />
<one-to-many class="com.magicbillboard.movie.Segment" />
</set>

<!-- properties -->
<property name="name" column="name" />
<property name="createDate" column="create_dt" />
<property name="displayTimeSeconds" column="display_time_secs" />
</class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jul 30, 2005 10:59 am 
Senior
Senior

Joined: Thu May 12, 2005 11:40 pm
Posts: 125
Location: Canada
You don't have to have a class modeling the association table if the association table has no other columns. Map the relationship using many-to-many, then

Code:
class Playlist {
  void addMovie(Movie movie) {
    getMovies().add(movie);
    movie.getPlaylists().add(this);
  }
}

class Movie {
  void addPlaylist(Playlist playlist) {
    getPlaylists().add(playlist);
    playlist.getMovies().add(this);
  }
}


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jul 30, 2005 5:25 pm 
Newbie

Joined: Sat Jul 30, 2005 6:27 am
Posts: 2
Thanks. After adding movie to playlist, and adding playlist to movie, do I need to do an explicit update on movie before saving playlists? Or would many-to-many mechanism automatically take care of the update on movie?


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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.