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