-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 
Author Message
 Post subject: One-To-Many mapping: duplicate
PostPosted: Tue Sep 13, 2005 2:57 am 
Senior
Senior

Joined: Tue Sep 13, 2005 2:01 am
Posts: 137
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 and HSQL db on XP.

Please help! Thank you.

Dave


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 14, 2005 1:10 am 
Senior
Senior

Joined: Tue Sep 13, 2005 2:01 am
Posts: 137
After merge, I found the id of Picture is null. I expect the picture id will be populated from persistence after merge. So this might be the reason Hibernate ignore what in the persistence. How to solve this problem? Is this a bug? Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 14, 2005 4:52 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Read the merge() JavaDoc before using it.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 14, 2005 10:43 pm 
Senior
Senior

Joined: Tue Sep 13, 2005 2:01 am
Posts: 137
emmanuel wrote:
Read the merge() JavaDoc before using it.

Hi emmanuel,
I read javadoc. I still have not figure out how to manage collection for one-to-many mapping. I appreciate it if you can throw me some light.
Suppose A has Many B(s). I am still struggling. When persisting/merging A, B are created, but B does not have Id. Thanks.!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 15, 2005 3:21 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Work on the graph that is returned by the merge operation, not the one you've injected

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 20, 2005 4:50 pm 
Senior
Senior

Joined: Tue Sep 13, 2005 2:01 am
Posts: 137
emmanuel wrote:
Work on the graph that is returned by the merge operation, not the one you've injected

HI emmanuel,
I do not understand. I new to Hibernate. Could you explain? Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 21, 2005 5:40 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
a' = em.merge(a);
work on a', not a

_________________
Emmanuel


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.