Alternative: using a convienience method
Below is a method added to Order to provide a way to manage
the Order - OrderDetail relationship. This is a method suggested by some but I
am not convinced about its merits.
It has potential issues with NullPointerException and firing Lazy Loading
so if you go down this route you need to think about those issues.
...
@Entity
class Order {
...
public void addOrderDetail(OrderDetail line) {
if (line == null) {
throw new IllegalArgumentException("line is null?");
}
if (line.getOrder() != null) {
line.getOrder().getDetails().remove(line);
}
line.setOrder(this);
details.add(childCategory);
}
In adding such a method some may suggest that the setDetails() method become private.
It should be noted that if setDetails is private then you can not control the construction
of the details. Specifically you lose the ability to control the details query yourself
and set those results into the Order.
Order order = ...
// set the details using your own query
FindByPredicates find = new FindByPredicates();
find.setBeanType(OrderDetail.class);
find.add("order_id", find.EQ, order.getId());
find.addAnd();
find.add("status_code", find.EQ, "DISPUTE");
List disputedDetails = Ebean.findList(find);
...
order.setDetails(disputedDetails);
|