I have the following configuration for the model mapper to convert an instance of User
class to an instance of 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);
}
Before commenting out setReceivedExpense
I received this error:
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
After spending some time and not finding the root cause, I tried to delete all suspecious cyclic dependencies in the DTOs (I have GetUserDto
referenced in GetExpenseDto
, the returning result of expenseDtoConverter
)
I still receive the same error, I commented out map().setReceivedExpenses
(as you can see in the code) and replaced it with simple for
loop.
I get the following error:
1) Invalid source method java.io.PrintStream.println(). Ensure that method has zero parameters and does not return void.
Why do I receive these errors ?
Edit 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
}