我对模型映射器进行了以下配置,以将User
类的实例转换为ExtendedGetUserDto
.
public ExtendedGetUserDto convertToExtendedDto(User user) {
PropertyMap<User, ExtendedGetUserDto> userMap = new PropertyMap<User, ExtendedGetUserDto>() {
protected void configure() {
map().setDescription(source.getDescription());
map().setId(source.getId());
// map().setReceivedExpenses(
// source.getReceivedExpenses()
// .stream()
// .map(expense -> expenseDtoConverter.convertToDto(expense))
// .collect(Collectors.toSet())
// );
Set<GetInvitationDto> result = new HashSet<GetInvitationDto>();
for (Invitation inv: source.getReceivedInvitations()) {
System.out.println("HELLO");
//result.add(null);
}
//map().setReceivedInvitations(result);
}
};
modelMapper.addMappings(userMap);
return modelMapper.map(user, ExtendedGetUserDto.class);
}
在注释掉之前,setReceivedExpense
我收到了这个错误:
org.modelmapper.ConfigurationException: ModelMapper configuration errors:
1) Invalid source method java.util.stream.Stream.map(). Ensure that method has zero parameters and does not return void.
2) Invalid source method java.util.stream.Stream.collect(). Ensure that method has zero parameters and does not return void.
2 errors
在花了一些时间并没有找到根本原因后,我尝试删除 DTO 中所有可疑的循环依赖项GetUserDto
(我在代码)并用简单的循环替换它。GetExpenseDto
expenseDtoConverter
map().setReceivedExpenses
for
我收到以下错误:
1) Invalid source method java.io.PrintStream.println(). Ensure that method has zero parameters and does not return void.
为什么我会收到这些错误?
编辑 1
User.java
@Entity
@Table(name="User")
public class User implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="id")
private long id;
@Column(name = "name")
private String name;
@Size(min=15, max=15)
@Column(name="image_id")
private String imageId;
@Size(max=100)
@Column(name="description")
private String description;
@OneToMany(mappedBy="admin")
private Set<Group> ownedGroups;
@ManyToMany(mappedBy="members")
private Set<Group> memberGroups;
@OneToMany(cascade = CascadeType.ALL, fetch=FetchType.EAGER, mappedBy="owner")
private Set<Expense> ownedExpenses;
@ManyToMany(cascade = CascadeType.REFRESH, fetch=FetchType.EAGER)
private Set<Expense> receivedExpenses;
@OneToMany(cascade=CascadeType.ALL)
private Set<Invitation> ownedInvitations;
@OneToMany(cascade=CascadeType.ALL)
private Set<Invitation> receivedInvitations;
//setters and getters for attributes
}
ExtendedGetUserDto.java
public class ExtendedGetUserDto extends GetUserDto {
private static final long serialVersionUID = 1L;
private Set<GetInvitationDto> receivedInvitations;
private Set<GetExpenseDto> receivedExpenses;
private Set<GetExpenseDto> ownedExpenses;
private Set<GetGroupDto> ownedGroups;
private Set<GetGroupDto> memberGroups;
//setters and getters for attributes
}