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

  • Home
  • SEARCH
  • 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 3311302
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T21:51:39+00:00 2026-05-17T21:51:39+00:00

I have one Entity call Comment Comment + id (PK) + fromUserId + targetId

  • 0

I have one Entity call Comment

Comment
+ id (PK)
+ fromUserId
+ targetId
+ replyId : This is the foreign key to id

The idea is a comment can have many replies, but a reply is also a comment. To distinguish between the two, a comment will have replyId equal -1, meanwhile a reply will have a non -1 value for replyId. Ultimately, what I want is for Comment to have a List like this

List<Comment> replyList;  //So each comment will store all their replies into this List

In JPA, I accomplish OneToMany relationship between two Entity, I create a List with the help of @OneToMany and @ManyToOne annotation, but in this case I am not sure how to accomplish this. Please help

EDIT
They way I create a Comment – Reply layout is by having one dataTable inside another

<h:form id="table">
<p:dataTable value="#{bean.comments}" var="item">
    <!-- Original Comment -->
    <p:column>
        <h:outputText value="#{item.comment}" />
    </p:column>

    <!-- Reply -->
    <p:column>
        <p:dataTable value="#{item.replies}" rendered="#{item.replies.size() > 0}" var="item2">
             <!-- Print out replies in here -->
             <h:outputText value="#{item2.comment}" />
        </p:dataTable>

        <!-- Text box and commandButton so user can add new reply -->
        <h:inputTextarea value="#{...}" />
        <p:commandButton value="Post" actionListener="#{bean.addReply(item)}" />
        <!-- Assume that I got this working, that a new Comment with correct REPLYTO_ID set -->
    </p:column>
</p:dataTable>
</h:form>

Here is my problem. When I type a reply and click Post, it creates an item entry in my database correctly, and after that recall getReplies(). I was assume at this point, replies.size() == 1, however replies.size() is equals to 0. Therefore I see nothing. I have to refresh the page, to see it display correct. I kind of see the problem here, since I generated the value of the second table by #{item.replies}, therefore if item is not update to date, then replies is not update to date. Not sure if this is JSF or JPA problem

  • 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-17T21:51:40+00:00Added an answer on May 17, 2026 at 9:51 pm

    You can map a self referencing entity like this:

    @Entity
    public class Comment {
        @Id
        private Long id;
    
        @ManyToOne(optional=true, fetch=FetchType.LAZY)
        @JoinColumn(name="REPLYTO_ID")
        private Comment replyTo;
    
        @OneToMany(mappedBy="replyTo")
        private List<Comment> replies = new ArrayList<Comment>();
    
        //...
    
        // link management method
        public void addToComments(Comment comment) {
            this.comments.add(comment);
            comment.setParent(parent);
        }
    }
    

    Where the root Comment has no replyTo.

    PS: make sure to set the bi-directional association correctly.

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

Sidebar

Related Questions

No related questions found

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.