在dataTable中显示onetomany关系

时间:2013-10-18 13:40:54

标签: jsf primefaces datatable one-to-many

我有两类电子邮件和数据。他们之间的关系是OneToMany。在数据的数据表中,我想显示它包含的电子邮件列表。我怎样才能做到这一点? 这些是我的实体的一部分:

  @Entity
@Table(name = "EMAILS")
public class Email implements Serializable { 
    @Id
    @Column(name = "id")
    private String id;
    @Column(name = "extraction_Date")
    @Temporal(TemporalType.TIMESTAMP)
    private Date date;
    @Size(max = 255)
    @Column(name = "email")
    private String email;
    @ManyToOne(cascade = CascadeType.PERSIST)
    private Data dataBase;

    public Email() {
        this.date = new Date();
        this.id = IdGenerator.IdGenerathorCall();

    }
    @Entity
@Table(name = "DATAS")
public class Data implements Serializable {

    @Id
    @Column(name = "id")
    private String id;
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "creation")
    private Date date;
    @Column(name = "timeofextraction")
    private float grabduration;
    @Column(name = "keyword")
    private String keyword;
    @Column(name = "number")
    private int number;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "dataBase", fetch = FetchType.EAGER)
    private List<Email> emails;

    public Data() {
        super();
        this.date = new Date();
        this.id = IdGenerator.IdGenerathorCall();
    }

我的页面dataList.xhtml看起来像:

 <p:dataTable value="#{dataMB.datas}" var="item" id="datas"  rowsPerPageTemplate="5,10,15,20,25,30" 
                                      paginator="true" rows="10" filteredValue="#{dataMB.filteredDatas}"
                                      selectionMode="single" rowKey="#{item.id}"
                                      selection="#{dataMB.selectedData}">
                        <p:ajax event="rowSelect" update=":form:dataView , :form:confirmDelete"/>
                        <p:column sortBy="#{item.date}" filterBy="#{item.date}">
                            <f:facet name="header">
                                <p:outputLabel value="Creation"/>
                            </f:facet>
                            <p:outputLabel value="#{item.date}">
                                <f:convertDateTime pattern="MM/dd/yyyy HH:mm:ss" />
                            </p:outputLabel>
                        </p:column>
                        <p:column sortBy="#{item.grabduration}" filterBy="#{item.grabduration}">
                            <f:facet name="header">
                                <h:outputText value="Time of execution"/>
                            </f:facet>
                            <h:outputText value="#{item.grabduration}"/>
                        </p:column>
                        <p:column sortBy="#{item.keyword}" filterBy="#{item.keyword}">
                            <f:facet name="header">
                                <h:outputText value="Keyword"/>
                            </f:facet>
                            <h:outputText value="#{item.keyword}"/>
                        </p:column>
                        <p:column sortBy="#{item.number}" filterBy="#{item.number}">
                            <f:facet name="header">
                                <h:outputText value="Number"/>
                            </f:facet>
                            <h:outputText value="#{item.number}"/>
                        </p:column>      
                          <p:column sortBy="#{item.emails}" filterBy="#{item.emails}">
                            <f:facet name="header">
                                <h:outputText value="Emails List"/>
                            </f:facet>
                            <h:outputText value="#{item.emails}"/>
                        </p:column>  
                    </p:dataTable> 

当我显示dataTable时,包含电子邮件列表的列为空,不会显示任何内容。

1 个答案:

答案 0 :(得分:1)

您正尝试在单个值组件中呈现列表。

将您的电子邮件列更改为:

<p:column headerText="Emails List">
    <p:dataList  value="#{item.emails}" var="email" itemType="none">
        #{email.email}
    </p:dataList>
</p:column>

除此之外,您还需要制作一个辅助方法来对电子邮件进行排序和过滤。