0

I have created Jaxb DTOs using Maven profile. Something like this:

class Parent { ChildOne one; }
class ChildOne { ChildTwo two; }
class ChildTwo { ChildThree three; }
class ChildThree { ChildFour four; }

The hierarchy roughly goes on till ChildEight.

class ChildEight { String valueToBeStored; }

I have to set the value of the string of the last ChildEight. The way it is currently being done is by creating each ChildObject instance and setting it to the parent reference.

Parent parent = new Parent();
ChildOne one = new ChildOne();
ChildTwo two = new ChildTwo();
ChildThree three = new ChildThree();
...
ChildEight eight = new ChildEight();

eight.setValueToBeStored("Hurray");
seven.setEight(eight);
...
one.setChildTwo(two);
parent.setChildOne(one);

My question is, instead of creating all the dependent objects (which are not going to be used again), is there a better or more efficient way to achieve the same result?

Notes:

  1. The instances are not created so I cannot use BeanUtils or PropertyUtils to copy objects, but I am expecting to find a similar way to make my life easier.
  2. There are no constructors with arguments.
  3. Child Five contains a List of ChildSix Objects and has a getter which returns the reference to a live list. So we can use getChildSixList().add() instead of using a setter.
phoenixSid
  • 447
  • 1
  • 8
  • 22
  • *"which are not going to be used again"* - well, effectively they *are* used as you need the whole hierarchy up to `ChildEight`. – lexicore Jul 15 '17 at 12:49
  • @lexicore Yes you are right. What I actually meant was that, I will only navigate the entire hierarchy from one to seven to reach object eight. So there is no scenario where I have 2 child objects of classes like two or four. – phoenixSid Jul 17 '17 at 09:29

0 Answers0