Using ForeignCollection

My object contains the following private attribute ForeignCollection :

 @ForeignCollectionField private ForeignCollection<Order> orderCollection; private List<Order> orderList; 

What is the best way, or the usual way, to avoid using a ForeignCollection ? Is there any tidy way to return Collections data to the caller?

What does the following method look like? It allows the caller to access data through a List . Would you recommend doing this?

 public List<Order> getOrders() { if (orderList == null) { orderList = new ArrayList<Order>(); for (Order order : orderCollection) { orderList.add(order); } } return orderList; } 
+4
source share
1 answer

If you can change the signature to Collection rather than List , you can try using Collections.unmodifiableCollection () .

 public Collection<Order> getOrders() { return Collections.unmodifiableCollection(orderCollection); } 

Otherwise, your approach to using a lazy member variable is fine (unless you need synchronization). Also note that you can simply use the ArrayList constructor to copy the values ​​from the original collection:

 orderList = new ArrayList<Order>(orderCollection); 
+3
source

All Articles