Add Primary Keys to Entities

This example will show you how you can add primary keys to entities. You can define one or more than one primary key (composite key) to an entity.

Java code listing

package com.northconcepts.datapipeline.foundations.examples.schema;

import com.northconcepts.datapipeline.core.FieldType;
import com.northconcepts.datapipeline.foundations.schema.EntityDef;
import com.northconcepts.datapipeline.foundations.schema.NumericFieldDef;
import com.northconcepts.datapipeline.foundations.schema.SchemaDef;
import com.northconcepts.datapipeline.foundations.schema.TextFieldDef;

public class AddPrimaryKeysToEntities {

    public static void main(String[] args) {
        SchemaDef schema = new SchemaDef("E-Commerce Schema");
        
        schema.addEntity(new EntityDef("suppliers")
                // Primary Key
                .addField(new NumericFieldDef("supplier_id", FieldType.LONG).setPrimaryKeyPosition(0))
                .addField(new TextFieldDef("supplier_name", FieldType.STRING))
                .addField(new TextFieldDef("city", FieldType.STRING))
                .addField(new TextFieldDef("country", FieldType.STRING))
                .addField(new TextFieldDef("contact_details", FieldType.STRING))
                );

        schema.addEntity(new EntityDef("products")
                // Composite key of product_id & supplier_id
                .addField(new NumericFieldDef("product_id", FieldType.LONG).setPrimaryKeyPosition(0))
                .addField(new NumericFieldDef("supplier_id", FieldType.LONG).setPrimaryKeyPosition(1))
                .addField(new TextFieldDef("product_name", FieldType.STRING))
                .addField(new TextFieldDef("color", FieldType.STRING))
                );

        System.out.println("=========================================================");
        System.out.println(schema.toXml());
        System.out.println("=========================================================");
    }
}

Code walkthrough

  1. Create a SchemaDef named E-Commerce Schema with two entities.
  2. First entity, suppliers has only one primary key - supplier_id.
  3. Second entity, products has two primary keys - product_id and supplier_id with position 0 and 1 respectively.
  4. Print this schema as XML.

Console Output

=========================================================
<schema name="E-Commerce Schema">
  <entities>
    <entity addMissingOptionalFields="false" allowExtraFieldsInMapping="true" allowExtraFieldsInValidation="true" name="suppliers">
      <fields>
        <field array="false" name="supplier_id" primaryKeyPosition="0" required="false" strictArrays="true" type="LONG"/>
        <field allowBlank="true" array="false" name="supplier_name" required="false" strictArrays="true" type="STRING"/>
        <field allowBlank="true" array="false" name="city" required="false" strictArrays="true" type="STRING"/>
        <field allowBlank="true" array="false" name="country" required="false" strictArrays="true" type="STRING"/>
        <field allowBlank="true" array="false" name="contact_details" required="false" strictArrays="true" type="STRING"/>
      </fields>
    </entity>
    <entity addMissingOptionalFields="false" allowExtraFieldsInMapping="true" allowExtraFieldsInValidation="true" name="products">
      <fields>
        <field array="false" name="product_id" primaryKeyPosition="0" required="false" strictArrays="true" type="LONG"/>
        <field array="false" name="supplier_id" primaryKeyPosition="1" required="false" strictArrays="true" type="LONG"/>
        <field allowBlank="true" array="false" name="product_name" required="false" strictArrays="true" type="STRING"/>
        <field allowBlank="true" array="false" name="color" required="false" strictArrays="true" type="STRING"/>
      </fields>
    </entity>
  </entities>
</schema>
=========================================================
Mobile Analytics