【发布时间】:2010-10-27 02:52:05
【问题描述】:
我有一个实体调用Comment
Comment
+ 身份证 (PK)
+ fromUserId
+ 目标ID
+ replyId : 这是 id
这个想法是comment 可以有很多回复,但reply 也是comment。为了区分这两者,comment 的 replyId 等于 -1,同时 reply 的 replyId 的值为非 -1。最终,我想要Comment 拥有这样的List
List<Comment> replyList; //So each comment will store all their replies into this List
在 JPA 中,我完成了两个实体之间的 OneToMany 关系,我在 @OneToMany 和 @ManyToOne 注释的帮助下创建了一个列表,但在这种情况下我不知道如何完成这个。请帮忙
编辑
我创建评论的方式 - 回复布局是将一个数据表放在另一个数据表中
<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>
这是我的问题。当我输入回复并单击Post 时,它会在我的数据库中正确创建一个项目条目,然后调用getReplies()。此时我假设replies.size() == 1,但是replies.size() 等于0。因此我什么也看不到。我必须刷新页面,才能看到它显示正确。我在这里看到了问题,因为我通过#{item.replies} 生成了第二个表的值,因此如果item 没有更新到最新,那么replies 没有更新到最新。不确定这是 JSF 还是 JPA 问题
【问题讨论】:
标签: jpa jakarta-ee one-to-many