Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
320 views
in Technique[技术] by (71.8m points)

java - How can convert DTO to Entity in Spring boot?

I looked for countless questions. But most of the questions were as follows.

  1. Entity -> Dto
  2. Entity and Dto have the same attribute value.

but I don't want this.

I wonder how I can convert Dto to Entity when if property value present in Entity did not exist in Dto.

Some Code

dosen't specify the codes for the [annotations, Lombok, jpa, ...etc] but it exists!

User Entity

class User {
  Long id,
  String username,
  String email,
  String password
}

UserDto

class UserDto {
  String email
}

Post Entity

class Post {
  Long id,
  String title,
  User user

}

PostDto

class PostDto {
  Long id,
  String title,
  String username
}

UserService

...

UserDto findUser(id: Long) {
   ...
}

...

PostService

...

PostDto savePost(Post post) {
   ...
}

...

PostController

...

PostDto createPost(@RequestBody PostDto postDto) {
  // spring security
  Long userId = XXX.getUserId()  

  ....

  UserDto userDto = userService.findUser(userId)
  
  // HERE !! what can i do ??
  // How can i convert userDto to user ?

  Post post = new Post(postDto.title, user)
  PostDto newPost = postService.savePost(post)
}

...

Question

All methods of userService return in DTO. how can I set user entity in Post Entity? I received a userDto return which has only username.

What should I do at times like this?

  1. The repository is not being called directly from the controller.
  2. The service would like to return the DTO unconditionally.
  3. Should I create an additional Dto that has the same properties as user entity?
question from:https://stackoverflow.com/questions/65895506/how-can-convert-dto-to-entity-in-spring-boot

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Normally I implemented my class in following manner,

Entity : Contain all data related to your actual database object. (Additional data by @transient annotation)

DTO : Customize class which creates to provides compatibility between Entity and required JSON.

Mapper : It can convert Entity to DTO and vice versa.

In sort, Create entity for database object, n numbers of DTO for verity of need and using mapper class convert it to appropriate type and write this terms in your service class. So based on your need create DTO and in service class implement logic for get them all together. (In best way use interface or inheritance when wants to bind multiple DTO in single entity or scattered it.)

Now comes to your question,

  1. I wonder how I can convert DTO to Entity when if property value present in Entity did not exist in DTO.

    Answer : Convert it using Mapper class. As per I define above Entity is database object and DTO is expected JSON. So doesn't matter it compulsory exist in your DTO. You can customize DTO as per your need.

  2. The repository is not being called directly from the controller.

    Answer : As obvious you have to do that and right way to implementation.

  3. Should I create an additional DTO that has the same properties as user entity? java

    Answer : Yes you can but if it compulsory required. My suggestion is follow structure as I defined earlier it becomes helpful in future also, Suppose new kind of data you need you just have to add one function in mapper class and it return data accordingly, you also modify from single place and use every where.

This suggestion for based your code.

  1. Move your all logical code controller to service class
  2. In service class fire query and contain data of userDTO.
UserDto userDto = userService.findUser(userId)
  1. It gives you email. Based on this fire another query for fetch user.

My personal suggestion as developer,

Create one JHipster project once and check their flow it gives you good idea regarding how to create structure in professional way.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...