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

user1926688

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?

BalusC

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to get filtered value on Primefaces lazy datatable?

From Dev

Primefaces RowEditor not changing value in Datatable

From Dev

How to make two foreign keys to same model unique together?

From Dev

How to make query with grouping by foreign keys with ordering by sum of field

From Dev

How to make Ruby on Rails create SQLite database with foreign keys enabled?

From Dev

How to make UserID and ProductID columns as the foreign keys in Downloads Table?

From Dev

How should I name and make foreign keys in Rails 5.1?

From Dev

How to apply style to a PrimeFaces dataTable?

From Dev

How to disable entire Primefaces dataTable

From Dev

How to make the value of the array of keys and values in php?

From Dev

How to make the value of the array of keys and values in php?

From Dev

How can we make an inputText accept Numbers where we had currencySymbol="$" in a DataTable,in JSF, Primefaces?

From Dev

Display key and value Map in primefaces datatable

From Dev

Display key and value Map in primefaces datatable

From Dev

Failure to make Foreign keys with two tables

From Dev

Cannot make 2 foreign keys, "errno: 150"

From Dev

Failure to make Foreign keys with two tables

From Dev

make mode inherit multiple foreign keys on creation

From Dev

How to define foreign keys in migrations?

From Dev

How to set foreign keys in Firestore?

From Dev

How to select the items with foreign keys?

From Dev

How to define foreign keys in migrations?

From Dev

How to save foreign keys (SQL)

From Dev

How to update a specific cell on a PrimeFaces dataTable

From Dev

How to iterate a HashMap with primefaces selectable datatable

From Dev

How to validate a List<Object> in a Primefaces dataTable - JSF

From Dev

How to update datatable in tab of accordion (primefaces)

From Dev

How to ajax update an item in the footer of a PrimeFaces dataTable?

From Dev

How to ajax update an item in the footer of a PrimeFaces dataTable?

Related Related

  1. 1

    How to get filtered value on Primefaces lazy datatable?

  2. 2

    Primefaces RowEditor not changing value in Datatable

  3. 3

    How to make two foreign keys to same model unique together?

  4. 4

    How to make query with grouping by foreign keys with ordering by sum of field

  5. 5

    How to make Ruby on Rails create SQLite database with foreign keys enabled?

  6. 6

    How to make UserID and ProductID columns as the foreign keys in Downloads Table?

  7. 7

    How should I name and make foreign keys in Rails 5.1?

  8. 8

    How to apply style to a PrimeFaces dataTable?

  9. 9

    How to disable entire Primefaces dataTable

  10. 10

    How to make the value of the array of keys and values in php?

  11. 11

    How to make the value of the array of keys and values in php?

  12. 12

    How can we make an inputText accept Numbers where we had currencySymbol="$" in a DataTable,in JSF, Primefaces?

  13. 13

    Display key and value Map in primefaces datatable

  14. 14

    Display key and value Map in primefaces datatable

  15. 15

    Failure to make Foreign keys with two tables

  16. 16

    Cannot make 2 foreign keys, "errno: 150"

  17. 17

    Failure to make Foreign keys with two tables

  18. 18

    make mode inherit multiple foreign keys on creation

  19. 19

    How to define foreign keys in migrations?

  20. 20

    How to set foreign keys in Firestore?

  21. 21

    How to select the items with foreign keys?

  22. 22

    How to define foreign keys in migrations?

  23. 23

    How to save foreign keys (SQL)

  24. 24

    How to update a specific cell on a PrimeFaces dataTable

  25. 25

    How to iterate a HashMap with primefaces selectable datatable

  26. 26

    How to validate a List<Object> in a Primefaces dataTable - JSF

  27. 27

    How to update datatable in tab of accordion (primefaces)

  28. 28

    How to ajax update an item in the footer of a PrimeFaces dataTable?

  29. 29

    How to ajax update an item in the footer of a PrimeFaces dataTable?

HotTag

Archive