我有實體:帖子,用戶,評論有雙向關係:EntityManager不從數據庫中提取?
--------- 1 * ---------
| Post | <--------> |Comment|
--------- ---------
--------- 1 * ---------
| User | <--------> |Comment|
--------- ---------
我有簡單的網頁,其中顯示一個帖子及其所有評論和誰寫他們的用戶。 在底部我有簡單的textarea當登錄用戶可以發表評論。當提交被單擊時,將調用從backing bean調用的saveComment方法。現在我鍵入評論點擊提交操作saveComment導航到同一頁面。 saveComment看起來是這樣的:
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看起來是這樣的:
<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>
然而,當我點擊提交評論保存到數據庫中,但它不是渲染。不是post = postBean.getPost(post.getId())
這基本上是return em.find(Post.class, id)
應該重新加載post
,所以應該有新的評論?
還有支持bean的viewPost動作,它執行相同的操作post = postBean.getPost(post.getId())
,但似乎又沒有從數據庫加載數據。我從數據庫中刪除了評論,但他們仍然列在視圖頁面中。
'viewpost?faces-redirect = true'的作品。 – l245c4l 2010-09-06 16:55:53
這是JSF 2.0的一部分。隱式導航。沒有faces-config了。 '?faces-redirect = true'取代舊式導航案例中的' '。 –
BalusC
2010-09-06 17:21:44
如何禁用緩存? – l245c4l 2010-09-06 22:16:50