3 Replies Latest reply on Oct 20, 2006 12:37 PM by gmournos

    Help:type of nodes returned by root.getNode() is unnarrowed

      I have created a custom node type and I need to cast to it during the workflow execution.
      However, when I cast the result of calling token.getNode() to my custom type, I get a ClassCastException, although the token is at my custom node at the time.

      My code is complicated so I created a simple test case, and added it to the already existing GraphSessionDbTest.java (basically I just added three assertions to the existing test case).
      The test case breaks, in the third assertion. The node has name 's' but it is not an instance of StartState...

      // added to jbpm-3.1/src/java.jbpm.test/org/jbpm/db/GraphSessionDbTest.java
      
       public void testUpdateProcessInstance2() {
       ProcessDefinition processDefinition = ProcessDefinition.parseXmlString(
       "<process-definition>" +
       " <start-state name='s' />" +
       " <node name='n' />" +
       "</process-definition>");
      
       processDefinition = saveAndReload(processDefinition);
      
       ProcessInstance processInstance = new ProcessInstance(processDefinition);
      
       processInstance = saveAndReload(processInstance);
       long pid = processInstance.getId();
      
       //this is the actual part added to the already existing testUpdateProcessInstance() case
       assertNotNull(processInstance.getRootToken().getNode());
       assertTrue(processInstance.getRootToken().getNode() instanceof Node);
       assertTrue(processInstance.getRootToken().getNode() instanceof StartState);
      
       assertEquals("s", processInstance.getRootToken().getNode().getName());
       processInstance.getRootToken().setNode(processInstance.getProcessDefinition().getNode("n"));
      
       processInstance = saveAndReload(processInstance);
       assertEquals("n", processInstance.getRootToken().getNode().getName());
       assertEquals(pid, processInstance.getId());
       }
      
      


      Is this normal?
      I am not a hibernate expert.
      In my normal code (not when I am running the simple test case), during the execution of signal() I get hibernate warnings of the type:

      14:01:34,429 [main] WARN StatefulPersistenceContext : Narrowing proxy to class org.jbpm.graph.node.StartState - this operation breaks ==
      


      Are these relevant?




        • 1. Re: Help:type of nodes returned by root.getNode() is unnarro

          Looking at the hibernate code, I can see that the last warning is probably irrelevant.
          It is thrown by hibernate during a successful narrowing to warn that the new object instance breaks equality...



          • 2. Re: Help:type of nodes returned by root.getNode() is unnarro

            I needed to be able to look at the correct type nodes so I finally added an <any instead of a <many-to-one in the hibernate mappings for Token.

            This solved my problem...In case someone has the same problem.

            • 3. Re: Help:type of nodes returned by root.getNode() is unnarro

              Actually I had the same problem when overriding other jbpm objects, so I finally wrote a method like this to get to the target object from the proxies...

              public static Object narrowProxy(Object maybeProxy) {
               if (maybeProxy == null)
               throw new IllegalArgumentException("cannot narrow null");
               if ( maybeProxy instanceof HibernateProxy ) {
               HibernateProxy proxy = (HibernateProxy) maybeProxy;
               LazyInitializer li = proxy.getHibernateLazyInitializer();
               return li.getImplementation();
               //get the real object where the proxy is working on.
               //if the proxy is not initialized then it will be initialized here
               } else
               return maybeProxy;
               }
              


              This code has more to do with hibernate, but if someone extends the Jbpm objects then it might be useful