Consider this scenario.
There are plots, some are residential plots and some are commercial plots.
There are also owners. But an owner can buy only a plot and it can be residential or commercial.
So, here is my code.
@Entity
@Table(name = "PLOT")
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class Plot {
private int id;
private String number;
private List<Owner> owners = new ArrayList<>();
// getters and setters...
}
@Entity
@Table(name = "RESIDENTIAL_PLOT")
@PrimaryKeyJoinColumn(name = "PLOT_ID")
public class ResidentialPlot extends Plot {
// Some fields
}
@Entity
@Table(name = "COMMERCIAL_PLOT")
@PrimaryKeyJoinColumn(name = "PLOT_ID")
public class CommercialPlot extends Plot {
// Some fields
}
@Entity
@Table(name = "OWNER")
public class Owner {
private int id;
private String name;
private Plot plot;
// getters and setters
}
All works well, but when I call owner.getPlot()
I was expecting ResidentialPlot or CommercialPlot instance
So, I can apply appropriate operation by using instanceof operator.
But it does not satisfy both conditions!
What am I doing wrong?