Reputation: 1544
Is there any way to force hibernate to use an inner-join for an association using annotations? I am using Hibernate 5 via Spring Boot JPA
@Data
class Entity {
private Integer id;
@OneToMany
@JoinColumn(name="entityId", referencedColumnName="id")
private Set<ComplexObject> complexObjects;
I would like the join for complexObjects
be an inner-join rather than a left join.
Upvotes: 1
Views: 698
Reputation: 16452
You shouldn't see a join by default at all since this is a @OneToMany
association which is lazy by default. There must be a query explictly using LEFT JOIN FETCH
or just LEFT JOIN
. Other possible reasons for left joins to happen are the use of an entity graph or the use of the fetch
method in the JPA CriteriaBuilder API.
In general, the left join here is correct as it is necessary to retain the expected cardinality. Fetching with an inner join would eliminate the main entity if there were no children which is probably not what one would expect when applying e.g. an entity graph.
I don't understand why you need an INNER JOIN
, but depending on how the join is created, you might have to adapt your query to get the desired result.
Upvotes: 1