Comments (7)
@darrachequesne Thanks a lot, it works !
For anyone who could be struggling to accomplish it, here is my temporary code down below with some explanations. I think I'll commit something cleaner soon on my fork of spring-data-jpa-datatables.
My datatables table contains a column named firstName (first_name in my H2 USER table).
For now, it allows filtering using regular expression on that same firstName column. Filtering of this column is (always) done using regular expression. Filtering of the other columns is done using standard LIKE mode.
From my UserRepository interface (extending the DataTablesRepository<User, Long>), I override the 2 following methods :
@Override
default DataTablesOutput<User> findAll(DataTablesInput input) {
System.out.println("Local implementation of DataTablesRepositoryImpl");
return findAll(input, null, null, null);
}
@SuppressWarnings("unchecked")
@Override
default <R> DataTablesOutput<R> findAll(DataTablesInput input, Specification<User> additionalSpecification,
Specification<User> preFilteringSpecification, Converter<User, R> converter) {
DataTablesOutput<User> output = new DataTablesOutput<User>();
output.setDraw(input.getDraw());
if (input.getLength() == 0) {
return (DataTablesOutput<R>) output;
}
try {
long recordsTotal =
preFilteringSpecification == null ? count() : count(preFilteringSpecification);
if (recordsTotal == 0) {
return (DataTablesOutput<R>) output;
}
output.setRecordsTotal(recordsTotal);
Column column = input.getColumn("firstName");
column.setSearchable(false); // so the default filter will not be applied
String regexValue = column.getSearch().getValue();
output = findAll(input, (root, query, builder) -> {
javax.persistence.criteria.Expression<?> regex = builder.function("REGEXP_LIKE", String.class, root.get("firstName"), builder.literal(regexValue));
System.out.println("Expression regex : "+regex);
System.out.println("column : "+column);
return builder.equal(regex, builder.literal(1));
});
} catch (Exception e) {
output.setError(e.toString());
}
return (DataTablesOutput<R>) output;
}
Again, it's a sandbox code (messy and done on a table corner). I will later, clean it, make it listen to the value of the checkboxes (regex:true or false, smart search:true or false) for every column. The front end is mainely inspired by this example.
Once again, I use embedded H2 database.
Thanks a lot Damien for saving the day !
Cheers,
Salutations toulousaines d'un ex lillois.
from spring-data-jpa-datatables.
Hi! The problem is that JPQL only supports LIKE expressions, with %
and _
tokens. And regex support does vary accross different DBMS (like regexp_matches
in Postgresql, RLIKE
in MySQL...)
That's why the regex flag is currently not handled. But I'm open to suggestions!
from spring-data-jpa-datatables.
Haven't thought about it yet. I have temporarily fixed using IN clause Specification for values that I send explicitly along with the data table request params.
from spring-data-jpa-datatables.
Closing due to inactivity, please reopen if needed.
from spring-data-jpa-datatables.
(and I don't know how it could be properly handled)
from spring-data-jpa-datatables.
Hi, Bonjour,
Thanks a lot for your work.
I'm trying to use regex. I'll only be using H2 database. I imported your project in its version 4.3.
I'm wondering what the most elegent way to actually use those 'regex=' flags in the backend.
H2 allows regex in native queries.
Like this dummy example that selects any value for my_column column :
SELECT * FROM my_table WHERE my_column REGEXP '.*'
Overriding methods from your repository interface and modifying its methods implementation seem to be an option. I thought maybe, you had tested some things on your side, using your embedded H2, and maybe you have an idea. Shall I use SQL :
@Query(value = "...", nativeQuery = true)
I tend to have some difficulties to adapt your approach using SpecificationBuilder and I don't want to disfigure your code.
Any idea ?
Excellente journée !
from spring-data-jpa-datatables.
@memento since that is so nicely asked, you should be able to do:
Column column = input.getColumn("my_column");
column.setSearchable(false); // so the default filter will not be applied
String regexValue = column.getSearch().getValue();
DataTablesOutput<...> output = repository.findAll(input, (root, query, builder) -> {
Expression<String> regex = builder.function("REGEXP_LIKE", String.class, root.get("my_column"), builder.literal(regexValue));
return builder.equal(regex, builder.literal(1));
});
(not thoroughly tested)
from spring-data-jpa-datatables.
Related Issues (20)
- Filter by "<column> NOT IN (<input array>)" HOT 2
- how to remove the text - filtered from X total entries HOT 1
- How to Work with UserDTO HOT 2
- Can we search date in data table? HOT 2
- Duplicates items after filtering HOT 2
- Log Exceptions or they are silently lost HOT 4
- Add support for Spring Boot 3.0.0 HOT 3
- Is it possible to use specification for view? HOT 2
- Is it possible to do a regular expression search using ~ in PostgreSQL? HOT 2
- AddOrder doesnt seem to work HOT 2
- Filtering data sometimes throws ORM exception (Hibernate 6.1) HOT 10
- No property 'findAll' found for type 'Class' HOT 4
- Performance issue (suggestions on database indexes) HOT 7
- [question] specifications and counting total records HOT 4
- Show ManyToMany data HOT 1
- enable datatable repositories without using DatatablesJpaRepositoryBean HOT 1
- Order can be null HOT 4
- Specify date format in global/column search when database column is date (Oracle) HOT 4
- Searching for a record with special (national) character "ř" doesn't work HOT 5
- Search Panes Error in Relationship Table HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from spring-data-jpa-datatables.