简体   繁体   English

多对多集-添加条目-org.hibernate.HibernateException:找到对集合的共享引用:

[英]many to many set - add entry - org.hibernate.HibernateException: Found shared references to a collection:

I just need to add an entry on a many to many table/object with hibernate (associate a song to a playlist) called SongsPlaylist . 我只需要在hibernate多对多表/对象上添加一个条目(将歌曲与播放列表关联),即SongsPlaylist

the structure of the tables is so defined: Songs---->SongPlaylist<----Playlists 表格的结构是这样定义的: 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: pojo类:

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(); 并且卡在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. 在持久对象上持久保存Set<Playlists (当我尝试使用Set<Songs>进行相同操作时),看起来好像做错了。 what 's the best practice for this case in order to insert a "SongPlaylist" and to avoid the hibernate exception mentioned? 在这种情况下,为了插入“ SongPlaylist”并避免提及休眠异常的最佳实践是什么?

found a solution... 找到了解决方案...

in the else block (instead of what I wrote), now is: 在else块中(而不是我写的),现在是:

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. SongsPlaylist不应作为hbm文件。 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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 引起:org.hibernate.HibernateException:找到对集合的共享引用 - Caused by: org.hibernate.HibernateException: Found shared references to a collection 找到对集合 org.hibernate.HibernateException 的共享引用 - Found shared references to a collection org.hibernate.HibernateException spring-data-jpa:找到对集合 org.hibernate.HibernateException 的共享引用 - spring-data-jpa : Found shared references to a collection org.hibernate.HibernateException org.hibernate.HibernateException:找不到会话 - org.hibernate.HibernateException: No Session found Hibernate在entitymanager.flush()上抛出“ org.hibernate.HibernateException:找到相同集合的两个表示形式” - Hibernate throws “org.hibernate.HibernateException: Found two representations of same collection” on entitymanager.flush() org.hibernate.HibernateException:/hibernate.cfg.xml找不到 - org.hibernate.HibernateException: /hibernate.cfg.xml not found Hibernate 4:org.hibernate.HibernateException:当前会话找不到会话 - Hibernate 4 : org.hibernate.HibernateException: No Session found for current thread org.hibernate.HibernateException错误 - org.hibernate.HibernateException error org.hibernate.HibernateException:collection与任何会话无关 - org.hibernate.HibernateException: collection is not associated with any session org.hibernate.HibernateException:必须显式设置休眠方言 - org.hibernate.HibernateException: Hibernate Dialect must be explicitly set
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM