Are you sure you need a DTO to retrieve a Entity ?
I suppose your User is as follows
public class User implements Serializable {
private String userName;
private String email;
private Occupation occupation;
private Country country;
}
Usually, a reference to another Entity when displaying your form is by using some <select HTML element, which is similar to
<select name="occupation">
<option name="0">A occupaation</option>
<option name="1">Other occupaation</option>
<option name="2">Another occupaation</option>
</select>
Spring uses data-dinding to bind your select HTML element to your property. The method used is called initBind
public class UserController extends BaseCommandController {
private OccupationRepository occupationRepository;
private CountryRepository countryRepository;
// getter's and setter's (retrieved by Dependency Injection supported by Spring)
public UserController() {
setCommandClass(User.class);
setValidator(new UserValidator());
}
public void initBind(HttpServletRequest request, ServletRequestDataBinder binder) {
binder.registerCustomPropertyEditor(Occupation.class, new PropertyEditorSupport() {
public void setAsText(String occupationId) {
// StringUtils belongs to jakarta-commons lang
if(StringUtils.isBlank(occupationId)) {
setValue(null);
return;
}
setValue(occupationRepository.getById(Integer.valueOf(occupationId)));
}
public String getAsText() {
if(getValue() == null)
return;
return String.valueOf(((Occupation) getValue()).getId());
}
});
// Same approach when binding Country
}
}
Notice you can replace initBind method by assigning a WebBindingInitializer object
public class UserBindingInitializer implements WebBindingInitializer {
private OccupationRepository occupationRepository;
private CountryRepository countryRepository;
// getter's and setter's (retrieved by Dependency Injection supported by Spring)
public void initBinder(WebDataBinder binder, WebRequest request) {
binder.registerCustomPropertyEditor(Occupation.class, new PropertyEditorSupport() {
public void setAsText(String occupationId) {
// StringUtils belongs to jakarta-commons lang
if(StringUtils.isBlank(occupationId)) {
setValue(null);
return;
}
setValue(occupationRepository.getById(Integer.valueOf(occupationId)));
}
public String getAsText() {
if(getValue() == null)
return;
return String.valueOf(((Occupation) getValue()).getId());
}
});
}
}
...
public class UserController extends BaseCommandController {
private OccupationRepository occupationRepository;
private CountryRepository countryRepository;
// getter's and setter's (retrieved by Dependency Injection supported by Spring)
public void setUserBindingInitializer(UserBindingInitializer bindingInitializer) {
setWebBindingInitializer(bindingInitializer);
}
And your UserValidator (Notice your Validator does not know anything else about any repository)
public class UserValidator implements Validator {
public boolean supports(Class clazz) {
return clazz.isAssignableFrom(User.class);
}
public void validate(Object command, Errors errors) {
User user = (User) command;
if(user.getOccupation() == null)
errors.rejectValue("occupation", "errors.required", null);
if(user.getCountry() == null)
errors.rejectValue("country", "errors.required", null);
}
}