title: How to get counts of all objects in second level of one-to-many relationship? [Java]
body: <p>I have three entities: Factory, Order and Item in one-to-many relationship like this:</p>
  
  <p>Factory has many Orders<br>
  Order has many Items</p>
  
  <p>I need to get total number/count of all Items for all Orders registered under selected Factory.
  I did it like this...</p>
  
  <pre><code>    Factory factory = factoryService.findOne(factoryId);
      List&lt;Order&gt; orders = factory.getOrders();
      int count=0;
      for(Order o: orders){
      if (o.isDeleted()!=true)
      int numberOfItems = o.getItems().size();
      count+= numberOfItems;
      }
  }
  </code></pre>
  
  <p>I did similar thing with 'soft' delete - when I mark Factory as deleted I needed to find all Orders and related Items for selected Factory and mark them deleted as well.</p>
  
  <p>It is all working fine but I have just test records in database, and I'm not sure this is way to do it if you have large database with many records...
  So I was wondering is there some elegant and more efficient way to do this.</p>
  
  <p>I'm working with Java Spring Boot 
  and PostgreSQL</p>