perplexhub / rsql-jpa-specification

Java Library to Translate RSQL into Spring Data JPA Specification and QueryDSL Predicate
MIT License
233 stars 63 forks source link

Throws an unknown property exception when i reduce the path by using the property map. #156

Closed jfb-NRB closed 2 months ago

jfb-NRB commented 5 months ago

Hello,

I have a structure where a user has multiples roles, but in our database, the relationship between user and role is in a user_role_rel table because this table has additional fields like creationDate.

public class User {
private Date creationDate;
@OneToMany
private Set<UserRoleRel> userRoleRels;
}

public class UserRoleRel {
private Date creationDate;
@ManyToOne
private Role role;
}

public class Role {
private Date creationDate;
private int roleId;
}

If we filter users with a roleId, the classic path is "userRoleRels.role.roleId" and that's OK. But I would like to simplify the path with "roles.roleId" and hide the relationship table by adding a property mapping to User like this:

RSQLJPASupport.addMapping(User.class, "roles", "userRoleRels.role");

I received the exception: Unknown property: roleId from entity User

I used version 5.x and the problem may be with the RSQLJPAPredicateConverter class in the findPropertyPath function . When the function is called by recursion, the root and attribute variables are set but not the classMetadata.

Thanks for your follow-up

Regards

tobjohnbx commented 4 months ago

we have the same problem with reducing the query path with RSQLCommonSupport.addMapping(..)

here is an example which produces this error: https://github.com/jdmmnn/rsql-embedded-bug-repository/tree/master

2024-07-11T09:40:22.776+02:00  INFO 26697 --- [    Test worker] i.g.perplexhub.rsql.RSQLCommonSupport    : Adding entity class mapping for class com.example.demo.entity.AccountEntity, selector invoiceAddress and property addressHistory.invoiceAddress

Unknown property: name from entity com.example.demo.entity.AccountEntity
io.github.perplexhub.rsql.UnknownPropertyException: Unknown property: name from entity com.example.demo.entity.AccountEntity
    at io.github.perplexhub.rsql.RSQLJPAPredicateConverter.findPropertyPathInternal(RSQLJPAPredicateConverter.java:101)
    at io.github.perplexhub.rsql.RSQLJPAPredicateConverter.findPropertyPath(RSQLJPAPredicateConverter.java:73)
    at io.github.perplexhub.rsql.RSQLJPAPredicateConverter.resolveExpression(RSQLJPAPredicateConverter.java:235)
    at io.github.perplexhub.rsql.RSQLJPAPredicateConverter.visit(RSQLJPAPredicateConverter.java:211)
    at io.github.perplexhub.rsql.RSQLJPAPredicateConverter.visit(RSQLJPAPredicateConverter.java:24)
    at cz.jirutka.rsql.parser.ast.ComparisonNode.accept(ComparisonNode.java:67)
    at io.github.perplexhub.rsql.RSQLJPASupport.lambda$toSpecification$7eef092d$1(RSQLJPASupport.java:135)
    at org.springframework.data.jpa.repository.support.SimpleJpaRepository.applySpecificationToCriteria(SimpleJpaRepository.java:841)
    at org.springframework.data.jpa.repository.support.SimpleJpaRepository.getQuery(SimpleJpaRepository.java:760)
    at org.springframework.data.jpa.repository.support.SimpleJpaRepository.getQuery(SimpleJpaRepository.java:745)
    at org.springframework.data.jpa.repository.support.SimpleJpaRepository.findAll(SimpleJpaRepository.java:449)
    at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103)
    at java.base/java.lang.reflect.Method.invoke(Method.java:580)
......
ng-galien commented 3 months ago

Hi @tobjohnbx, thanks for your detailed report.

tobjohnbx commented 3 months ago

it is merged with https://github.com/perplexhub/rsql-jpa-specification/pull/159

jfb-NRB commented 3 months ago

Is it possible to do the same fix on version 5.x because our project is still in SpringBoot 2. Thanks in advance.

perplexhub commented 2 months ago

Is it possible to do the same fix on version 5.x because our project is still in SpringBoot 2. Thanks in advance.

5.1.2