3 Replies Latest reply on Jun 27, 2007 1:18 PM by tuxzilla

    Problem with injecting EntityManager when invoking action wi

    tuxzilla

      I have a list view page that pops up within page a send-to-friend form (using display block/none toggle). The send to friend form is submitted via <a:commandButton action="#{sendToFriend.send}"/>. The problem is that when the form is submitted, I always get the exception saying that the entityManager in the sendToFriend bean is null. I configured the entityManager the standard way, with auto-create and Conversation context.

       <core:managed-persistence-context name="entityManager"
       auto-create="true"
       persistence-unit-jndi-name="java:/myEntityManagerFactory"/>
      


      The sendToFriendBean is of EVENT scope:
      @Name("sendToFriend")
      @Scope(ScopeType.EVENT)
      @AutoCreate
      public class SendToFriend {
      ...
       @In
       private EntityManager entityManager;
      ...
       public void send() {
       ...
       }
      }
      

      I did some debugging, and found out that when BijectionInterceptor was trying to inject entityManager, there was no conversationContext alive (which was the scope of the entityManager). So no entityManager was created. How do I solve this problem? A related question is that, I have many other beans with EVENT scope and entityManager injection. How can they work, with conversation scope alive without me starting it?

      Thanks.


        • 1. Re: Problem with injecting EntityManager when invoking actio
          tuxzilla

          Ok, it turned out the problem has nothing to do with AJAX submission. The form has a hidden field which I bound it to sendToFriend.entityIdField property. If I remove this property, everything works fine. But I do need this property to pass the id of the entity to sendToFriend bean. Here is the view code related to the hidden field:

          <h:form>
          ...
          <h:inputHidden value="#{listing.id}" binding="#{sendToFriend.entityIdField}"/>
          </h:form>
          

          and the entityIdField property in sendToFriend bean:
           private UIInput entityIdField;
          

          Isn't that weird? Binding a UIInput field interferes with injection of entityManager. Any help?

          • 2. Re: Problem with injecting EntityManager when invoking actio
            delphi'sghost

            As a possible suggection, did you put getters and setters on the entityIdField and in any interface that the bean implements?



            • 3. Re: Problem with injecting EntityManager when invoking actio
              tuxzilla

              Yes I do have getters and setters for the property. It is a POJO. I finally found out the solution. It is simply adding "(required=false)" in the entityManager annotation. This is anti-intuitive because you'd expect entityManager to always be available. But it seems that when the UIInput property is set to sendToFriend bean (through binding not value of input), it is unlike the usual setters where binding is done via value="". When the UIInput is set, entityManager is still null. But when action is invoked, entityManager is no longer null. So the solution is to mark entityManager injection not required to avoid any problem during setting UIInput. It will be automatically created when action is invoked.