I'm actually trying to use JPA @OneToOne annotation to link a Child entity to its Parent.

It's working well, except the fact that when getting a list of Childs, the JPA engine (Hibernate in this case) make 1+n queries.

Here is the log of the Hibernate queries :

select child0_.id as id1_0_, child0_.parent as parent3_0_, child0_.value as value2_0_ from child child0_
select parent0_.id as id1_1_0_, parent0_.something as somethin2_1_0_ from parent parent0_ where parent0_.id=?
select parent0_.id as id1_1_0_, parent0_.something as somethin2_1_0_ from parent parent0_ where parent0_.id=?
select parent0_.id as id1_1_0_, parent0_.something as somethin2_1_0_ from parent parent0_ where parent0_.id=?

Using exactly the same entities definition, when I get a child in particular, JPA executes the query with expected JOIN :

select child0_.id as id1_0_0_, child0_.parent as parent3_0_0_, child0_.value as value2_0_0_, parent1_.id as id1_1_1_, parent1_.something as somethin2_1_1_ from child child0_ left outer join parent parent1_ on child0_.parent=parent1_.id where child0_.id=?

The point is that I would like not to write the query myself, as I will use pagination and filters in a more complex situation than in the example But thanks for the help, it may be the solution I'll have to use in the end
– Alexandre CarbenayDec 12 '17 at 12:32

Actually, I do not want my association to be loaded lazily, but eagerly. What I really want is JPA to use one single database query to get the children and their parent association
– Alexandre CarbenayDec 12 '17 at 12:30

I want to use EAGER because in that case, I'm sure I will need the associated type information (special query for list rendering purposes), so it makes sense to use EAGER in that situation. But I don't understand what you mean : are you saying that, when using Hibernate EntityManager, if I define JOIN FETCH in combination with EAGER, hibernate will use both the JOIN in initial query, then secondary queries for each associated type ? But if I use only JPA and don't interact with EntityManager directly, everything seems to be OK: select ... from child child0_ inner join parent parent1_ on ...
– Alexandre CarbenayDec 12 '17 at 15:57

EAGER means always, not just in one case, and you already have a query, so it will work just fine with LAZY at map time + JOIN FETCH at query time. Hibernate EntityManager is just JPA, like EclipseLink or OpenJPA. if you use JPA and load the Child entity by its id (entityManager.find(Child.class, childId)), Hibernate will issue a JOIN for this association even if you don't need it. If you execute a JPQL query and forget to JOIN FETCH, a secondary query for every associated entity will be generated,
– Vlad MihalceaDec 12 '17 at 16:01

OK now I understand why you recommand using FETCH JOIN instead of EAGER association : to ensure that no mistake / forgetting could imply secondary queries. Thanks for the advice
– Alexandre CarbenayDec 13 '17 at 10:34