Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 919963
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T18:37:48+00:00 2026-05-15T18:37:48+00:00

I am creating a website that will have articles; each article will have comments.

  • 0

I am creating a website that will have articles; each article will have comments. These comments will be stored in a “comment” table with a field called “parent_id” that is a foreign key to the field “id” in the same table.

I am hoping to use Hibernate to recursively grab all of the comments for a specific article.

Here is my Entity:

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import org.hibernate.annotations.IndexColumn;

@Entity
public class Comment implements Serializable {
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private int id;
    @ManyToOne
    @JoinColumn(name="user_id")
    private User user;
    @OneToMany(targetEntity=Comment.class)
    @JoinColumn(name="parent_id")
    @IndexColumn(name="id", base=0)
    private List<Comment> comments = new ArrayList<Comment>();
    @Column(name="article_id", length=10)
    private int articleId;
    @Column(name="text", length=8192)
    private String text;

    public int getArticleId() {
        return articleId;
    }

    public void setArticleId(int articleId) {
        this.articleId = articleId;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }



    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    @Override
    public String toString() {
        return "Comment [" + "articleId " + articleId + " " + "id " + id + " "  + "text " + text + " " + "]";
    }


}

The code is “kinda” working, however, when all of the comments are retrieved the “child” list of comments contains the same number of elements as there are total comments (if that makes sense. For instance, assume I have only three comments in the table for article with id number 1… this article with 2 comments, and one of the comments has a child comment.. the array with the child comment has 3 entries, the first 2 are null and the last is the child comment.

Is this code correct?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T18:37:49+00:00Added an answer on May 15, 2026 at 6:37 pm

    I wonder if the problem is not coming from the fact that you are using the PK column as index column. I’d recommend to use a dedicated column for the index column. Something like this:

    @OneToMany
    @JoinColumn(name="parent_id")
    @IndexColumn(name="comments_index", base=0)
    private List<Comment> comments = new ArrayList<Comment>();
    

    Could you give this a try?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am creating a website that has 5 different sub-sites each with the same
I have been creating a website with Ruby on Rails, and will be hosting
I have an ASP.NET website that will hit about 2gb physical memory used within
I have a website that right now, runs by creating static html pages from
I'm creating an installer for a website that uses a custom event log source.
Are there any good books or website that go over creating a JTable ?
I'm creating a user-based website. For each user, I'll need a few MySQL tables
I am creating a desktop/client application that will be installed on +/- 5 PCs,
I am creating a website in CakePHP and I am kind of new on
I'm creating a website for my church and I'm having problems making it display

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.