Add Relationship Between Entities
Updated: Jan 31, 2023
This example will show you how you can add relationships between entities similar to database.
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.EntityRelationshipDef; import com.northconcepts.datapipeline.foundations.schema.ForeignKeyAction; import com.northconcepts.datapipeline.foundations.schema.NumericFieldDef; import com.northconcepts.datapipeline.foundations.schema.PrimaryKeyType; import com.northconcepts.datapipeline.foundations.schema.RelationshipCardinality; import com.northconcepts.datapipeline.foundations.schema.SchemaDef; import com.northconcepts.datapipeline.foundations.schema.TextFieldDef; public class AddRelationshipBetweenEntities { public static void main(String[] args) { SchemaDef schema = new SchemaDef("E-Commerce Schema"); schema.addEntity(new EntityDef("orders") .addField(new NumericFieldDef("order_id", FieldType.LONG).setPrimaryKeyPosition(0).setPrimaryKeyType(PrimaryKeyType.TABLE)) .addField(new NumericFieldDef("customer_id", FieldType.LONG)) .addField(new TextFieldDef("product_name", FieldType.STRING)) .addField(new NumericFieldDef("quantity", FieldType.LONG)) ); schema.addEntity(new EntityDef("customers") .addField(new NumericFieldDef("customer_id", FieldType.LONG).setPrimaryKeyPosition(0).setPrimaryKeyType(PrimaryKeyType.AUTO)) .addField(new TextFieldDef("customer_name", FieldType.STRING)) .addField(new TextFieldDef("address", FieldType.STRING)) ); EntityRelationshipDef entityRelationship = new EntityRelationshipDef("fk_customers_orders") .setPrimaryEntityName("customers") .setForeignEntityName("orders") .setCardinality(RelationshipCardinality.ONE_TO_MANY) .setForeignKeyFieldNames("customer_id") .setOnUpdateAction(ForeignKeyAction.SET_DEFAULT) .setOnDeleteAction(ForeignKeyAction.SET_NULL); schema.addEntityRelationship(entityRelationship); System.out.println("========================================================="); System.out.println(schema.toXml()); System.out.println("========================================================="); } }
Code walkthrough
- Create a SchemaDef named E-Commerce Schema with two entities.
- An entity, customers has one primary key - customer_id.
- Another entity, orders has a field - customer_id which is foreign key and referred from customers entity.
- Create EntityRelationshipDef to define the relationship between two entities.
- Here customers is primary entity and orders is foreign entity.
- You can also define properties similar to SQL database e.g. RelationshipCardinality, ForeignKeyAction
- Print this schema as XML.
Console Output
========================================================= <schema name="E-Commerce Schema"> <entities> <entity addMissingOptionalFields="false" allowExtraFieldsInMapping="true" allowExtraFieldsInValidation="true" name="orders"> <fields> <field array="false" name="order_id" primaryKeyPosition="0" primaryKeyType="TABLE" required="false" strictArrays="true" type="LONG"/> <field array="false" name="customer_id" required="false" strictArrays="true" type="LONG"/> <field allowBlank="true" array="false" name="product_name" required="false" strictArrays="true" type="STRING"/> <field array="false" name="quantity" required="false" strictArrays="true" type="LONG"/> </fields> </entity> <entity addMissingOptionalFields="false" allowExtraFieldsInMapping="true" allowExtraFieldsInValidation="true" name="customers"> <fields> <field array="false" name="customer_id" primaryKeyPosition="0" primaryKeyType="AUTO" required="false" strictArrays="true" type="LONG"/> <field allowBlank="true" array="false" name="customer_name" required="false" strictArrays="true" type="STRING"/> <field allowBlank="true" array="false" name="address" required="false" strictArrays="true" type="STRING"/> </fields> </entity> </entities> <entity-relationships> <entity-relationship cardinality="ONE_TO_MANY" foreignEntityName="orders" name="fk_customers_orders" onDeleteAction="SET_NULL" onUpdateAction="SET_DEFAULT" primaryEntityName="customers"> <foreign-key name="customer_id"/> </entity-relationship> </entity-relationships> </schema> =========================================================