Class JacksonUniqueIdGenerator

java.lang.Object
com.fasterxml.jackson.annotation.ObjectIdGenerator<String>
org.optaplanner.examples.common.persistence.jackson.JacksonUniqueIdGenerator
All Implemented Interfaces:
Serializable

public final class JacksonUniqueIdGenerator extends com.fasterxml.jackson.annotation.ObjectIdGenerator<String>
Exists so that complex data models (such as TSP chaining) can be serialized/deserialized. These complexities include:
  • Serializing maps where keys are themselves serialized objects that need to be referenced later.
  • Serializing polymorphic types.
  • Serializing self-referential and/or recursive types.
Jackson can easily handle any of these problems individually, but struggles when they are all combined.

This class and other classes in this package aim to solve those issues by introducing a new ID field on all serialized objects, typically called "@id". This field is used exclusively for referencing objects in the serialized JSON, it never enters the Java data model. Therefore it is not related to AbstractPersistable.getId(), which is the actual object ID used in the Java examples. See Vehicle Routing example to learn how to use this pattern.

For use cases without these advanced needs, the less complex way of using JsonIdentityInfo with ObjectIdGenerators.PropertyGenerator is preferred. See Cloud Balancing example to learn how to use this pattern.

The implementation is similar in principle to ObjectIdGenerators.UUIDGenerator, but without the long and undescriptive UUIDs. Works only for children of AbstractPersistable. No two such classes must have the same Class.getSimpleName().

See Also:
  • Nested Class Summary

    Nested classes/interfaces inherited from class com.fasterxml.jackson.annotation.ObjectIdGenerator

    com.fasterxml.jackson.annotation.ObjectIdGenerator.IdKey
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    canUseFor(com.fasterxml.jackson.annotation.ObjectIdGenerator<?> gen)
     
    com.fasterxml.jackson.annotation.ObjectIdGenerator<String>
    forScope(Class<?> scope)
     
    generateId(Object forPojo)
     
     
    com.fasterxml.jackson.annotation.ObjectIdGenerator.IdKey
    key(Object key)
     
    com.fasterxml.jackson.annotation.ObjectIdGenerator<String>
     

    Methods inherited from class com.fasterxml.jackson.annotation.ObjectIdGenerator

    isValidReferencePropertyName, maySerializeAsObject

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • JacksonUniqueIdGenerator

      public JacksonUniqueIdGenerator()
  • Method Details

    • getScope

      public Class<?> getScope()
      Specified by:
      getScope in class com.fasterxml.jackson.annotation.ObjectIdGenerator<String>
    • canUseFor

      public boolean canUseFor(com.fasterxml.jackson.annotation.ObjectIdGenerator<?> gen)
      Specified by:
      canUseFor in class com.fasterxml.jackson.annotation.ObjectIdGenerator<String>
    • forScope

      public com.fasterxml.jackson.annotation.ObjectIdGenerator<String> forScope(Class<?> scope)
      Specified by:
      forScope in class com.fasterxml.jackson.annotation.ObjectIdGenerator<String>
    • newForSerialization

      public com.fasterxml.jackson.annotation.ObjectIdGenerator<String> newForSerialization(Object context)
      Specified by:
      newForSerialization in class com.fasterxml.jackson.annotation.ObjectIdGenerator<String>
    • key

      public com.fasterxml.jackson.annotation.ObjectIdGenerator.IdKey key(Object key)
      Specified by:
      key in class com.fasterxml.jackson.annotation.ObjectIdGenerator<String>
    • generateId

      public String generateId(Object forPojo)
      Specified by:
      generateId in class com.fasterxml.jackson.annotation.ObjectIdGenerator<String>