title: JPA Performance: Entity or Entity Id as query parameter?
body: <p>Let's say there are two entity types: <code>Book</code> (with primary key <code>bookId</code>) and <code>Author</code> (with primary key <code>authorId</code>). An author writes 0...n books. So <code>Author</code> is referenced in <code>Book</code> using <code>@ManyToOne</code>.</p>
  
  <p>If we want to retrieve all books written by a certain author, we could do two things in JPQL (e. g. NamedQuery):</p>
  
  <ol>
  <li><code>SELECT b FROM Book b WHERE b.author = :author</code> (entity as parameter)</li>
  <li><code>SELECT b FROM Book b WHERE b.author.authorId = :authorId</code> (primary key ID of entity as parameter)</li>
  </ol>
  
  <p>In the first option we have to watch out that the passed object is really of type <code>Author</code> and it already has a primary key.</p>
  
  <p>But: Is there a difference regarding performance in the two options mentioned above? I guess (2) is less expensive, but (1) might be easier to optimize for the JPA provider. Is there a performance test or literature available on this question?</p>
  
  <p>We can assume that the <code>Author</code> instance (whose books we want to search for) has already been loaded (e. g. to view his/her profile in the response), so it was no additional code writing effort to either pass the whole object or just it's ID to the book search method. But is there a difference in execution speed? Maybe depending on the JPA provider used?</p>