This is the parent class
@MappedSuperclass
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
public class APostCommon extends Actionable {
private static final long serialVersionUID = 1L;
@Column(name = "TITLE")
private String title;
@Lob
@Column(name="DESCRIPTION")
private String description;
//omitted others for purity
}
This is a child class
@Entity
@Table(name="QUESTION")
public class Question extends APostCommon {
private static final long serialVersionUID = 1L;
@Transient
private List<Answer> answers;
@Column(name="FOLLOW_COUNT")
private Integer followerCount;
@Column(name="ANSWER_COUNT")
private Integer answerCount;
//omitted others for purity
}
Answer
only differs from the superclass by including the questionId
field
@Entity
@Table(name="ANSWER")
public class Answer extends APostCommon{
private String questionId;
//omitted others for purity
}
Which data model is suitable for me. Should I use InheritanceType.SINGLE_TABLE
or TABLE_PER_CLASS
. Whats happen when there is millions record in future.
As i was preparing for JPA Certificate, these are my cherry picked notes on both strategies:
Single Table Strategy
The approach may be more wasteful of database tablespace, but it does offer peak performance for both polymorphic queries and write operations.
The SQL that is needed to issue these operations is simple, optimized and does not require joining
Joined Strategy
Mapping a table per entity provides the data reuse that a normalized data schema offers and is the most efficient way to store data, that is shared by multiple subclasses in the hierarchy.
Conclusion:
Initially i was leaning towards the Single Table, but as i give myself a bit of time, it made realize that would be a 'lazy' decision (put everything in one bag and forget about design).Joined Tables seems more of a mature decision as you need to think about your indexing strategy, into how many tables you should normalize (and which groups of data) and finally coming up with efficient queries, batch statements.
Decision is up to you, but if you go for the joined strategy you will definately increase your skillset as it will be more demanding.