I'm working on a java spring mvc application and have an important question about mapping view model objects to database model objects. Our application uses dozer mapper for that purpose.
Suppose I have a Person model and BaseInformation model. The BaseInformation model is for general data that can be used in all other models, for example genders, colors, units, ....
BaseInformation:
class BaseInformation{
private Long id;
private String category;
private String title;
}
This can has a database table like this:
Id | Category | Title
-------------------------
1 | "gender" | "male"
2 | "gender" | "female"
3 | "color" | "red"
4 | "color" | "green"
...
This is part of my Person Model:
public class Person{
...
private BaseInformation gender;
...
}
And this is part of my RegisterPersonViewModel
public class RegisterPersonViewModel{
...
private Integer gender_id;
...
}
In the register person view, I have a <select>
that be filled from BaseInfromation with gender category. When a user submit that form, an ajax request sends to a methods of controller like this:
@RequestMapping("/person/save", method = RequestMethod.POST, produces = "application/json")
public @ResponseBody Person create(@Valid @RequestBody RegisterPersonViewModel viewModel) throws Exception {
//Mapping viewModel to Model via dozer mapper
//and passing generated model to service layer
}
Now, here is my question:
A user can change value
of gender combobox in the view manually(for example set a value of color instead of gender) and send invalid related data to controller's method. Dozer mapper map viewModel to model and this invalid data go through data access layer and persist in database. In the other words, An invalid data can save into database without any control. I want to know the best way for controlling relational data with minimum code.