Parent-child relationship mapping করা গাইড ও নোট

Java Technologies - জ্যাকসন অ্যানোটেশন (Jackson Annotations) - @JsonManagedReference এবং @JsonBackReference
318

Jackson ব্যবহার করে Parent-Child Relationship Mapping এর জন্য কিছু বিশেষ অ্যানোটেশন রয়েছে, যা আপনাকে Circular Reference সমস্যা সমাধান এবং Parent-Child Object গুলোকে সঠিকভাবে JSON আউটপুটে রূপান্তর করতে সাহায্য করে।

এখানে @JsonManagedReference এবং @JsonBackReference অ্যানোটেশন প্রধানভাবে ব্যবহার করা হয় Circular References হ্যান্ডেল করার জন্য। এছাড়াও, @JsonTypeInfo এবং @JsonSubTypes অ্যানোটেশন দিয়ে আপনি Polymorphic parent-child relationship mapping করতে পারেন।


1. Circular Reference Handling with @JsonManagedReference and @JsonBackReference

Circular Reference সমস্যা তখন ঘটে যখন দুটি অবজেক্ট একে অপরকে রেফার করে এবং এর ফলে অবিরাম রেফারেন্স তৈরি হয়, যার ফলে StackOverflowError হতে পারে। Jackson এ এই সমস্যা সমাধানে @JsonManagedReference এবং @JsonBackReference অ্যানোটেশন ব্যবহার করা হয়।

  • @JsonManagedReference: Parent Object-এর ফিল্ডের জন্য ব্যবহৃত হয় যা Child Object কে রেফার করে।
  • @JsonBackReference: Child Object-এর ফিল্ডের জন্য ব্যবহৃত হয় যা Parent Object কে রেফার করে এবং এই ফিল্ডটি serialization-এর সময় ignore করা হয়।

উদাহরণ:

import com.fasterxml.jackson.annotation.JsonManagedReference;
import com.fasterxml.jackson.annotation.JsonBackReference;

public class Parent {
    private String name;

    @JsonManagedReference
    private Child child;

    // Constructors, Getters and Setters
}

public class Child {
    private String name;

    @JsonBackReference
    private Parent parent;

    // Constructors, Getters and Setters
}

Serialization Example:

import com.fasterxml.jackson.databind.ObjectMapper;

public class ParentChildSerialization {
    public static void main(String[] args) throws Exception {
        Parent parent = new Parent();
        parent.setName("Rahim");

        Child child = new Child();
        child.setName("Rita");
        
        parent.setChild(child);
        child.setParent(parent);

        ObjectMapper objectMapper = new ObjectMapper();
        String json = objectMapper.writeValueAsString(parent);

        System.out.println(json);
    }
}

JSON Output:

{
  "name": "Rahim",
  "child": {
    "name": "Rita"
  }
}

ব্যাখ্যা:

  • @JsonManagedReference এবং @JsonBackReference অ্যানোটেশনগুলির মাধ্যমে circular reference এড়ানো হয়েছে। এখানে child অবজেক্টকে flatten করা হয়েছে, কিন্তু parent অবজেক্টে রেফারেন্স করা parent ফিল্ডটি @JsonBackReference দ্বারা ignored হয়েছে।

2. Polymorphic Serialization with @JsonTypeInfo and @JsonSubTypes

Polymorphic parent-child relationship mapping এ Jackson এর @JsonTypeInfo এবং @JsonSubTypes অ্যানোটেশন সাহায্য করে, যা আপনাকে Inheritance Hierarchies (Parent এবং তার Subclass) সহজে serialize এবং deserialize করতে সাহায্য করে।

  • @JsonTypeInfo: এটি parent class এ ব্যবহার করা হয় এবং কোন subclass এর instance serialize/deserialze করা হবে তা নির্ধারণ করে।
  • @JsonSubTypes: এটি parent class এ ব্যবহার করা হয় এবং কোন subclass গুলো serialize/deserialze হবে তা নির্ধারণ করে।

উদাহরণ:

import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.JsonSubTypes;

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
@JsonSubTypes({
    @JsonSubTypes.Type(value = Car.class, name = "car"),
    @JsonSubTypes.Type(value = Truck.class, name = "truck")
})
abstract class Vehicle {
    private String brand;

    // Constructors, Getters and Setters
}

class Car extends Vehicle {
    private int seatingCapacity;

