hibernate - @OneToMany save duplicate records -


i have 3 tables: playlist , advertisement , playlistadmap (join table)

1- playlist:

@entity @table(name = "playlist", catalog = "advertisedb") public class playlist implements java.io.serializable {       @onetomany(fetch = fetchtype.lazy, cascade = cascadetype.all)     private set<playlistadmap> playlistadmaps = new hashset<playlistadmap>(0);  } 

2- advertisement:

@entity @table(name = "advertisement", catalog = "advertisedb") public class advertisement implements java.io.serializable {       @onetomany(fetch = fetchtype.lazy, mappedby = "advertisement")     private set<playlistadmap> playlistadmaps = new hashset<playlistadmap>(0);  } 

3- playlistadmap:

@entity @table(name = "playlist_ad_map", catalog = "advertisedb") public class playlistadmap implements java.io.serializable {       @manytoone(fetch = fetchtype.eager)     @joincolumn(name = "fk_advertisement", referencedcolumnname = "pkid", nullable = false)     private advertisement advertisement;      @manytoone(fetch = fetchtype.eager)     @joincolumn(name = "fk_playlist", referencedcolumnname = "pkid", nullable = false)     private playlist playlist;  } 

lets suppose playlist 1 has 3 related ads in database:

(1,1)(1,2)(1,3) if trying add 3 more ads old ones list updated contain (1,1)(1,2)(1,3) (1,4)(1,5)(1,6) records in db be:

(1,1)(1,2)(1,3) (1,1)(1,2)(1,3)(1,4)(1,5)(1,6)

do have remove duplicate records manually deleting records insert new ones ? or there's solution in hibernate issue.

your mapping wrong: defined 2 different associations between playlist , playlistadmap instead of single bidirectional one, because forgot mappedby attribute in playlist.

and no, don't have remove , re-add existing ads set. wouldn't change anything. add 3 new ads set, make sure other side of association initialized correctly, , should fine.

also note cascading not related problem. cascading means: when x on entity, x on associated entities. you're creating association between entities here. there's nothing cascade.


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -