2

I have request like this:

http://localhost:4000/services/querywithdsl?min=30&max=90

My domain entity only consists of id :

@Entity
@Data
public class ServiceNumberPorting {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(updatable = false, nullable = false)
    private Long id;
}

And my rest controller with predicate

@GetMapping(value = "/services/querywithdsl")
    public ResponseEntity<List<ServiceNumberPortingMapper>>getServiceNumberPortingWithDsl(@QuerydslPredicate(root = ServiceNumberPorting.class) Predicate predicate) {
        ...

    }

Repository:

public interface ServiceNumberPortingRepository extends JpaRepository<ServiceNumberPorting, Long>,QuerydslPredicateExecutor<ServiceNumberPorting>,    QuerydslBinderCustomizer<QServiceNumberPorting>

I want to build query with querydsl like this one using bindings

..where id>min and id< max

All examples I found are directly mapping query parameters to entity column, but in this example, I have only id in my entity and I want to do range check with incoming parameter values which have different name than my entity columns.

However, since my domain won't consist of min and max columns I could't get it working with customizing binding such as using QueryDslBinderCustomizer:

bindings.bind(store.city).single((path, value) -> path.endsWith(value));

How to accomplish that?

Here is docs that I gone through: spring data examples binding examples

Maistrenko Vitalii
  • 994
  • 1
  • 8
  • 16
sarah
  • 580
  • 1
  • 6
  • 24
  • [Here](https://stackoverflow.com/a/48596145/5380322) is an approach (see 'between filter'). – Cepr0 Mar 13 '18 at 11:10
  • you are using profile.heightMeters which probably exists in Profile entity. But min and max properties do not exist in my entity. How do I compare them with id? – sarah Mar 13 '18 at 11:19
  • But you have property `id` - when you implement this approach you will get 'between' 30 and 90 filter for `id` like this: `/services/?id=30&id=90` – Cepr0 Mar 13 '18 at 11:25
  • @sarah did my solution work for you? – tryingToLearn Mar 15 '18 at 03:27
  • I ended up using some dto object to map request fields to object and creating dsl query myself with pageable repository. Not quite what I looked for to be honest. But I thank you both for solutions and your time, learned new approaches from those answers. – sarah Mar 15 '18 at 20:30
  • i am in the same situation, any workaround? – Andres Jun 14 '18 at 01:30
  • @Andres I ended up with classic approach, some dummy object for holding query parameters(mapping incoming request directly to this object), then some repository which extending QuerydslPredicateExecutor. Then I build Predicate object and querying with this repository which supports predicate as an argument. – sarah Jun 14 '18 at 14:32

1 Answers1

1

One of the solutions I can think of is this:

In your repository,I have defined a method getValueLyingBetween and written a simple query:

public interface ServiceNumberPortingRepository extends JpaRepository...{
    @Query("SELECT m FROM <YOUR_TABLE_NAME> m where ( m.id >=:min and m.id <= :max )")
    public ServiceNumberPorting getValueLyingBetween(@Param("min") String min, @Param("max") String max);

}

And then you can simply call this repository method from your controller with min and max values.

Is this what you were looking for?

tryingToLearn
  • 10,691
  • 12
  • 80
  • 114