turkraft / springfilter

Dynamically filter JPA entities and Mongo collections with a user-friendly query syntax. Seamless integration with Spring APIs. Star to support the project! ⭐️
https://turkraft.com/springfilter
225 stars 38 forks source link

Avoid duplicates #303

Closed zuryx closed 1 year ago

zuryx commented 1 year ago

Hi everybody,

First of all thanks for give us this amazing tool, it's sweet how useful and simple it is to use.

But sadly I'm having a hard time trying to find a way to avoid getting duplicate records.

I would like know if there is a way to avoid getting duplicates records in version 3.x.x. I have been looking through all the API but I'm not able to find any useful class/interface in which I can get access to CriteriaBuilder or maybe a method that let me specify that I do not want duplicated records.

In version 2.x.x was possible to extend class com.turkraft.springfilter.boot.FilterSpecification and override method toPredicate including CriteriaQuery.distinct(true) with this custom class I was able to do not get duplicate records.

class CustomFilterSpecification<T> : FilterSpecification<T> {
    constructor(string: String) : super(string)

    override fun toPredicate(root: Root<T>, query: CriteriaQuery<*>, criteriaBuilder: CriteriaBuilder): Predicate? {
        query.distinct(true)
        return super.toPredicate(root, query, criteriaBuilder)
    }
}

but looks like there is not way to achieve the same in version 3.x.x, if that's the case it would be amazing if you can include it as a new feature :)

Regards,

torshid commented 1 year ago

Hi @zuryx :)

Can you share the query which gives you duplicate records? Are you filtering relations? Since joins are wrapped with exists, this is something unexpected.

zuryx commented 1 year ago

Hi @torshid

You are totally right, with version 3.x.x there is no need to do any sort of customization to avoid duplicates. Out of the box data is coming without dups :) .

Thank you so much for your help, I'm wondering if there is some sort of forum/site where I can post more questions about how to use the API? because I'm aware that this is not the right place to post question about that matter.

For instance I'm not sure how I can create a new specification, in version 2.x.x was possible to do this

val defaultSpecification = FilterSpecification<UserProfile>("isPublic: true and profileType: 'TAKER'")

this kind of behavior was useful to me in case I want to define a default filter and then append whatever filter the user sends through the controller, like this:

    @GetMapping("/")
    fun getAllBySpec(
        @Filter spec: Specification<UserProfile>?,
        pageable: Pageable
    ): MappingJacksonValue {
        val defaultSpec = FilterSpecification<UserProfile>("isPublic: true and profileType: 'TAKER'")
        val finalSpec = defaultSpec.and(spec)
        val result = repo.findAll(finalSpec)
    }

but in version 3.x.x looks like is not longer possible.

zuryx commented 1 year ago

@torshid nvm I just found out that an specification can be created with FilterSpecificationConverter service.

torshid commented 1 year ago

Great! You can ask your questions in this repository, there are no forums or other support places.

yswtrue commented 10 months ago

I have get duplicate records too, in 2.x.x. How can I use this code

Hi everybody,

First of all thanks for give us this amazing tool, it's sweet how useful and simple it is to use.

But sadly I'm having a hard time trying to find a way to avoid getting duplicate records.

I would like know if there is a way to avoid getting duplicates records in version 3.x.x. I have been looking through all the API but I'm not able to find any useful class/interface in which I can get access to CriteriaBuilder or maybe a method that let me specify that I do not want duplicated records.

In version 2.x.x was possible to extend class com.turkraft.springfilter.boot.FilterSpecification and override method toPredicate including CriteriaQuery.distinct(true) with this custom class I was able to do not get duplicate records.

class CustomFilterSpecification<T> : FilterSpecification<T> {
    constructor(string: String) : super(string)

    override fun toPredicate(root: Root<T>, query: CriteriaQuery<*>, criteriaBuilder: CriteriaBuilder): Predicate? {
        query.distinct(true)
        return super.toPredicate(root, query, criteriaBuilder)
    }
}

but looks like there is not way to achieve the same in version 3.x.x, if that's the case it would be amazing if you can include it as a new feature :)

Regards,