How to apply filter condition's on two tables for one-to-many association

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 :confused: :
https://hibernate.atlassian.net/browse/HHH-12967

Have a nice day

Renaud

Please try to create a reproducer with our test case template and if you are able to reproduce the issue, attach that to the issue you linked.

1 Like

Hello,

Thanks for you reply :slight_smile:
I created some tests related to my use cases with the super useful test case template.
Here is the link : GitHub - geektortoise/hibernate-test-case-templates: Templates and examples to report issues to Hibernate

The issue that I linked has enough differences that lead me to create a new ticket :
https://hibernate.atlassian.net/jira/software/c/projects/HHH/boards/31?selectedIssue=HHH-18355

Of course, I’m available to offer more tests or details to help as much as I can.

Have a nice day.

1 Like