Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Java Beans > Bidirectional o...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 1 Topic 1288 of 1397
Post > Topic >>

Bidirectional onetomany relation****p doesn't work

by cold80@[EMAIL PROTECTED] Feb 14, 2007 at 07:26 AM

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
relation****p between two entities, but this relation****p 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

@[EMAIL PROTECTED]
(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

@[EMAIL PROTECTED]
    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 "relation****p" 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 relation****p with some relation****p attributes and a many-
to-one relation****p that connects an entity with itself (in order to
build a tree)?

Thank you in advance for your help

Cold
 




 1 Posts in Topic:
Bidirectional onetomany relationship doesn't work
cold80@[EMAIL PROTECTED]   2007-02-14 07:26:18 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Mon Oct 13 3:53:43 CDT 2008.