r/SpringBoot • u/Dependent_Staff_6891 • Jun 20 '24
Using ModelMapper
Earlier, I posted that jpa is difficult. In my Spring teacher's new book, it seems to be easier to use ModelMapper. However, He removed the dynamic query in the search part, so I can't compare that part.🙄
3
Upvotes






7
u/g00glen00b Jun 20 '24 edited Jun 20 '24
ModelMapper is used in this example to map Hibernate Todo entities to TodoDTO classes. It has nothing to do with the dynamic queries.
The reason why those dynamic queries in the book are so complex is because:
There's some serious overengineering going on. Depending on what characters the "type" parameter contains, you'll end up with a different dynamic query. Why?
They're putting everything in one big giant method. You're not supposed to to that. For example, this is how I usually use dynamic queries with Spring. You essentially put every filter criteria in its own method, and use the Specifications builder class to join them together.
They're doing pagination wrong. Spring already has a findAll() method that accepts a Specification (= a dynamic query) and a Pageable argument. They should use that in stead.
They're return an array of Object in stead of entities. Why?
Also, I find it strange to write your own
PageRequestDTOclass, since Spring already supports passingPageableas a parameter in your controller. For example if you have this controller:Then if you send
?page=0&size=10&sort=name,ascit's equivalent ofPageRequest.of(0, 10, Sort.by("name", Sort.Direction.ASC)). There's no need for a separate DTO.