How can I prevent Hibernate 6 from removing a join to an associated entity when I don't directly use the joined entity's property in my queries

hi,
We are using hibernate-core:6.2.7.Final and jakarta.persistence-api-3.1.0. We are using predicates to form our query. We observed that if my query is not using any property of the join entity, it is not making the join in statements.

‘Class A {
private EntityB entityB ;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = “EntityBId”)
public EntityB getEntityB() {
return this.entityB ;
}
}’

In my predicates If I make explicit join statement, then it adds it otherwise it’s not adding it.

Kindly help at earliest, we will be grateful on valuable direction/suggestions.

If the association that you join is protected by a foreign key and you’re doing a left join or the column is non-nullable also with inner joins, then Hibernate ORM optimizes the join away if you are never referring to any of the columns of the join, because the join is unnecessary and can be removed without changing semantics.

If you don’t have a foreign key you should tell Hibernate ORM about this by annotating @NotFound(IGNORE).

Thank you Beikov, you always help.
We upgraded from hibernate 5 criteria api to predicates of hibernate 6.
Our createQuery is not bringing the joins of eager type if we are not referring its properties in where conditions.
We still cannot see the joins even after using @NotFound annotation.

JPA Criteria is just like HQL, such queries are the “definitive source” of what the SQL query should contain. Even if you have EAGER associations, Hibernate ORM will not add joins for such associations to the resulting SQL query. The only way to alter the joins of the resulting SQL is to use an entity graph of fetch profile.
Hibernate ORM will still respect EAGER, but will load the data through subsequent select statements.