英文:
Java map Set to another Set (Using Model Mapper or Any other way)
问题
在我目前正在工作的项目中,有一个名为 Project 的类,
public class Project {
private Integer projectId;
private String projectName;
// 获取器和设置器(getters & setters)
}
这个项目在 User 类中被用作 Set,
public class User {
private Integer userId;
private Set<Project> projects;
// 获取器和设置器(getters & setters)
}
我需要将这个 User 类映射到 UserDto。UserDto 如下所示。
public class UserDto {
private Integer userId;
private Set<ProjectDto> projectDtos;
// 获取器和设置器(getters & setters)
}
ProjectDto 也和 Project 类相同。
public class ProjectDto {
private Integer projectId;
private String projectName;
// 获取器和设置器(getters & setters)
}
为了将 User 映射到 UserDto,我尝试了下面的代码。但是它报错了。
private UserDto map(User user) {
UserDto userDto = new UserDto();
userDto.setUserId(user.getUserId());
Set<ProjectDto> projectDtos = new HashSet<>();
Type setType = new TypeToken<HashSet<ProjectDto>>(){}.getType();
projectDtos = modelMapper.map(user.getProjects(), setType);
userDto.setProjectDtos(projectDtos);
return userDto;
}
返回的错误如下:
> 无法将 org.hibernate.collection.internal.PersistentSet 转换为 java.util.Set
如果你能对此提供支持,将会非常棒。也欢迎不使用 ModelMapper 的答案。提前感谢!
英文:
In the project currently I'm working on, has a Project class,
public class project {
private Integer projectId;
private String projectName;
//getters & Setters
}
This project is using as a Set in User class
public class User {
private Integer userId;
private Set<Project> projects;
//getters & Setters
}
I need to map this User class to UserDto. UserDto is like below.
public class UserDto {
private Integer userId;
private Set<ProjectDto> projectDtos;
//getters & Setters
}
The ProjectDto is also same as Project class.
public class projectDto {
private Integer projectId;
private String projectName;
//getters & Setters
}
To map User and UseDto, I tried below code. But it's giving an error.
private UserDto map(User user) {
UserDto userDto = new UserDto();
userDto.setUserId(user.getUserId());
Set<ProjectDto> projectDto= new HashSet<>();
Type setType = new TypeToken<HashSet<ProjectDto>>(){}.getType();
projectDto = modelMapper.map(user.getProjects(), setType);
userDto.setProjectDto(projectDto);
return userDto;
}
Following error is returned:
> failed to convert org.hibernate.collection.internal.PersistentSet to
> java.util.Set
It'd be great if you could support me for this. Answers without using ModelMapper are also welcome.
Thanks in advance!
答案1
得分: 3
首先在 ProjectDto 类中添加一个构造函数:
public class ProjectDto {
private Integer projectId;
private String projectName;
public ProjectDto(Integer projectId, String projectName) {
this.projectId = projectId;
this.projectName = projectName;
}
// 获取器和设置器
}
然后使用 Java 8 的流式 API,可以像这样完成:
private UserDto map(User user) {
UserDto userDto = new UserDto();
userDto.setUserId(user.getUserId());
userDto.setProjects(user.getProjects().stream()
.map(project -> new ProjectDto(project.getProjectId(), project.getProjectName()))
.collect(Collectors.toSet()));
return userDto;
}
英文:
first add a constructor to ProjectDto class:
public class ProjectDto {
private Integer projectId;
private String projectName;
public ProjectDto(Integer projectId, String projectName) {
this.projectId = projectId;
this.projectName = projectName;
}
//getters & Setters
}
then with java 8 stream API, it can be done like this:
private UserDto map(User user) {
UserDto userDto = new UserDto();
userDto.setUserId(user.getUserId());
userDto.setProjects(user.getProjects().stream()
.map(project -> new ProjectDto(project.getProjectId(), project.getProjectName()))
.collect(Collectors.toSet()));
return userDto;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论