Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add restriction to range between two dates on query by example in Spring Data

I have one class with two dates and my search is dynamic, Hibernate provides Query By Example (QBE), but now use QBE of Spring Data. The problem is when created a QBE but the documentation restrict the ExampleMatcher for String types, i need create a ExampleMatcher for search all entities between in this dates.

How implement this using query by example?.

My alternatives for now is:

  • Create a hibernate dao and use QBE and criteria of hibernate (Bye JPA :'( )

  • Explore this http://spring.io/blog/2011/04/26/advanced-spring-data-jpa-specifications-and-querydsl/ (really ugly code)

Example properties for class:

  private int id;
  private String property1;
  private String property2;
  private Date dateFirst;
  private Date dateSecond;

Related :

  • http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#query-by-example.usage

  • How to combine multiple date-between searches with CrudRepository of Spring Data JPA?

  • Spring Data JPA: Query by Example?

like image 322
Israel Perales Avatar asked Dec 09 '16 00:12

Israel Perales


1 Answers

The short answer is, you don't. Query by example is considered exactly what its name suggests: you give an example and the query is built on a match of the given fields. That's also documented in the "Limitations" section of the reference documentation you linked to.

For more advanced use cases and predicate definitions, have a look at the Querydsl support.

like image 161
Oliver Drotbohm Avatar answered Nov 17 '22 20:11

Oliver Drotbohm