AppletTalk.com Forum Index AppletTalk.com
Java discussions newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Bidirectional onetomany relationship doesn't work

 
Post new topic   Reply to topic    AppletTalk.com Forum Index -> JavaBeans
View previous topic :: View next topic  
Author Message
Guest






PostPosted: Wed Feb 14, 2007 9:26 pm    Post subject: Bidirectional onetomany relationship doesn't work Reply with quote



I'm really getting crazy. This is my first experience with EJB and
JBoss (and Hibernate as well actually) but I'm finding a lot of
problems even with the easy stuff!!! I wanna set up a one-to-many
relationship between two entities, but this relationship is supposed
to be bidirectional. I have a "site" entity and a "item" entity and I
want to say that every site can have more than one entity. But I would
like to access the sites linked to an entity from the item object and
viceversa. These are the code fragments:

Site.java

@OneToMany(cascade={CascadeType.ALL},fetch=FetchType.LAZY,mappedBy="site")
public java.util.Collection<Item> getItems(){
return this.items;
}

public void setItems(Collection<Item> items){
this.items=items;
}

Item.java

@ManyToOne
public Site getSite(){
return this.site;
}

public void setSite(Site site){
this.site=site;
}

This operation seems to work actually. Jboss creates a column
"site_id" on the items table and if I run this code

Item item=new Item();
Site site=new Site();
item.setSite(site);
manager.persist(site);
manager.persist(item);

I find that the "items" table contains a record, and it is mapped to
the new record I can find on the "sites" table. The problem is that if
I check the value for site.getItems() after the persist operation I
get a null value. It seems that the opposite "relationship" is not set
properly. I get a problem even if I don't persist the site before the
item and I can't understand why...isn't Hibernate supposed to persist
automatically the site entity by itself? Maybe I'm using badly the
annotation, please help me!!!

Can some of you explain me if it's possible with Hibernate to define a
many-to-many relationship with some relationship attributes and a many-
to-one relationship that connects an entity with itself (in order to
build a tree)?

Thank you in advance for your help

Cold
Back to top
Display posts from previous:   
Post new topic   Reply to topic    AppletTalk.com Forum Index -> JavaBeans All times are GMT
Page 1 of 1

 
Jump to:  
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
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.