-->
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.  [ 2 posts ] 
Author Message
 Post subject: why is it best to make a one to many bidirectional?
PostPosted: Sun Nov 22, 2009 12:53 am 
Beginner
Beginner

Joined: Wed Mar 19, 2008 12:10 am
Posts: 36
im using jpa and i'm wondering why its preferred to make a one to many bidirectional on the many to one side?


Top
 Profile  
 
 Post subject: Re: why is it best to make a one to many bidirectional?
PostPosted: Sun Nov 22, 2009 2:27 am 
Beginner
Beginner

Joined: Wed Mar 19, 2008 12:10 am
Posts: 36
I'm reading in the java persistence book the following:
Quote:
At runtime, there are two different in-memory representations of the same foreign
key value: the item property of Bid and an element of the bids collection
held by an Item. Suppose the application modifies the association, by, for example,
adding a bid to an item in this fragment of the addBid() method:
bid.setItem(item);
bids.add(bid);
This code is fine, but in this situation, Hibernate detects two changes to the inmemory
persistent instances. From the point of view of the database, only one
value has to be updated to reflect these changes: the ITEM_ID column of the
BID table.
Hibernate doesn’t transparently detect the fact that the two changes refer to
the same database column, because at this point you’ve done nothing to indicate
that this is a bidirectional association. In other words, you’ve mapped the same
column twice (it doesn’t matter that you did this in two mapping files), and Hibernate
always needs to know about this because it can’t detect this duplicate automatically
(there is no reasonable default way it could be handled).







this is the annotations it ends up using for the Item side:
Code:
....

public class Item {
...
@OneToMany(mappedBy = "item")
private Set<Bid> bids = new HashSet<Bid>();
...
}


but i'm still not clear on what this means.

my understanding is that i need to make it bidirectional otherwise if i try to do:

bid.setItem(item)
item.getbids().add(bid)
session.save()

it persists nothing? i thought it would at least set up the association so that i could call item.getBids() and properly return all the bid objects, and that trying to do a bid.getItem() wouldnt work if it wasnt mapped there? I'm using annotations so any explanations i'd prefer to be written using those. thanks


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.