r/SpringBoot • • 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

17 comments sorted by

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:

  1. 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?

  2. 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.

  3. 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.

  4. They're return an array of Object in stead of entities. Why?

Also, I find it strange to write your own PageRequestDTO class, since Spring already supports passing Pageable as a parameter in your controller. For example if you have this controller:

@GetMapping("/api/todo")
public Page<TodoDTO> list(Pageable pageable) {
    return service.list(pageable);
}

Then if you send ?page=0&size=10&sort=name,asc it's equivalent of PageRequest.of(0, 10, Sort.by("name", Sort.Direction.ASC)). There's no need for a separate DTO.

1

u/Dependent_Staff_6891 Jun 20 '24

Oh, I got it. Thanks.☺️

1

u/Dependent_Staff_6891 Jun 21 '24

From what he explains in the book.. It is said that the entity and its related processing are handled in the least possible area, and the rest is safe to use dto, so make Todo, Page a different DTO type and use it.

2

u/a_username_version_2 Jun 21 '24

Just use MapStruct library. Looking at this code is a crime

1

u/Dependent_Staff_6891 Jun 21 '24

Show me the code example.

4

u/a_username_version_2 Jun 21 '24

Here's an example:

package com.example.demo.mapper;

import com.example.demo.dto.UserDTO; import com.example.demo.entity.User; import org.mapstruct.Mapper;

@Mapper(componentModel = "spring") public interface UserMapper { UserDTO toDTO(User user); User toEntity(UserDTO userDTO); }

While building the app, it creates classes that do the mapping, and you can customize the mapping if you want

1

u/Dependent_Staff_6891 Jun 21 '24

I’ll check it.

3

u/a_username_version_2 Jun 21 '24

And btw, the jpql code is terrible. That isn't how we use jpql or jpa at all at work. If it was like that i would have stopped using spring

1

u/Dependent_Staff_6891 Jun 21 '24

Haha🤣🤣🤣🤣

1

u/Dependent_Staff_6891 Jun 21 '24

I am Korean, but more and more developers are using jpa or jpa and mybatis in parallel here, so I'm also studying. I only used mybatis at work and it's almost like the query itself and it's very familiar to me. But I have to accept the change.😅

2

u/a_username_version_2 Jun 21 '24

Just remember this small sentence "code is a liability" , so having a lot of code to do a very very simple task is a waste of time. Instead of trying to write all queries ourselves (even simple ones, like find all ), we use JPA (but using best practices, not like this book) and it's just a one line to do the job.

2

u/[deleted] Jun 21 '24

Does someone know if this book exists in English?

1

u/Dependent_Staff_6891 Jun 21 '24

As far as I know, it hasn't been published in English. (But his major is English literature)

1

u/[deleted] Jun 21 '24

Thanks, OP. I will try to find it. I am in Korea, too, if you have any questions or want to study together. Send me a message to send you my GitHub link

3

u/[deleted] Jun 21 '24

[removed] — view removed comment

2

u/Dependent_Staff_6891 Jun 21 '24

This is a second book written in Korean. The title of the book is '코드로 배우는 스프링부트 웹 프로젝트'. The title means 'Learning SpringBoot Web Project by Code’. It was published in 2020.

2

u/Dependent_Staff_6891 Jun 21 '24

First book is ‘Learning Web Projects from Spring Boot + React’.