Add a @DiscriminatorColumn
to your Person
entity to explicitly specify the column that differentiates the subtypes (Hunter, Policeman). This improves readability.
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "person_type")
public class Person {
// ...
}
How often do you need to access the dog associated with a Policeman? If it's very frequent, the performance benefit of your current approach becomes more significant.
If you anticipate a massive number of Hunters compared to Policemen, the wasted space due to NULLs in the dog_id
column might become more of a concern. However, with modern databases, this is usually not a major issue unless you're dealing with truly enormous datasets.