1

I want to display ArrayList of objects into JSF table:

I created this Object:

public List<TData> td = new ArrayList<TData>();

    public class TData {

        private long id;
        private String key;
        private String language;
        private String translation;

        public TData(){};

        public TData(long id, String key, String language, String translation) {
            // super();
            this.id = id;
            this.key = key;
            this.language = language;
            this.translation = translation;
        }

        public long getId() {
            return id;
        }

        public void setId(long id) {
            this.id = id;
        }

        public String getKey() {
            return key;
        }

        public void setKey(String key) {
            this.key = key;
        }

        public String getLanguage() {
            return language;
        }

        public void setLanguage(String language) {
            this.language = language;
        }

        public String getTranslation() {
            return translation;
        }

        public void setTranslation(String translation) {
            this.translation = translation;
        }

    }

    public List<TData> getDataObj() {
        return td;
    }

    @PostConstruct
    public void loadData() {

        String query = "SELECT c FROM TranslationTestEntitie c";
        List<Object> result =  dao.jpqlQuery(query, 1, 0);

        for(int i =0; i < result.size(); i++){
            TranslationTestEntitie test = (TranslationTestEntitie) result.get(i);
            td.add((TData) result.get(i));

        }

    }

Then I use this datatable to display the result;

<h:dataTable value="#{languageBeanTest.dataObj}" var="o">

        <h:column>
            <f:facet name="header">Id</f:facet>
                    #{o.id}
                </h:column>

        <h:column>
            <f:facet name="header">key</f:facet>
                    #{o.key}
                </h:column>

        <h:column>
            <f:facet name="header">language</f:facet>
                    #{o.language}
                </h:column>

        <h:column>
            <f:facet name="header">translation</f:facet>
                    #{o.translation}
                </h:column>

    </h:dataTable>

I get this error:

java.lang.ClassCastException: callflow.cfdbc_common.entities.TranslationTestEntitie cannot be cast to callflow.ccadmin.beans.LanguageBeanTest$TData

I get the result from the DB as List of Objects. How I can display these Objects into JSF page?

P.S This is the source code of the entitie

@Entity
@Table(name = "Translations")
public class TranslationTestEntitie extends AbstractEntityWithId {

    private static final long serialVersionUID = 2029240552230401080L;

    @Column(name = "id", insertable=false, updatable=false)
    private long id;
    @Column(name = "key", insertable=false, updatable=false)
    private String key;
    @Column(name = "language", insertable=false, updatable=false)

    private String language;
    @Column(name = "translation", insertable=false, updatable=false)
    private String translation;

    public TranslationTestEntitie() {
    }

    // Getters and setters
    public Long getId() {
        return id;
    }

    //@Transient    Field will not be saved in database
    public void setId(long id) {
        this.id = id;
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public String getLanguage() {
        return language;
    }

    public void setLanguage(String language) {
        this.language = language;
    }

    public String getTranslation() {
        return translation;
    }

    public void setTranslation(String translation) {
        this.translation = translation;
    }

}

I suppose that the Object returned by the JPA will be the same as the TData because I have the same object attributes.

1
  • Why don't you use a List<TranslationTestEntitie> instead of List<TData>? Commented Mar 20, 2013 at 13:41

1 Answer 1

2

Apparantly, the objects returned are of type TranslationTestEntitie, and not of TData. You'll need to write the code to transform a TranslationTestEntitie into an object of type TData.

TranslationTestEntitie test = (TranslationTestEntitie) result.get(i);
TData newTData = new TData(test.getId(), test.getKey(), test.getLanguage(),  test.getTranslation());
td.add(newTData);

If it has the same fields anyway, why can't you use the TranslationTestEntitie itself?

List<TranslationTestEntitie> td=  new ArrayList<TranslationTestEntitie>();

for(int i =0; i < result.size(); i++){
        TranslationTestEntitie test = (TranslationTestEntitie) result.get(i);
        td.add(test);
}
Sign up to request clarification or add additional context in comments.

7 Comments

I updated the post. The two objects have the same attributes.
That doesn't change the answer :) You'll have to write a method that creates a new TData object with the values from your TranslationTestEntitie object.
I'm new to this technology, would you please give me some more information how I can solve this problem.
If it has the same fields anyway, why can't you use the TranslationTestEntitie itself? How I can do that?
You can create a List of TranslationTestEntitie (instead of TData), and simply add your result to it in the loop.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.