Search code examples
javaspring-datagemfirespring-data-gemfire

How to create composite primary key scenario in Gemfire region?


I am working on migration of mainframe to java. I have one table in DB2. I am replicating this table as a region in Gemfire. While doing this, I have composite primary key for that table. To replicate table to gemfire region. I want to make composite primary key for region. I just want to know that, is there any possibility to create composite primary key in region?


Solution

  • Ideally, you are creating keys that do not change in value after the Key/Value pair is "put" into a Region. A GemFire/Geode Region is a just a glorified version of a java.util.Map (and a HashMap to be precise).

    This means that the "composite" key should not be based on any property values in the Region value itself. By way of example, imagine I have a value Customer defined like so...

    class Customer {
    
      Long accountNumber;
    
      Gender gender;
    
      LocalDate birthDate;
    
      String firstName;
      String lastName;
    
      ...
    }
    

    It would be inadvisable to then create a key like so...

    class CustomerKey {
    
      LocalDate birthDate;
    
      String firstName;
      String lastName;
    
    }
    

    While a person's birth date, first and last name are generally enough to uniquely identify someone, if any of these individual property/field values change then you lose your pairing given the intrinsic behavior of the Java Map (so be careful).

    You should also take care to carefully craft the equals/hashCode methods of any "custom/composite" key class you create.

    I would generally recommend that you use internal (only known to the application), scalar types for your keys, e.g. Long or String.

    This would be as simple as adding a property/field to the Customer class like so...

    class Customer {
    
      Long id;
    
      ...
    
    }
    

    Most of the time, your (OQL) query predicates are based on the values of the individual domain object properties/fields, not the keys. For example...

    SELECT c FROM /Customers c WHERE c.lastName = 'Doe'
    

    Additionally, if your key were a String, you could use the UUID class to generate unique values. Otherwise, you will need to craft your own unique, key generation strategy.

    In summary, while what you are asking is possible given carefully crafted class types in Java, it is not generally recommended; prefer simple, scalar types.

    Here is a link to some things in general to keep in mind.

    Hope this helps.

    -John