4 Replies Latest reply on Jun 26, 2008 12:25 PM by Chris A.

    LinkedList as @DataModel- ClassCastException

    Chris A. Newbie

      Hi,
      I have a small problem with using @DataModel.


      Here is my code:
      web page:



      <rich:dataTable sortMode="multi" id="tableId"                         
             width="100%" rows="10" value="#{dane}" var="row">
       <rich:column sortBy="#{row.value1}">
         <f:facet name="header">
              <h:outputText styleClass="headerText" value="First Name" />
         </f:facet>
         <h:outputText value="#{row.value1}"/>
       </rich:column>
       ...
      </rich:dataTable>




      Java:


      @Stateless
      @Scope(ScopeType.SESSION)
      @Name("testData")
      public class TestData implements TestDataLocal {
          @DataModel
          @Out(required=false)
          private List<SampleDataDTO> dane;
         
          
          @Factory("dane")
          public void pupulateDane(){
              dane = new LinkedList<SampleDataDTO>();
              dane.add(new SampleDataDTO("1","2","3"));
          }
          ...
      }





      @Local
      public interface TestDataLocal {  
         ...
         public void pupulateDane();
         ...
      }




      DTO:


      public class SampleDataDTO {
              private String value1;
              private String value2;
              private String value3;
              
              public SampleDataDTO(String value1, String value2, String value3){
                      this.value1 = value1;
                      this.value2 = value2;
                      this.value3 = value3;
              }
      
              public String getValue1() {
                      return value1;
              }
      
              public void setValue1(String value1) {
                      this.value1 = value1;
              }
      
              public String getValue2() {
                      return value2;
              }
      
              public void setValue2(String value2) {
                      this.value2 = value2;
              }
      
              public String getValue3() {
                      return value3;
              }
      
              public void setValue3(String value3) {
                      this.value3 = value3;
              }
      }




      When I enter the page I get an ClassCastException.


      ...
      Caused by: java.lang.ClassCastException: java.util.LinkedList
              at org.jboss.seam.databinding.DataModelBinder.isDirty(DataModelBinder.java:14)
              at org.jboss.seam.Component.outjectDataModel(Component.java:1537)
              at org.jboss.seam.Component.outjectDataModels(Component.java:1522)
              at org.jboss.seam.Component.outject(Component.java:1474)
              at org.jboss.seam.core.BijectionInterceptor.aroundInvoke(BijectionInterceptor.java:47)
              at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
              at org.jboss.seam.core.MethodContextInterceptor.aroundInvoke(MethodContextInterceptor.java:42)
              at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
              at org.jboss.seam.persistence.EntityManagerProxyInterceptor.aroundInvoke(EntityManagerProxyInterceptor.java:26)
              at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
              at org.jboss.seam.persistence.HibernateSessionProxyInterceptor.aroundInvoke(HibernateSessionProxyInterceptor.java:27)
              at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
              at org.jboss.seam.intercept.RootInterceptor.invoke(RootInterceptor.java:107)
              at org.jboss.seam.intercept.SessionBeanInterceptor.aroundInvoke(SessionBeanInterceptor.java:50)
              at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
              at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
              at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
              at java.lang.reflect.Method.invoke(Method.java:585)
              at org.jboss.ejb3.interceptor.InvocationContextImpl.proceed(InvocationContextImpl.java:118)
              at org.jboss.ejb3.interceptor.EJB3InterceptorsInterceptor.invoke(EJB3InterceptorsInterceptor.java:63)
              at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
              at org.jboss.ejb3.entity.TransactionScopedEntityManagerInterceptor.invoke(TransactionScopedEntityManagerInterceptor.java:54)
              at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
              at org.jboss.ejb3.AllowedOperationsInterceptor.invoke(AllowedOperationsInterceptor.java:47)
              at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
              at org.jboss.aspects.tx.TxPolicy.invokeInCallerTx(TxPolicy.java:126)
              ... 118 more




      In all examples I have seen factory method used .getResultList() from Query. Why can't I use LinkedList?
      Is it possible to have a List as a DataModel but not containing Entity Beans ?

        • 1. Re: LinkedList as @DataModel- ClassCastException
          Benjamin Graf Novice

          Did you try not to use @DataModel and @Out. I think that should solve your problem!

          • 2. Re: LinkedList as @DataModel- ClassCastException
            Chris A. Newbie

            It works if I change the code like that:


            @Stateless
            @Scope(ScopeType.SESSION)
            @Name("testData")
            public class TestData implements TestDataLocal {
            
                private List<SampleDataDTO> dane;
               
                
                @Factory("dane")
                public List<SampleDataDTO> pupulateDane(){
                    dane = new LinkedList<SampleDataDTO>();
                    dane.add(new SampleDataDTO("1","2","3"));
                    return dane;
                }
                ...
            }



            But I think it does not solve my problem.
            I also want to use @DataModelSelection. Correct me if I am wrong but I need @DataModel for that.

            • 3. Re: LinkedList as @DataModel- ClassCastException
              Alexander Torstling Newbie

              When you have both @DataModel and @Out, they will both outject your list into the context under the name dane. The @DataModel outjection will wrap your list in a ListDataModel. The @Out will simply outject the list as-is. The exception your are getting is when these two objects get mixed up (trying to cast the ListDataModel to List or the other way around).


              If you want to outject a variable both as a @DataModel and @Out, you have to give them different names, something like


              @DataModel("daneModel")
              @Out("dane")
              private List<SampleDataDTO> dane;
              



              This way, there will be no confusion about the type of the context variables.

              • 4. Re: LinkedList as @DataModel- ClassCastException
                Chris A. Newbie

                I wrote that based on example from Core JavaSercer Faces book.
                (It seems like I should have look into seam examples where @DataModel is used without @Out annotation.)
                It works without @Out.
                Thank you for your explanation!