convert values from java..sql.Struct to Pojo with dozer

310 Views Asked by At

We have access the following (I'm writing pseudo) structs on database side:

struct_pet(
  name varchar2(20),
  age number
)

struct_person(
  first_name varchar2(20),
  last_name varchar2(20),
  dob date,
  pets table of struct_pet
)

We have the following java classes (with getters, setters)

Pet {
  String name;
  Integer age;
}

Person {
  String firstName;
  String lastName;
  Date dob;
  List<Pet> pets;
}

We have access to a stored procedure that returns with the database type of struct_person.

Currently our java classes implements SQLData interface and implements proper readSQL (stream.readInt(), stream.readString, etc) methods so the Person resultPerson = (Person)[result of stored procedure call].getObject() automatically casted/converted from the java.sql.Struct to our Person (and Pet) class via the readSQL method.

Our problem is:

  • if we expect that struct_person will have a fifth field and that field isn't present in the database, stream.readXXX throws in IndexOutOfBoundsExcetion
  • database structs (and stored procedures) are maintained by another vendor and often happens that their development/deployment cycle is behind us so we are blocked with deploying our new developments (almost real situation: customer requested five new feature in the next release, two of these features requires new fields in the structs and we are ready with the development but another vendor only finished the development of one feature)

I'm thinking on the following workaround (accepting the business problem of absence the new struct fields) - forget readSQL, use getStruct() on the result of the prepared statement call - create a List attrList in Pet and Person (see below) - use DozerBeanMapper to map java.sql.Struct to Pet or Person and map the values of java.sql.Struct / attributes to [Pet|Person] / attrList with a specially implemented customConverter [org.dozer.CustomConverter] interface (and maybe reflection based class cast)

In this case, my Java classes will be extended with the following properties (of course, with getters and setters) and constructor:

Pet {
  .
  .
  List<Object> attrList;

  Pet() {
    attrList.add(name);
    attrList.add(age);
  }
}

Person {
  .
  .
  List<Object> attrList;

  Person() {
    attrList.add(firstName);
    attrList.add(lastName);
    attrList.add(dob);
    attrList.add(pers);
  }

}

My questions are:

  • Did anybody try a similar approach? Did it work?
  • Any other suggestions?

Thx in advance

0

There are 0 best solutions below