user1926688
user1926688

Reputation:

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.

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 i.e "#{issues.issueType}" is a foreign key, I want to have value associated with this in another table to be displayed in that place.

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.

Bean code

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 . Please assist me in this issue, Is there any simple way to go with this?

Upvotes: 4

Views: 1060

Answers (1)

BalusC
BalusC

Reputation: 1109865

You aren't creating a new issueDisplayDTO instance during every iteration. 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.

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. It's just basic 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.


Unrelated to the concrete problem, consider using an enum instead of int. And, that list.get(i) call everytime is really inefficient. Get hold of it only once.

Upvotes: 2

Related Questions