title: JPA fetching too many entries
body: <p>I have a Customer Entity with a OneToMany relationship to an Invoice Entity.</p>
  
  <p>In plain old sql i can do "select customer_name,customer_age,[some other fields] from customer, invoice where ... [put some filtering here]", which gets me exactly one record with the fields i need.</p>
  
  <p>In JPA i use "select c from Customer c join c.invoiceCollection where ... [same filtering as above]"</p>
  
  <p>This works, but i get the Customer entity with <em>all</em> its associated invoices.
  This is nonsense, because i pull a huge amount of (invoices) data from the database, which i do not need. I need only my customer data with exactly one invoice, as specified in the where clause.</p>
  
  <p>To make things worse, i have to loop over the Customer.invoiceCollection in order to find the one specific invoice needed. This costs me even more time plus it exposes my "where" clause to the middle-tier.</p>
  
  <p>Question: is there a JPA select syntax which fetches exactly one record from a one-to-many relationship, as defined in the where clause?</p>
  
  <p>Things tried so far:
  a) lazy loading. This does not work, throws an exception whenever i try to access Customer.invoiceCollection.
  Even if it worked, i'd get a Collection with some 1000 entries, which i do not need.</p>
  
  <p>b) changed my jpa statement to "select c,i from Customer c join c.invoiceCollection i where ...". This returns me an array of objects, which i have to manually map to a Customer / Invoice entity.
  It works, but it makes the ORM philosophy obsolete. If i perform all the mapping from relational database records/fields to java objects manually in my code, why do i need JPA?</p>