0

I've been trying to optimize the way hibernate achieve some information, but I can't understand next issue.

I've one entity like this

public class AnoEmp{
    //bi-directional many-to-one association to Empleado
    @ManyToOne(fetch=FetchType.EAGER)
    @JoinColumn(name="ID_EMPLEADO", nullable=false, insertable=false, updatable=false)
    private Empleado empleado;
}
...

And another one like this

public class Empleado{
    //bi-directional many-to-one association to AnoEmp
    @JsonIgnore
    @OneToMany(mappedBy="empleado", fetch=FetchType.LAZY)
    private List<AnoEmp> anosEmp;
...

Althoug it's eager (and it has to be like that) It performs N + 1 queries one for the list of AnoEmp and one per Empleado. But it could be done with just one query (as I expected becouse of @eager)

I found this Why hibernate perform two queries for eager load a @OneToOne bidirectional association? but can't get mine running properly. Any suggestion? Thank you very much.

Community
  • 1
  • 1
CalamarBicefalo
  • 87
  • 2
  • 11

1 Answers1

0

The only thing I have been found always working is the use of join in the JPQL e.g.

   "from Empleado emp join emp.anosEmp"
Yogendra Singh
  • 33,927
  • 6
  • 63
  • 73
  • I tried, new select make explicitly the join, but still get N + 1 queries... Thank you very much anyway. – CalamarBicefalo Oct 16 '12 at 12:45
  • Is there any specific reason for keeping `@OneToMany(mappedBy="empleado", fetch=FetchType.LAZY)` as `Lazy`? – Yogendra Singh Oct 16 '12 at 13:40
  • Yes, I dont need anoEmp everytime i need empleado. In the other way is different. The query anyway gets AnoEmp, so I suppose empleado should be loaded eagerly and with one query. – CalamarBicefalo Oct 16 '12 at 14:41