title: JPA OneToMany Relationship with Distinct
body: <p>I have 3 tables and 3 subsequent entities, which are Companies which has a OneToMany with Users which has a OneToMany with Shirts.</p>
  
  <p>So just to be clear each Company has many Users and each User has many Shirts.</p>
  
  <p>Company has 2 columns CompanyId and CompanyName
  Users has 3 columns UserId, Username and CompanyId (this is foreign key)
  Shirts has has 4 columns ShirtId, ShirtColor, ShirtBrand and UserId (this is foreign key)</p>
  
  <p>Currently I have a named query like this</p>
  
  <pre><code>@NamedQuery(name="Company.getCompanyById", query="SELECT c FROM Company c WHERE c.companyId = :companyId")
  </code></pre>
  
  <p>When I call this query and pass the companyId, I get back an object that contains the compnay, all the users for that company, and all the shirt for that user. In JSON it looks like this</p>
  
  <pre><code>{
      CompanyId:1,
      CompanyName:"ABC Inc",
      {Users:[{UserId:1,Usernmae:"James",
                Shirts:{[
                   {Color:"Blue", Brand:"GAP"},
                   {Color:"Blue", Brand:"Old Navy"}]
                }                 
       }]} 
  }
  </code></pre>
  
  <p>Now if I wanted to have a similar query, but only return shirts that are DISTINCT by color to the specific user how could I do that?</p>
  
  <p>So it would be like</p>
  
  <pre><code>{
      CompanyId:1,
      CompanyName:"ABC Inc",
      {Users:[{UserId:1,Usernmae:"James",
                Shirts:[Blue]                 
       }]} 
  }
  </code></pre>
  
  <p>Thanks for the help</p>