aurelije (6) [Avatar] Offline
#1
Hi,

I am reading chapter 2.2.4 and I understand that it is important to have bidirectional association always set on both sides.

You are giving an example of that in listing 2.3 where addBid method in class Item sets association on both sides. But you should give listing of Bid class, where setItem method should also perform setting bidirectional association.

In listing 2.4 Bid constructor is setting connection with Item but opposite connection is not set so Item's collection of bids is not updated.

Maybe the code that will come with the book would make this much clear, but for now those examples are fuzzy.

Keep a good work,
Zlatan Kadragic
Christian Bauer (56) [Avatar] Offline
#2
Re: 2.2.4 Implementing POJO associations
> You are giving an example of that in listing 2.3
> where addBid method in class Item sets association on
> both sides. But you should give listing of Bid class,
> where setItem method should also perform setting
> bidirectional association.

No, that would be an endless loop. The Bid#setItem() method should probably be package-visible, so mutations of the relationship is only possible with the public Item#addBid() method. I've added a sentence to clarify this.

> In listing 2.4 Bid constructor is setting connection
> with Item but opposite connection is not set so
> Item's collection of bids is not updated.

I've added this to the example and explained why this is actually not a good idea.