@ParentProperty Annotation
The @ParentProperty annotation is used to identify a setter method or field for adding a parent reference to a child object.
This annotation is used by parsers to automatically establish parent-child relationships when parsing nested objects. When a child object is created within a parent object (e.g., in a list or as a property), the parser automatically sets the parent reference on the child.
Requirements
- Must be an instance method or field (not static)
- For methods: Must accept exactly one parameter (the parent object type)
- For fields: Can be any type (typically the parent object type)
- The method or field does not need to be public (will be made accessible automatically)
Usage
Can be applied to:
- Bean setter methods or fields
@Rest-annotated classes and@RestOp-annotated methods when using theon()parameter
Example
Using a Field
public class AddressBook {
public List<Person> people;
}
public class Person {
@ParentProperty
public AddressBook addressBook; // Automatically set to containing AddressBook
public String name;
public char sex;
}
Using a Setter Method
public class AddressBook {
public List<Person> people;
}
public class Person {
private AddressBook parent;
@ParentProperty
protected void setParent(AddressBook parent) {
this.parent = parent;
}
public String name;
public char sex;
}
When It's Called
- During parsing when a child object is created within a parent object
- The parser automatically calls the setter or sets the field with a reference to the parent object
- This allows child objects to navigate back to their parent if needed
- Useful for bidirectional relationships where child objects need access to their parent context
Share feedback or follow-up questions for this page directly through GitHub.