It's not about Spring JPA but JPA itself. When you add a relationship, following defaults apply unless specified otherwise.
@xxToOne
- FetchType.EAGER
@xxToMany
- FetchType.LAZY
So, in your example you have a @ManyToOne
which has a default EAGER
fetch and one join query is appended. If your parent has another @xxToOne
it adds one more join and so on. It's good to know the boundaries of your entities and decide which type of FetchType
is required.
Even if you add like this:
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="parentCode",referencedColumnName="code")
Entity parentEntity;
.. if you parent has more relationships you might be getting everything loaded while fetching parent. Thus, all relationships need to be Lazy. It's a design choice based on entities.
But be aware about the ORM's N+1 problem : JPA Hibernate n+1 issue (Lazy & Eager Diff)