5 Replies Latest reply on Feb 1, 2007 10:41 AM by Chris Russell

    A sample with HibernateLongInstance  or HibernateStringInsta

    pinston Newbie

      I am a beginner with JBPM and tried to persist custom variable with Hibernate.
      I saw in documentation things about HibernateLongInstance and HibernateStringInstance but I can't get a small example to work.
      Documentation "Storing hibernate persistent objects" is with the TODO status so ...

      If anybody succeded in this task, I would appreciate any help.

      Cheers

      Philippe

        • 1. Re: A sample with HibernateLongInstance  or HibernateStringI
          Jan Cheyns Newbie

          An example of this feature would indeed by appreciated, also by me...

          • 2. Re: A sample with HibernateLongInstance  or HibernateStringI
            Jan Cheyns Newbie

             

            "jcheyns" wrote:
            An example of this feature would indeed by appreciated, also by me...


            Oh...

            for the time being I just save the object through
            session.save(myObject);

            I then retrieve the Id of this object, say
            myObject.getId();

            and than i pass this on in the context. On the receiving side I than use a hibernate query to find myObject. I suppose this is very analogue to the way intended by JBPM, however because of lack of complete documentation this remains a mystery to me (and apperently others too).

            • 3. Re: A sample with HibernateLongInstance  or HibernateStringI
              Michiel Trimpe Newbie

              OK, I've just spent a couple of hours working on this. This is the process as it goes:

              In the ContextInstance you've created a variable that's supposed to be Hibernate managed.
              The variable is passed through a number of variable matchers, until it reaches HibernateLongIdMatcher.

              This searches the hibernate configuration file for a class of the given type, and if it finds one it gets the ID field and stores that into the database.

              I stopped there, because I use the starterskit and I needed to edit the hibernate.cfg.xml in the jbpm-server\server\jbpm\deploy\jbpm.sar\jbpm.sar.cfg.jar but then I needed to add my classfile to the jBPM classloader and I felt that that would break loose coupling and end up just being an ugly hack.

              • 4. Re: A sample with HibernateLongInstance  or HibernateStringI
                doballve Newbie

                Just to give it code example, here is how you would do for a HibernateLongInstance:

                First, You have a hibernate entity with id of type Long and a hibernate mapping to your class properly loaded in the same session as JBPM mappings. The mapping:

                <class name="mypackage.MyEntity" table="SAMPLE_ENTITY">
                 <id name="id" column="id" unsaved-value="0" type="java.lang.Long">
                 <generator class="native"/>
                 </id>
                 ...
                 </class>


                Then you use a HibernateLongInstance to wrap your entity and store it as any other JBPM variable:

                ...
                // create my entity an wrap it with HibernateLongInstance
                MyEntity ent = new MyEntity();
                HibernateLongInstance entWrapper = new HibernateLongInstance();
                envWrapper.setObject(ent);
                
                // store it to root token context
                ContextInstance contextInstance = (ContextInstance)
                 token.getProcessInstance().getInstance(ContextInstance.class);
                contextInstance.setVariable("my-entity-variable", entWrapper);
                ...


                To access it later, just get the wrapped object and class cast it back to your entity:

                ...
                ContextInstance contextInstance = (ContextInstance)
                 token.getProcessInstance().getInstance(ContextInstance.class);
                HibernateLongInstance entWrapper = (HibernateLongInstance)
                 contextInstance.getVariable("my-entity-variable");
                MyEntity entData = (MyEntity)entWrapper.getObject();
                ...


                That's it!

                A warning though: The default configuration has Serializable type mapped before Hibernate and EJB types. That means that if your entity class implements java.io.Serializable it will get serialized as a byte array and not as a Hibernate entity. To avoid this behavior, re-order the types in jbpm.varmapping.xml.



                • 5. Re: A sample with HibernateLongInstance  or HibernateStringI
                  Chris Russell Newbie

                  See also http://www.jboss.com/index.html?module=bb&op=viewtopic&t=100452
                  I am trying to do the same thing with an entity that does not have a resource mapping file but uses annotations only to define schema.
                  Problem there is there is no way to "tell" jbpm about the annotated class because of the HibernateHelper use of Configuration object rather than AnnotationConfiguration.
                  Need a way to add config in hibernate.cfg.xml that sees the annotated class:
                  e.g.

                  <mapping class="foo.MyAnnotatedFooClass"/>