Hello,
I have entities: Post, User, Comment with bidirectional relationships:
--------- 1 * ---------
| Post | <--------> |Comment|
--------- ---------
--------- 1 * ---------
| User | <--------> |Comment|
--------- ---------
I have simple page which displays single post and all its comments and users who wrote them. At the bottom I have simple textarea when logged in user can post comment. When Submit is clicked method saveComment from backing bean is invoked. Now I type comment click Submit action saveComment navigate to the same page. saveComment looks like this:
String username = getLoginName();
if(username != null)
{
User u = userBean.getUser(username);
post = postBean.getPost(post.getId());
comment.setPost(post);
comment.setUser(u);
commentBean.updateComment(comment);
post = postBean.getPost(post.getId());
}
return "viewpost?faces-redirect=true";
viewpost looks like this:
<ui:define name="content">
<h:outputText value="#{postController.post.title}"/>
<br/>
<h:outputText value="#{postController.post.body}"/>
<br/>
<h:dataTable value="#{postController.post.comments}" var="item">
<h:column>
<h:outputText value="#{item.body}"/>
<br/>
<h:outputText value="#{item.user.username}"/>
<br/>
</h:column>
</h:dataTable>
<h:form rendered="#{postController.loggedIn}">
<h:inputTextarea value="#{postController.comment.body}"/>
<h:commandButton value="Submit" action="#{postController.saveComment}"/>
</h:form>
</ui:define>
However when I click Submit comment is saved to the database but its not rendered. Doesn't post = postBean.getPost(post.getId())
which is basically return em.find(Post.class, id)
should reload post
, so that new comment should be available?
There is also viewPost action in backing bean which does the same post = postBean.getPost(post.getId())
, but again it seems like data is not loaded from database. I deleted comments from database but they still were listed in viewpost page.