    // Constructors, Getters and Setters
}

class Truck extends Vehicle {
    private int loadCapacity;

    // Constructors, Getters and Setters
}

Serialization Example:

import com.fasterxml.jackson.databind.ObjectMapper;

public class VehicleSerialization {
    public static void main(String[] args) throws Exception {
        Vehicle car = new Car();
        ((Car) car).setSeatingCapacity(5);
        ((Car) car).setBrand("Toyota");

        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(car);
        System.out.println(json);
    }
}

JSON Output:

{
  "type": "car",
  "brand": "Toyota",
  "seatingCapacity": 5
}

ব্যাখ্যা:

  • @JsonTypeInfo এবং @JsonSubTypes ব্যবহার করে Jackson এখানে Polymorphic objects serialize করতে সক্ষম হয়েছে।
  • JSON এর type ফিল্ডের মাধ্যমে Jackson নির্ধারণ করতে পারে যে কোন subclass (Car বা Truck) serialize হবে।

3. Flattening Nested Objects with @JsonUnwrapped

@JsonUnwrapped অ্যানোটেশন ব্যবহার করে আপনি nested objects-এর ফিল্ডগুলোকে parent object এর অংশ হিসেবে flatten করতে পারেন, যা nested object কে JSON properties হিসেবে উপস্থাপন করে।

উদাহরণ:

import com.fasterxml.jackson.annotation.JsonUnwrapped;

public class Parent {
    private String name;

    @JsonUnwrapped
    private Address address;

    // Constructors, Getters and Setters
}

class Address {
    private String street;
    private String city;
    private String zipCode;

    // Constructors, Getters and Setters
}

Serialization Example:

import com.fasterxml.jackson.databind.ObjectMapper;

public class FlattenNestedObjects {
    public static void main(String[] args) throws Exception {
        Parent parent = new Parent();
        parent.setName("Rahim");

        Address address = new Address();
        address.setStreet("123 Main St");
        address.setCity("Dhaka");
        address.setZipCode("1212");

        parent.setAddress(address);

        ObjectMapper objectMapper = new ObjectMapper();
        String json = objectMapper.writeValueAsString(parent);

        System.out.println(json);
    }
}

JSON Output:

{
  "name": "Rahim",
  "street": "123 Main St",
  "city": "Dhaka",
  "zipCode": "1212"
}

ব্যাখ্যা:

  • @JsonUnwrapped ব্যবহার করে Address object-এর ফিল্ডগুলোকে flatten করা হয়েছে, অর্থাৎ Parent object-এর মধ্যে Address object-এর ফিল্ডগুলোকে直接 JSON properties হিসেবে রাখা হয়েছে।

4. Using @JsonProperty for Custom Field Naming

@JsonProperty অ্যানোটেশন ব্যবহার করে Parent-Child relationship এর মধ্যে JSON ফিল্ডের নাম কাস্টমাইজ করা যায়। এর মাধ্যমে আপনি প্রতিটি ফিল্ডের নাম কীভাবে JSON-এ প্রদর্শিত হবে তা নিয়ন্ত্রণ করতে পারেন।

উদাহরণ:

import com.fasterxml.jackson.annotation.JsonProperty;

public class Parent {
    private String name;

    @JsonProperty("address_info")
    private Address address;

    // Constructors, Getters and Setters
}

class Address {
    private String street;
    private String city;
    private String zipCode;

    // Constructors, Getters and Setters
}

JSON Output:

{
  "name": "Rahim",
  "address_info": {
    "street": "123 Main St",
    "city": "Dhaka",
    "zipCode": "1212"
  }
}

  1. Circular Reference সমস্যার সমাধান করতে @JsonManagedReference এবং @JsonBackReference ব্যবহার করুন।
  2. Polymorphic Serialization-এর জন্য @JsonTypeInfo এবং @JsonSubTypes ব্যবহার করুন।
  3. Flatten Nested Objects করতে @JsonUnwrapped ব্যবহার করুন।
  4. Custom Field Naming এর জন্য @JsonProperty ব্যবহার করুন।
  5. Inheritance Mapping-এর জন্য Jackson annotations-এর মাধ্যমে Parent-Child relationship এর মধ্যে সঠিক serialization এবং deserialization সম্ভব।
Content added By
Promotion

Are you sure to start over?

Loading...