Thanks for help. I am using Hibernate with J2EE, not standalone. I do not have access to hibernate configuration file. Everything is annotation.

Duncan Krebs <[EMAIL PROTECTED]> wrote:
Hey,
Here are my two cents. The biggest obstacle with Hibernate is figuring out
how to correctly map the stupid collections and relationships that are
modeled in your object model. I did not take the time to look at all your
code but maybe this will help you out:

Simple situation. You have a class say CarItem and it has an ArrayList of
CarPictures.

Hibernate requirements for mapping collections:
The collection must be an interface. The two most common ones you will use
is either a list or a set. A set garunetess that objects in its collection
are unique, however and kind of annoyinng a set does not gaurnetee or
preserve the order of objects that you add to it. A list does.


Back to the example, writing the java objects is the easy part.

CarItem() {

List carPictures = new ArrayList();

etc...

}

now we want to create a hibernate mapping file for this...

its a list so we use the list element off the of the root .hbm config
element.

Create a list with the name attribute equal to the collection interface
property name.














Wow the more I think about this the more I realize I'm rewriting whats in
the doucmentation. I was just there yesterday go to

http://www.hibernate.org/hib_docs/v3/reference/en/html/collections.html and< BR>setup the examples in your dev environment, its not rocket science once you
get it you'll feel on top of your world. - Duncan























----- Original Message -----
From: "Rafael Nami" <[EMAIL PROTECTED]>
To: "MyFaces Discussion"
Sent: Monday, September 12, 2005 8:31 PM
Subject: Re: Hibernate: 1-to-many --> struggling


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 ver y 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;
> ....
> }
& gt; // 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.
>
&g t; 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
Java - thus in the Server as it is in Palm


Yahoo! for Good
Click here to donate to the Hurricane Katrina relief effort.

Reply via email to