Questionmay have many
Answers, but only one
Country. Lets see how to model this relation in Java in order to be convenient and easy to use.
countryIdproperty mapped to corresponding column in database, as any other column:
Country, you just need the id (and not the
Question. For those situations we usually put the following set and get methods pair:
Countryis needed. Other solution, for 1-1 relationships, is to fetch both
Countrytogether, using join hints in template sql:
Questionmay have some
Answers. There is nothing to map or configure in
Questionfor this relation.
Answers has to be loaded manually when needed. Here is what we usually put for 1-to-many relations in parent entity (here:
List<Answer>filtered and ordered anyhow you need them, or to simply load them in provided
Question. If you do not need to have sorted or filtered list, you can simply invoke:
AppDao#findRelated(Answer.class, question). See relations & hints for more details.
Answer) you can do everything as for 1-1 relation: map FK and add set/get methods in the same manner. On the child side (here:
Question) you have to manage this relations as already said in above note.
User, where user may have many favorite questions. We have
Favoritesentity since we use template-sql.