Well maybe the original guy did not use session.saveOrUpdate
on the parent element...

I usually deal with 1:n which usually are far less problematic
then m:n (which is the case where you usually sink 90% of the dev time
banging your head and cursing hibernate left and right)
is to have a 1:n rel, with a set mapping and to use cascade all
with a save on the root element.
That has worked best.

The crux of hibernate is, you have so many ways to solve things
for everything around 100 ways, but 90 of them fail with either no error or cryptic error messages once you have to apply them, while there still is a huge lack of best practices especially in the areas
many-to-many and one-to-many

I will give an example:
Implicit mapping, not working if you have additional data in your many to many binding table, now how do you resolve that
you can resolve it via an explicit binding object declaration,
the doc is very vague on this approach,

you finally get it up and running, loading works, but deleting utterly fails with a cascade error, no real help in the forum, after banging your head for straight 8 hours you finally know why this happens,
you delete the objects with an explicit delete
and regenerate the binding objects, but the binding table does not have
a surrogate, you do that within the same transaction.
What wouldnt pose a problem in straight JDBC because you clear the m:n elements out of the many to many table before setting them anew, causes hibernate to choke. Solution, set the relational binding to lazy thus the objects do not get marked
as deleted before loading the parent object and thus the setting
works again.

It is cases like that, which are very commmon where all the documentation unfortunately utterly fails and where you can sink hours into hibernate.

Hibernate to my taste lacks a high level layer which resolves such cases automatically, some kind of hibernatetool.resolveManyToManyBinding(BindingList) would have helped a lot in this case.

Before Hibernate 2 it also was the session handling which lacked those tools, that fortunately is resolved now.



Rafael Nami wrote:
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




Reply via email to