I'm not an expert in this, but I think if you use another type of collection, like a Set or a List, this behavior won't occur. I usually use Sets to many to many and Lists to bidirecional relations.
Hope that it helps Rafael Mauricio Nami 2005/9/12, Dave <[EMAIL PROTECTED]>: > I am struggling with Hibernate 1-to-many mapping(collection). > Could someone tell me how to manage the collection of one-to-many mapping? a > little code will be very helpful. > > I have a very simple one-to-many relationship: an Item has many Picture(s). > > ENTITY -- Item > @... > public class item { > private Collection pictures; > private Integer id; > > // getter/setter for id and @Id(...) > > > @OneToMany(cascade = {CascadeType.MERGE}, fetch = FetchType.EAGER, > mappedBy="item") > public Collection getPictures() { > return pictures; > } > > public void setPictures(Collection pictures) { > this.pictures = pictures; > } > > public void addPicture(Picture p) { > > if (pictures == null) > pictures = new ArrayList(); > pictures.add(p); > > } > } > > ENTITY Picture -- > @ .... > public class Picture { > private Item item; > private Integer id; > > // constructor > public Picture(Item item, ....) { > this.item = item; > .... > } > // getter/setter for id and @Id(...) > > @ManyToOne > @JoinColumn(name="itemId") > public Item getItem() { > return item; > } > > public void setItem(Item item) { > this.item = item; > } > } > > I create an Item in persistence without any pictures. Then use a session > bean to add pictures to the item. > > Session Bean -- > > @Stateless > public class ItemManagerBean implements ItemManager { > > public void addPictureToItem(Item item, Picture p) { > item.addPicture(p); > em.merge(item); > } > > } > > The merge() ignores those pictures that are already in persistence. > Add picture P1: ---> persistence: P1, ( great!) > Add picture P2 ---> persistence: P1, P1, P2 (P1 duplicated) > Add picture P3 ---> persistence: P1, P1, P2, P1, P2, P3 > > Obviously, the merge() ignored what are already in persistence. All pictures > has Id. EM.merge() should be able to know P1 is already in persistence when > adding P2. > > I f I do not call merge(), nothing will be persisted. > > I do this from Web GUI. Transaction is thread-based.(default) > > Do I miss anything? or Is this a Hibernate bug ? > > > ----------------- > > I tried one thing: Create a picture in persistence using another session > bean. Then use em.refresh(item). First time it works, and I can see the > picture from item.getPictures(). > Then I create another picture the same way, and call em.refresh(item) again, > but this time item.getPictures() does not have the second picture. > Not that I have changed the Cascade type to REFRESH. > > Basically what is the way to manage the collection in one-to-many mapping? > > I am using JBoss 4.0.3RC1 on XP. > > Please help! Thank you. > > Dave > > > > __________________________________________________ > Do You Yahoo!? > Tired of spam? Yahoo! Mail has the best spam protection around > http://mail.yahoo.com -- Java - Assim no Server como no Palm <i>Java - thus in the Server as it is in Palm