我只需要在hibernate
(将歌曲与播放列表相关联)的多个表/对象上添加一个名为SongsPlaylist
的条目。
表的结构如此定义:Songs---->SongPlaylist<----Playlists
映射文件(省略不是文件的重要部分):
<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>
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);
(...)
}
这是我的功能
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();
并且它与
一起停留在transaction.commit();
(最后一行)
org.hibernate.HibernateException: Found shared references to a collection:
model.pojo.Songs.songsPlaylists
在持久对象上持久化Set<Playlists
(当我尝试用Set<Songs>
执行此操作时相同)时,我似乎做错了。为了插入“SongPlaylist”并避免提到的hibernate异常,这个案例的最佳做法是什么?
答案 0 :(得分:0)
找到了解决方案......
在else块(而不是我写的)中,现在是:
Set<Playlists> sP = song.getPlaylistses(); // terrific netbeans naming when automatic pojo mapping :-)
sP.add(p);
song.setPlaylistses(sP);
s.save(song);
只是一个与这个没有关系的漏洞,但是
SongsPlaylist不应该像hbm文件一样。 hibernate映射应该是
<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>