I do the following:
the Books collection is not hydrated because the author instance was already
hydrated when populating $author1. This means when I call
count($author1->getBooks()) a third query is executed to fetch the Books again.
The result is even more problematic when adding a WITH clause to the
INNER JOIN, in that case one would expect the Books collection to only
contain the books matching matching that criteria.
The result is expected when commenting out the $author1 =... line, thus when
the author does not already exist in the entity manager.
I'm sorry I can't produce a test case right now.