0

I m trying to implement a application. In this application i have an entity name comment,answer,question. Question,answer and comment has comments so i m planning to create a Commentable class and all of three class can extend Commentable. In this commentable class it hold Set but i wonder this structure's performance. Because it will always create a join query to get comments. What if i create a seperate comment entity which has commentedObjectId and when comments are needed make query on seperate table like where commentedObjectId = id ?

    class Answer extends Commentable{}
    class Comment extends Commentable{}
    class Question extends Commentable{}

    class Commentable extends BaseModel{

    @OneToMany
    Set<Comment> comments;

    public Set<Comments> getComments() {
        return comments;
    }

    public void setComments(Set<Comment> comments) {
        this.comments = comments;
    }`

alternative for avoiding join operations. Should i denormalize database like that?

    class Comment extends BaseModel {
    String commentedObjectId;

    public String getcommentedObjectId() {
        return commentedObjectId;
    }

    public void setcommentedObjectId(String commentedObjectId) {
        this.commentedObjectId = commentedObjectId;
    }
}
ilkerbfl
  • 23
  • 1
  • 4

1 Answers1

0

You definitely should use domain objects. Hibernate allows to specify what to join or lazy-load.

To load just your Commentables without joining Comments you may use this query:

Criteria cirt = session.createCriteria(Commentable.class);
crit.setFetchMode("comments", FetchMode.LAZY);

@SuppressWarnings("unchecked")
List<Commentable> result = crit.list();
  • Thank you for your fast reply, but when fetching comments it will make join queries again. If the day comes when i have millions questions and millions comment. How this join query performs? – ilkerbfl Jan 19 '17 at 15:42
  • Still it will make join sql, the focus point is; i had better make denormalization for performance ? – ilkerbfl Jan 20 '17 at 13:45