Is it possible to create a list of SelectItems for a selectOneMenu that
contains parent-class objects...and use them?  Basically, let's say I'm
running Glassfish and I have a base class called ClassA.  Via EJB 3.0
(or JPA if you prefer to call it that), I am using a single table with a
discriminator column to define multiple classes.  Now, let's say I have
ClassB and ClassC as subclasses of Class A.  Can I create a list of
SelectItems (see code below) by performing a query for all ClassA
objects?  Well, I partly know the answer...I can create the list, and I
can view those selectitems on a JSF page.  I can even select one and
submit the page (well, that's where it chokes).  The problem is,
somewhere after I hit the "Process Validations" phase, my application
server chokes when it tries to convert the selected object to the
appropriate type.  

 

I get this:

Cannot convert objectB of type class
com.my.host.persistence.entity.ClassB to class
com.my.host.persistence.entity.ClassC

 

Followed by:

java.lang.IllegalArgumentException: Cannot convert objectB of type class
com com.my.host.persistence.entity.ClassB to class
com.my.host.persistence.entity.ClassC

        at com.sun.el.lang.ELSupport.coerceToType(ELSupport.java:367)

        at
com.sun.el.ExpressionFactoryImpl.coerceToType(ExpressionFactoryImpl.java
:52)

        at
javax.faces.component.UISelectOne.matchValue(UISelectOne.java:177)

        at
javax.faces.component.UISelectOne.validateValue(UISelectOne.java:137)

 

 

This is how I am making my list of SelectItems:

 

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

        EntityManager em = getEntityManager();

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

        em.close();

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

        int i = 0;

        for(ClassA x : l) {

                select[i++] = new SelectItem(x, x.getName());

        }

        return select;

    }

 

My base class converter (for ClassA) _is_ in fact called, and it shows
that it is getting the appropriate string value of the selected
selectitem when the page is submitted...

 

What's driving me nuts (besides that this isn't working), is that if I
select another item from the dropdown, such as objectC, I get an
exception that says it can't convert objectC of class ClassC to class
ClassB.  If I change my SelectItems to only contain objects of one type,
everything works just fine.  Does anybody know how I can accomplish what
I am attempting?  I've spent the better part of the day trying to figure
this out, including searching the abyss of mail lists, blogs and
whatever else Google can throw at me...and nowhere have I found an
example using a base class (i.e., superclass) for SelectItems.  How do I
handle this kind of conversion?  Is there a better way to do what I
want?

 

 

Tim 

Reply via email to