I am using JPA with Hibernate 4.1.3. I noticed that when I annotate many-to-one relations to be eager "@ManyToOne(fetch=FetchType.EAGER)", the related entity is recovered in a second query, instead of creating a single query using a join.
To solve that, I decided to use @Fetch(FetchMode.JOIN), but it didn't work at all.
Code:
@ManyToOne(fetch=FetchType.EAGER)
@Fetch(FetchMode.JOIN)
@JoinColumn(name="ID_TIPO_CONTRATO", insertable=true, updatable=false, nullable=false)
private TipoContrato tipoContrato;
The only way to force a join seems to be writing a query with a "left join fetch" to the related entity. Doing that, the main entity and the related one are recovered in a single query
Code:
"select cj " +
"from ContratoJoin cj " +
"left join fetch cj.tipoContrato tc "
Does @Fetch work with JPA or only in pure Hibernate?