1

I just need to add an entry on a many to many table/object with hibernate (associate a song to a playlist) called SongsPlaylist.

the structure of the tables is so defined: Songs---->SongPlaylist<----Playlists

the mapping files (omitted not important portion of file):

<hibernate-mapping>
    <class name="model.pojo.Songs" table="songs" catalog="dbname" lazy="false">
        <id name="id" type="java.lang.Integer">
            <column name="id" />
            <generator class="identity" />
        </id>
        <set name="songsPlaylists" table="songs_playlist" inverse="true" lazy="true" fetch="select">
            <key>
                <column name="song" not-null="true" unique="true" />
            </key>
            <one-to-many class="model.pojo.SongsPlaylist" />
        </set>
    </class>
</hibernate-mapping>
<!-- Generated 24-set-2014 8.40.14 by Hibernate Tools 3.6.0 -->
<hibernate-mapping>
    <class name="model.pojo.SongsPlaylist" table="songs_playlist" catalog="dbname" lazy="false">
        <id name="id" type="java.lang.Integer">
            <column name="id" />
            <generator class="identity" />
        </id>
        <many-to-one name="songs" class="model.pojo.Songs" fetch="select">
            <column name="song" not-null="true" unique="true" />
        </many-to-one>
        <many-to-one name="playlists" class="model.pojo.Playlists" fetch="select">
            <column name="playlist" not-null="true" unique="true" />
        </many-to-one>
    </class>
</hibernate-mapping>

<!-- Generated 24-set-2014 8.40.14 by Hibernate Tools 3.6.0 -->
<hibernate-mapping>
    <class name="model.pojo.Playlists" table="playlists" catalog="dbname" lazy="false">
        <id name="id" type="java.lang.Integer">
            <column name="id" />
            <generator class="identity" />
        </id>
        <set name="songsPlaylists" table="songs_playlist" inverse="true" lazy="true" fetch="select">
            <key>
                <column name="playlist" not-null="true" unique="true" />
            </key>
            <one-to-many class="model.pojo.SongsPlaylist" />
        </set>
    </class>
</hibernate-mapping>

the pojo classes:

public class SongsPlaylist  implements java.io.Serializable {
     private Integer id;
     private Songs songs;
     private Playlists playlists;
     (...)
}

public class Songs  implements java.io.Serializable {
     private Integer id;
     private Set songsPlaylists = new HashSet(0);
     (...)
}

public class Playlists  implements java.io.Serializable {
     private Integer id;
     private Set songsPlaylists = new HashSet(0);
     (...)
}

this is my function

            s = new HibernateUtil().getSessionFactory().openSession();
            transaction = s.beginTransaction();
            (...)
                q = s.createQuery("select sp from SongsPlaylist sp where sp.playlists = :p");
                q.setParameter("p", p);
                sp = q.list();
                //already exists a song binded to that playlist
            if(sp.size()>0){
                Set<Playlists> sP = sp.get(0).getPlaylists().getSongsPlaylists();
                sP.add(p);
                song.setSongsPlaylists(sP);
                s.save(s);
            }else{
                //or it is in another playlist...
                q = s.createQuery("select sp from SongsPlaylist sp where sp.songs = :s");
                q.setParameter("s", song);
                sp = q.list();
                if(sp.size()>0){
                    Set<Songs> sP = sp.get(0).getSongs().getSongsPlaylists();
                    sP.add(song);
                    p.setSongsPlaylists(sP);
                    s.save(p);
                }else{
                    s.save(new SongsPlaylist(song, p));
                }
            }
            }
            transaction.commit();
            s.close();

and it stucks on the transaction.commit(); (last lines) with

org.hibernate.HibernateException: Found shared references to a collection: 
model.pojo.Songs.songsPlaylists

it looks like I do something wrong when persisting a Set<Playlists (the same when I try to do that with Set<Songs>) on a persistent object. what 's the best practice for this case in order to insert a "SongPlaylist" and to avoid the hibernate exception mentioned?

Dario Rusignuolo
  • 2,090
  • 6
  • 38
  • 68

1 Answers1

0

found a solution...

in the else block (instead of what I wrote), now is:

Set<Playlists> sP = song.getPlaylistses(); // terrific netbeans naming when automatic pojo mapping :-)
sP.add(p);
song.setPlaylistses(sP);
s.save(song);

and just a side bug, not related to this one, but

SongsPlaylist should not be as hbm file. hibernate mapping should be instead

<hibernate-mapping>
    <class name="model.pojo.Songs" table="songs" catalog="dbname" lazy="false">
        <id name="id" type="java.lang.Integer">
            <column name="id" />
            <generator class="identity" />
        </id>
        (...)
        <set name="playlistses" table="songs_playlist" inverse="false" lazy="false" fetch="select">
            <key>
                <column name="song" not-null="true" />
            </key>
            <many-to-many entity-name="model.pojo.Playlists">
                <column name="playlist" not-null="true" />
            </many-to-many>
        </set>
    </class>
</hibernate-mapping>


<hibernate-mapping>
    <class name="model.pojo.Playlists" table="playlists" catalog="dbname" lazy="false">
        <id name="id" type="java.lang.Integer">
            <column name="id" />
            <generator class="identity" />
        </id>
        <set name="songses" table="songs_playlist" inverse="false" lazy="false" fetch="select">
            <key>
                <column name="playlist" not-null="true" />
            </key>
            <many-to-many entity-name="model.pojo.Songs">
                <column name="song" not-null="true" />
            </many-to-many>
        </set>
    </class>
</hibernate-mapping>
Dario Rusignuolo
  • 2,090
  • 6
  • 38
  • 68