简体   繁体   English

如何使值的外键显示在primefaces数据表中

[英]How to make foreign keys of a value be displayed in primefaces datatable

I have a primefaces data-table , where one column's value is a foreign key , I want to display the value which is attached to the foreign key in another database table to be displayed in that column of the data-table. 我有一个primefaces数据表,其中一列的值是外键,我想显示附加到另一个数据库表中的外键的值,以显示在数据表的该列中。

issue.xhtml issue.xhtml

<p:dataTable id="dataTable" var="issues" value="#{displayIssuesBean.issues}"
                scrollable="true" style="width:1000px;" emptyMessage="#{msg['prometheus_issuesdisplayemptymessage']}">
                <f:facet name="header">#{msg['prometheus_issues']}</f:facet>
                <p:column headerText="#{msg['prometheus_number']}">
                    <h:outputText value="#{issues.id}" />
                </p:column>

                <p:column headerText="#{msg['prometheus_title']}">
                    <h:outputText value="#{issues.issueTitle}" />
                </p:column>

                <p:column headerText="#{msg['prometheus_type']}">
                    <h:outputText value="#{issues.issueType}" />
                </p:column>

                <p:column headerText="#{msg['prometheus_estimatedtime']}">
                    <h:outputText value="#{issues.estimationTime}" />
                </p:column>
            </p:dataTable>

In this datatable the third column value ie "#{issues.issueType}" is a foreign key, I want to have value associated with this in another table to be displayed in that place. 在这个数据表中,第三列值即“#{issues.issueType}”是一个外键,我希望在另一个表中具有与此关联的值以在该位置显示。

I tried to set values with another DTO along with the missing attribute and trid to set the values and again get but didn't get opted result. 我尝试使用另一个DTO设置值以及缺少的属性和trid来设置值并再次获取但没有选择结果。

Bean code Bean代码

public List<IssueDisplayDTO> getIssueDataToForm(List<IssueDBDTO> list) {
        for (int i = 0; i < list.size(); i++) {
            issueDisplayDTO.setIssueNumber(list.get(i).getId());
            issueDisplayDTO.setIssueType(setIssueTypeWithIssueTypeId(list.get(i).getId()));
            issueDisplayDTO.setTitle(list.get(i).getIssueTitle());
            issueDisplayDTO.setEstimatedTime(list.get(i).getEstimationTime());
            modifiedList.add(issueDisplayDTO);
        }
        return modifiedList;
    }

    private String setIssueTypeWithIssueTypeId(int issueTypeId) {
        String type=null;
        if (issueTypeId == 1){
            type="Bug";}
        else if (issueTypeId == 2)
            {type="Story";}
        else if (issueTypeId == 3)
            {type="Task";}
        return type;
    }

With the above code modifiedList is holding values until the for loop lasts but when passed or even set with setters and getters,the list is being filled with last record . 使用上面的代码,modifiedList保持值,直到for循环持续,但是当传递或甚至使用setter和getter设置时,列表将填充最后一条记录。 Please assist me in this issue, Is there any simple way to go with this? 请帮助我解决这个问题,有什么简单的方法可以解决这个问题吗?

You aren't creating a new issueDisplayDTO instance during every iteration. 您不是在每次迭代期间创建新的issueDisplayDTO实例。 Instead, you're reusing the same instance and only changing its properties during every iteration. 相反,您重复使用相同的实例,并且只在每次迭代期间更改其属性。 So all added entries in the list will obviously all reference one and same issueDisplayDTO instance having the properties set during the last iteration round. 因此,列表中所有添加的条目显然都会引用同一个具有在上一轮迭代期间设置的属性的issueDisplayDTO实例。

You should be creating a new one during every iteration. 您应该在每次迭代期间创建一个新的。

for (int i = 0; i < list.size(); i++) {
    IssueDBDTO issueDisplayDTO = new IssueDBDTO(); // Here.
    issueDisplayDTO.setIssueNumber(list.get(i).getId());
    // ...
    modifiedList.add(issueDisplayDTO);
}

This problem has got nothing to do with MySQL, FK relationships, let alone with JSF/PrimeFaces. 这个问题与MySQL,FK关系无关,更不用说与JSF / PrimeFaces有关。 It's just basic Java. 它只是基本的Java。 You'd have had exactly the same problem when recreating the problem in a plain Java application with a main() method and presenting it using System.out.println() instead of a whole JSF web page. 在使用main()方法在普通Java应用程序中重新创建问题并使用System.out.println()而不是整个JSF网页呈现问题时,您会遇到完全相同的问题。


Unrelated to the concrete problem, consider using an enum instead of int . 具体问题无关 ,请考虑使用enum而不是int And, that list.get(i) call everytime is really inefficient. 并且, list.get(i)每次调用都是非常低效的。 Get hold of it only once. 只抓住一次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM