Say we have a User class with UserType property defined:
private UserType userType;
Is it possible to set this property using only ID of needed UserType? Something like
public void setUserTypeById(Long id){
this.userType.setId(id);
}
But this one will only change the id property of assigned UserType instance, while I need to change it to another one.
In theory, it would be possible, if you had access to a session or to a DAO in your
Userclass. Then you could just retrieve thenUserTypecorresponding to the given ID and set it.But I do not recommend this. I think it is better to clearly separate the domain classes (
User,UserType) from the persistence classes (DAOs or session). Better create a service class (UserServicefor instance) that has a methodsetUserType(User user, int typeId)which takes care of this.