9 Replies Latest reply on Oct 11, 2007 7:37 AM by Pete Muir

    Can EntityConverter handle different EntityManagers?

    Jose Rubio Newbie

      Here's my situation, the application I'm wiriting uses two entityManagers. One is called entityManager and the other anotherEntityManager. I had a form on the page with a select that was populated from the anotherEntityManager. I was getting erros about the entity being unknown, and then I read about the entityConverter using entityManager by default.

      I see how I can change that, but what I want to do is tell the s:convertEntity at runtime what entityManager to use, because if I do it in the components.xml it will be hardcoded to that one. Unless there is a way to define to entityConverters then I will be fine with that approach also.

      Does my question make sense?

      Thanks,

      Jose

        • 1. Re: Can EntityConverter handle different EntityManagers?
          Jose Rubio Newbie

          I tried doing this in the xhtml itself:

          <s:convertEntity entityManager="#{anotherEntityManager}" />
          


          and it still wanted to go look for entityManager. Actually it gives that error if the variable name is not the same as the component name. If it I get this error:

          Caused by: java.lang.IllegalArgumentException: Cannot convert org.jboss.seam.persistence.EntityManagerProxy@41d260 of type class org.jboss.seam.persistence.EntityManagerProxy to interface org.jboss.seam.core.Expressions$ValueExpression
           at org.jboss.el.lang.ELSupport.coerceToType(ELSupport.java:358)
           at org.jboss.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:188)
           at com.sun.facelets.el.TagValueExpression.getValue(TagValueExpression.java:71)
           at com.sun.facelets.tag.TagAttribute.getObject(TagAttribute.java:233)
           ... 78 more
          


          Any ideas?

          • 2. Re: Can EntityConverter handle different EntityManagers?
            Stuart Douglas Master

            Try

            <s:convertEntity entityManager="anotherEntityManager" />


            don't know if it will work or not.

            • 3. Re: Can EntityConverter handle different EntityManagers?
              Jose Rubio Newbie

              that's what I tried. Our posts probably got crossed. If I tried without being an evaluation it gave me an error. It has to be #{anotherEntityManager}

              But what I'm finding is the the EnvtityConverterStore is looking for a component called entityManager.

              I'm going to try removing the parameter from the JSF to see if that is still the case.

              This is getting weird.

              • 4. Re: Can EntityConverter handle different EntityManagers?
                Jose Rubio Newbie

                If I don't have a component called "entityManager" I get a n NPE:

                java.lang.NullPointerException
                 at org.jboss.seam.ui.converter.EntityConverterStore.getEntityManager(EntityConverterStore.java:81)
                 at org.jboss.seam.ui.converter.EntityConverterStore.put(EntityConverterStore.java:60)
                 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:597)
                 at org.jboss.seam.util.Reflections.invoke(Reflections.java:21)
                 at org.jboss.seam.intercept.RootInvocationContext.proceed(RootInvocationContext.java:31)
                 at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:56)
                 at org.jboss.seam.transaction.RollbackInterceptor.aroundInvoke(RollbackInterceptor.java:31)
                 at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
                 at org.jboss.seam.transaction.TransactionInterceptor$1.work(TransactionInterceptor.java:38)
                 at org.jboss.seam.util.Work.workInTransaction(Work.java:39)
                 at org.jboss.seam.transaction.TransactionInterceptor.aroundInvoke(TransactionInterceptor.java:32)
                 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.intercept.RootInterceptor.invoke(RootInterceptor.java:106)
                 at org.jboss.seam.intercept.JavaBeanInterceptor.interceptInvocation(JavaBeanInterceptor.java:155)
                 at org.jboss.seam.intercept.JavaBeanInterceptor.invoke(JavaBeanInterceptor.java:91)
                 at org.jboss.seam.ui.converter.EntityConverterStore_$$_javassist_1.put(EntityConverterStore_$$_javassist_1.java)
                 at org.jboss.seam.ui.converter.EntityConverter.getAsString(EntityConverter.java:67)
                 at org.jboss.seam.ui.converter.PrioritizableConverter.getAsString(PrioritizableConverter.java:67)
                 at org.jboss.seam.ui.converter.ConverterChain.getAsString(ConverterChain.java:123)
                 at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.getFormattedValue(HtmlBasicRenderer.java:469)
                 at com.sun.faces.renderkit.html_basic.MenuRenderer.renderOption(MenuRenderer.java:502)
                


                Here's my piece of the components.xml with the real names I'm using:

                <persistence:managed-persistence-context name="numinsEntityManager"
                 auto-create="true"
                 persistence-unit-jndi-name="java:/numinsPersistenceUnit" />
                
                <component name="org.jboss.seam.ui.entityConverter">
                 <property name="entityManager">#{numinsEntityManager}</property>
                </component>
                


                • 5. Re: Can EntityConverter handle different EntityManagers?
                  Jose Rubio Newbie

                  I kept diging and I think I found why it always tries to use entityManager.

                  this is from MutableEntityController:
                  
                   @Override
                   protected String getPersistenceContextName()
                   {
                   return "entityManager";
                   }
                  


                  • 6. Re: Can EntityConverter handle different EntityManagers?
                    Jose Rubio Newbie

                    I know "entityManager" is the default value, but I never see the entityManager attribute in EntityConverter being injected. So it always goes up to the default and creates the entityManager.

                    So for some reason my component entry to set the entityManager is not being used.

                    • 7. Re: Can EntityConverter handle different EntityManagers?
                      Matt Drees Master

                      I have no idea if this will work or not, but try this:

                      <s:convertEntity entityManager="#{expressions.createValueExpression('#{anotherEntityManager}')}" />
                      

                      (I think that parses...)

                      • 8. Re: Can EntityConverter handle different EntityManagers?
                        Jose Rubio Newbie

                        This one did the trick!! Thanks!!!

                        Still curious as to why the component tag in the components.xml wasn't working right. It was kind of being ignored.


                        One thing I still need to figure out is how to use the s:convertEntity with two different entityManagers in the same page. I think somewhere it only saves one. I will need to do some debugging on it to figure it out (and I'm fallign asleep!!!)

                        Thanks for the replies. Once I figure it out I'll make a blog entry or something so we can link it from the wiki.

                        • 9. Re: Can EntityConverter handle different EntityManagers?
                          Pete Muir Master

                          Try

                          components.xml

                          <component name="myEntityConverter" class="org.jboss.seam.ui.converter.EntityConverter">
                           <property name="entityManager">#{myEntityManager}>/property>
                          </component>


                          facelet
                          <h:selectOneMenu ...>
                           <f:converter converterId="myEntityConverter" />
                          


                          Of course you could wrap the converter up in a custom tag if you wanted to.