users@glassfish.java.net

Can f:selectItems take a collection of abstract types?

From: Romanowski, Tim <tim.romanowski_at_lmco.com>
Date: Wed, 07 Feb 2007 19:36:35 -0500

In my JSF app (Glassfish V2 b34), I am trying to generate a
SelectManyListbox that is built from an array of SelectItems. The problem
I'm having is that the select items are built from entities that are part of
an inheritance model and I am getting a conversion error when I try to
submit my form. Simply, I am trying to do something like this:

 

    public void setEmployees(Employee[] emp_items) {

        for( Employee e : emp_items ){

           empCollection.add(e);

        }

    }

 

In this example, Employee is an abstract base class.

 

The emp_items are constructed like so:

 

    public javax.faces.model.SelectItem[] getEmpSelectItems() {

        EntityManager em = getEntityManager();

        List <Employee> l = (List < Employee >) em.createQuery("select o
from Employee as o").getResultList();

        em.close();

        SelectItem select[] = new SelectItem[l.size()];

        int i = 0;

        for(Employee e : l) {

                select[i++] = new SelectItem(e);

        }

        return select;

    }

 

My inheritance model uses the single-table strategy, whereby I have a
discriminator column. Now, let's say I have the subtypes Programmer and
SlaveDriver, each being a concrete subclass of Employee. In my JSF app, I
can create the SelectItems without any issues. My page with the
SelectManyListbox is displayed fine. However, when I select an item from
that listbox and submit my form, I get a conversion error. The id (that is,
the primary key) of the selected object is displayed as follows:

 

Conversion Error setting value '25' for '#{employeeSearchForm.employees}'.

 

 

I've tried solving this many ways:

  First, I tried using a converter for the abstract class, Employee. That
did not work.

  Second, I created a converter for each concrete subclass, and in my JSF
code, I simply did not specify a converter for the SelectManyListbox,
instead letting Glassfish figure it out. It turns out that Glassfish is
pretty smart =-) and called the appropriate converter for each item in the
SelectItems array when the SelectManyListbox is constructed. Unfortunately,
upon submittal of the form, I get a conversion error like the one above.

 

So, my question: Is there any way, let alone a good way, to do what I am
trying to do: create a listbox of objects belonging to a common base class,
and submit a form with multiple items selected? I should add that in my
current setup, the setEmployees method doesn't appear to be called in my
backing bean (or if it is, the point at which the conversion takes place is
clearly choking). Any suggestions?

 


Tim