【发布时间】:2014-06-29 09:11:42
【问题描述】:
我想使用 p:rating 组件来投票并将值保存到数据库中(如果它已经被投票以便以后能够从数据库中提取值)。在实体中,我也有 cmets 字段,我想如果 cmets 已经编写为仅添加投票,所以我有时会进行合并而不是持久化(如果数据库中没有写入此请求)。 我的 .xhtml 页面中有以下代码:
<h:form
rendered="#{not empty userRequestBean.request.hiredAgency.city}">
<p:rating value="#{userRequestBean.rating}" stars="10"
onRate="#{userRequestBean.rateAgency()}">
</p:rating>
</h:form>
在我的 bean 中我有:
@ManagedBean(name = "userRequestBean")
@SessionScoped
public class UserRequestBean implements Serializable {
private Integer rating; // Plus get and set methods
private TComment agencyComment = new TComment();
public void rateAgency() {
if (rating == null) return;
EntityManager em = HibernateUtil.getEntityManager();
if (!em.getTransaction().isActive())
em.getTransaction().begin();
Query queryAgencyComment = em.createQuery("select comment "
+ "from TRequest req join req.requestComments comment "
+ "where req.id = :requestId ");
Long requestId = (Long) request.getId();
queryAgencyComment.setParameter("requestId", requestId);
agencyComment = (TComment) queryAgencyComment.getSingleResult();
if (agencyComment.equals(null)) {
agencyComment = new TComment();
agencyComment.settRequest(request);
agencyComment.setCommentDate(new Date());
agencyComment.setAssessment(rating);
em.persist(agencyComment);
em.getTransaction().commit();
} else {
agencyComment.setAssessment(5);
em.merge(agencyComment);
em.getTransaction().commit();
}
}
但是onRate,方法rateAgency没有执行。它只在页面渲染时执行一次。我怎样才能实现这个工作?
【问题讨论】:
标签: java jsf jsf-2 primefaces