[Java programming language only]

Defining ClassAlias and FieldAlias annotations to correlate Java classes

[Version 8.6 and later]To enable sharing of objects in the data grid between different Java™ classes, use ClassAlias and FieldAlias annotations. When two classes are correlated, the fields and field types are matched between the classes, even if the class names are different.

Before you begin

  • You must have IBM® eXtremeIO configured. For more information, see Configuring IBM eXtremeIO (XIO).
  • Your copyMode attribute in your ObjectGrid descriptor XML file must be set to COPY_TO_BYTES. For more information, see Configuring data grids to use eXtreme data format (XDF).
  • Use ClassAlias and FieldAlias annotations when you are running two different classes within different application scopes, or run times. The data that is stored in the data grid can be shared and reused across two different application run times. As a result, you do not need to maintain two different metadata descriptors. If your classes are within the same application scope, or run time, it can be confusing from the application provider or development point of view to have two classes be correlated.

About this task

For more information about the ClassAlias and FieldAlias annotations, see ClassAlias and FieldAlias annotations.

Procedure

  1. [Java programming language only]Use ClassAlias and FieldAlias annotations to correlate objects between two different Java classes.
    In the following example classes, the @ClassAlias("ACME_Customer") Java annotation is specified. Some fields have a @FieldAlias("") annotation. Because both of these classes have the same ClassAlias annotation and FieldAlias definitions, the objects are maintained with the same class type ID by XDF. The same XDF metadata is used when these objects are serialized or deserialized during the get and put operations.
    Figure 1. Customer1 class with @ClassAlias and @FieldAlias annotations
        @ClassAlias("ACME_Customer")
        class Customer1 {
            @FieldAlias("Employee ID")
            int empId = -1;
            
            @FieldAlias("Department No.")
            int deptId = -1;
            
            @FieldAlias("Year Salary")
            float salary = 0;
            
            String sex = "M";
                    
            int age = -1;
                   
            String homeAddress = "";      
            
            public Customer1(int empId, int deptId, float salary, String sex, int age, String homeAddress) {
                this.empId = empId;
                this.deptId = deptId;
                this.salary = salary;
                this.sex = sex;
                this.age = age;
                this.homeAddress = homeAddress;
            }       
        }     
    Figure 2. Customer2 class with @ClassAlias and @FieldAlias annotations
     @ClassAlias("ACME_Customer")
        class Customer2 {
            @FieldAlias("Employee ID")
            int empId = -1;
            
            @FieldAlias("Department No.")
            int deptId = -1;
            
            @FieldAlias("Year Salary")
            float salary = 0;
            
    
            String sex = "M";
                    
            int age = -1;
                   
            String homeAddress = "";      
            
            public Customer2(int empId, int deptId, float salary, String sex, int age, String homeAddress) {
                this.empId = empId;
                this.deptId = deptId;
                this.salary = salary;
                this.sex = sex;
                this.age = age;
                this.homeAddress = homeAddress;
            }
            
        }    
  2. Optional: Specify the class alias discovery path, so that the class alias can be used to correlate with an equivalent class in the client class path.
    Set the discovery path if the deserialization process does not find the equivalent class from the client. Set the discovery path if you have another class in your client that defines the same class alias, but is not loaded in your current class loader.
    • [Java programming language only]Enable a Java application to scan and load classes that match with the specified ClassAlias value from the application class path.

      When you start the application, specify the -Dwxs.classalias.discovery.path Java virtual machine (JVM) argument. The list of Java archive (JAR) files or specific folders that contain the Java classes to match with a class alias that is defined in the user-defined classes are scanned.

      For example, you might specify: -Dwxs.classalias.discovery.path=c:\myApp\lib\customer1.jar;c:\myApp\lib\customer2.jar;c:\myApp\classes The scan operation scans all the specified JAR files and class path folders to find all the available Java classes. The Java class that is matched first in the client application environment is based on the class alias is loaded during the get operation.