Hello Hibernate team & lovers.
I have 2 entities as described below.
What I would like to achieve is to fetch the related entities in a single query (fetch graph), but the collections related by the one-to-many should be filter with a specific condition based on columns on the two joined tables.
In this example, I want to fetch all the rollerCoasters I am allowed to use.
@Entity
@Table(name = "people")
//...
@Column(name = "PPL_WEIGHT")
private Integer weight;
//... we also have a zip code used for join
@OneToMany
@JoinColumns({
@JoinColumn(name = "ZIP_CODE", referencedColumnName = "PPL_ZIP_CODE")
})
@Filter(name = "onlyAuthorized",
condition="{rc}.MAX_SUPPORTED_WEIGHT > {ppl}.PPL_WEIGHT",
deduceAliasInjectionPoints = false,
aliases = {
@SqlFragmentAlias(alias = "ppl",table = "people"),
@SqlFragmentAlias(alias = "rc",table = "roller_coaster")
})
private Set<RollerCoaster> authorizedRollerCoasters = new LinkedHashSet<>();
}
@Entity
@Table(name = "roller_coaster")
@FilterDef(name = "onlyAuthorized")
public class RollerCoaster {
// ... id & zipcode ...
@Column(name = "MAX_SUPPORTED_WEIGHT")
private Integer maxWeight;
}
I tried to make this work with @SqlFragmentAlias but it seems that only alias refereing to the collection’s class is authorized (here RollerCoaster).
More precisely, the getAlias() method in TableGroupFilterAliasGenerator always return null and my condition became {rc}.MAX_SUPPORTED_WEIGHT > null.PPL_WEIGHT that ends throwing
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "Object.hashCode()" because "key" is null
at java.base/java.util.concurrent.ConcurrentHashMap.get(ConcurrentHashMap.java:936)
at org.hibernate.metamodel.model.domain.internal.EntityPersisterConcurrentMap.get(EntityPersisterConcurrentMap.java:30)
at org.hibernate.metamodel.model.domain.internal.MappingMetamodelImpl.findEntityDescriptor(MappingMetamodelImpl.java:415)
at org.hibernate.query.sqm.sql.BaseSqmToSqlAstConverter.registerEntityNameUsage(BaseSqmToSqlAstConverter.java:3066)
at org.hibernate.query.sqm.sql.BaseSqmToSqlAstConverter.registerEntityNameUsage(BaseSqmToSqlAstConverter.java:3050)
at org.hibernate.internal.FilterHelper.render(FilterHelper.java:273)
As added conditions on LEFT JOIN are now forbidden in Hibernate 6, this way with Filters seemed to be my last option.
Do you have any ideas on how to solve my issue ?
Similar topics but without response :
https://hibernate.atlassian.net/browse/HHH-12967
Have a nice day
Renaud