0

My question is how to get values from the database (PostgreSQL) by regex, BUT not using native SQL queries. The only way I found is to use Hibernate Criteria API:

criteria.add(Restrictions.sqlRestriction("name ~ '^(?!" + filter + ")'"));

But this method also uses SQL. Spring data has query creation functionality but regex search seems to be working only with MongoDb.

http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repository-query-keywords

My goal is not depending on native sql queries, use objects instead, and make my project portable for any other databases.

9
  • JPA is doing O-R mapping. Your queries have to finally be turned into SQL to be executed in the DBMS. Forget about JPA, ask yourself: how can you retrieve data from PostGres without using SQL and is "portable"? I don't believe you can do it. Commented Dec 29, 2016 at 8:26
  • It is clear that hibernate, spring data and others will use sql queries eventually and there is no other way. I am looking for the functions of these like: someObject.addRegex(String regex). Commented Dec 29, 2016 at 10:35
  • someObject.addRegex(String regex) means nothing. What you are looking for is not archievable: There is no portable that you can query data from different DBMS without SQL Commented Dec 29, 2016 at 10:37
  • Please, have a look: docs.spring.io/spring-data/jpa/docs/current/reference/html/… docs.spring.io/spring-data/jpa/docs/current/reference/html/… Commented Dec 29, 2016 at 10:39
  • so?... what you have quoted has nothing to do with your question Commented Dec 29, 2016 at 10:41

1 Answer 1

2

You can do it with extending Hibernate Dialect class(es) and add your custom methods. Magic really ? :)

Sign up to request clarification or add additional context in comments.

1 Comment

UPDATE : If you are using Spring MVC and you want to get only matches from url params then you can use regex pattern within RequestMapping. for example : @RequestMapping(value = "/e{^\\+?\\d+\$}")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.