如何在p:dataTable中显示实体

时间:2014-01-20 16:39:02

标签: jsf jpa primefaces

我有两个表:question(id_question,id_form,id_type,question,count)form(id_form,id_user,name,date_creation,start_date,end_date)。我想展示所有问题。我有id_form。那么,如果我有id_form,我怎样才能从问题表中显示我的所有问题。

这是我到目前为止所写的:

FormBean中的代码:

public String loadToView() {
    EntityManager em = DBManager.getManager().createEntityManager();
    this.form = em.find(Form.class, form.getIdForm());
    em.close();
    return "viewForm.xhtml";
}

我知道我必须以某种方式得到问题。 我想在vieForm.xhtml上我可以使用DataTable形式的PrimeFaces UI。 你能给我一些帮助或提供我如何解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

从你的问题看来,表格和问题表之间有很多对一的关系。所以我理解的是一个表格可以有很多问题,更清楚的表格id可以在问题表中相似。

为了通过在JPA的Form类上调用em.find来加载所有问题,您需要在JPA中映射这些实体。

在你的情况下,它将是:

 @Entity
public class Form{
  @Id
  private long id;
  ...
  @OneToMany(mappedBy="form",fetch = FetchType.LAZY, cascade = CascadeType.ALL)
  @MapKey(name="type")
  private Map<String, Question> question;
  ...
}

@Entity
public class Question{
  @Id
  private long id;

  ...
  @ManyToOne
  private Form form;
  ...
}

在上面的问题中,对于Map问题有getter和setter,你也可以使用List来代替Map。