I'm trying to do some simple XML serialization with JavaBeans, with an object having five getter/setter properties, and two getters. These two are from type List<...> :
public List<MasterDataQueryMDType> getType() {
if (type == null) {
type = new ArrayList<MasterDataQueryMDType>();
}
return this.type;
}
and
public List<MasterDataQueryKey> getKey() {
if (key == null) {
key = new ArrayList<MasterDataQueryKey>();
}
return this.key;
}
I'm then using the XMLEncoder class (although here JAXB may be more appropriate, but I'm keeping it simple for now). The resulting XML has only the five getter/setter properties, the two properties of type List haven't been encoded. Is it because they are read only, or do I have to write a PersistenceDelegate for those kind of generic Lists ?
Okay I've researched more, and the easiest way to solve this without going mad with writing its own PersistenceDelegate seems to be the creation of a wrapper class :
public class MasterDataQueryWrapper {
private MasterDataQuery query;
public MasterDataQuery getQuery(){
return this.query;
}
public void setQuery(MasterDataQuery value){
this.query = value;
}
public List<MasterDataQueryMDType> getType(){
return query.getType();
}
public void setType(List<MasterDataQueryMDType> value){
for (MasterDataQueryMDType t:value){
this.query.getType().add(t);
}
}
public List<MasterDataQueryKey> getKey(){
return query.getKey();
}
public void setKey(List<MasterDataQueryKey> value){
for (MasterDataQueryKey k:value){
this.query.getKey().add(k);
}
}
}
That way, java Beans has no problems getting and setting the read-only properties. However if you have a more elegant solution, feel free to chime in...
After testing and reflexion, it seems my solution is the easiest in the context of JavaBeans, simply create the wrapper class...