Camunda JPA Entity Serialization

Hello Camunda team.

I have a question about process variables. (Process Variables | docs.camunda.org)

Here we multiple supported variable values, such primitive types as complex java pojo objects.
I want to set Entity classes into variables but I don’t want to use java object serialization (byte array streams into database).

I remember somewhere in youtube videos was talk about JPA entity serialization (I cant find this video now) which means Camunda can save only ID value of an Entity Class not a full java object. And in process of deserialization Camunda will retrieve this Entity class from database using saved Entity ID.

Is there any example of documentation how to implement this kind of serialization ?

P.S.

We made debug of camunda serialization example. Camunda Version (7.9.0).
We tried to store our entity Class object into variables. For Example : EmergencyCase.

We know that this Class : “DefaultVariableSerializers” is used to store variables.
During the debug we step into the method : “findSerializerForValue”.

this method finds 2 type of serialization object with this criteria
“if(serializer.canHandle(value))”

1.JPAVariableSerializer
2.JavaObjectSerializer

Serializators are stored the same order as I described above into matchedSerializers list :

List<TypedValueSerializer<?>> matchedSerializers = new ArrayList<TypedValueSerializer<?>>();

Default Serializator data from in the configuration class “ProcessEngineConfigurationImpl” is
JAVA(“application/x-java-serialized-object”)

so the this part of code ignores JPAVariableSerializer.

// ambiguous match, use default serializer
  if(defaultSerializationFormat != null) {
    for (TypedValueSerializer<?> typedValueSerializer : matchedSerializers) {
      if(defaultSerializationFormat.equals(typedValueSerializer.getSerializationDataformat())) {
        return typedValueSerializer;
      }
    }
  }

So we override default serialization data format from the configuration (Application.yml) :

  camunda:
    bpm:
       history-level: full
       default-serialization-format: application/json

This kind of configuration cause : Camunda now uses JPAVariableSerializer, as we wanted to.
Now we see data in the table (act_ru_variable) like this :

type_ = ‘jpa’ instead of ‘serializable’
bytearray_id_ is null instead of UUID of related table.
text_ = fully classified class name of an entity instead of null
text2_ = entity ID value instead of fully classified class name of an entity.

Is this the correct way to use JPAVariableSerializer ?

Regards,
Paata.

1 Like

Hi Paata,

It is not possible to tell the engine explicitly to treat a value as a JPA variable. So in the session you debugged, both the JPA serializer and the Java object serializer were considered for serialization. If you don’t implement java.io.Serializable in your entity class, then the second one will not be considered and the JPA serializer should be used by default. The workaround you found (changing the default serialization format) does work and is probably fine, but rather hacky.

Cheers,
Thorben

1 Like

Thank you so much for your reply.
Everything is clear. I will remove java.io.Serializable implementation from my entity beans.

P.S
Is it normal design to save entity beans into Camunda variables and use JPA Serialization ?

Regards,
Paata.

Hi Paata,

I think most people either do not use JPA or rather store the reference themselves as a String or long variable. But as long as everything works in your use case, using the JPA variable type is perfectly fine.

Cheers,
Thorben