Skip to content

Nested Mapping

Alessandro Vurro edited this page Jun 8, 2016 · 13 revisions

It's possible define the nested mapping following a syntax equal to JSP Expression Language and placeholders currently used to define static/dynamic conversions: ${field.nestedField}.
This allows to distinguish it from the normal mapping and regex usage.

example

public class Flatten {

	@JMap("${nested2.nested3.nested4}")
	private String field;

        //get and set..
}

public class Nested {

	private Nested2 nested2;

        // get and set..

}

public class Nested2 {

	private Nested3 nested3;

        // get and set..

}

public class Nested3 {

	private String nested4;

        // get and set..

}

You can use nested mapping for destination or source in equal mode, the only thing that changes is the generated code.

In case of destination nested, the code generated is like this:

...
Nested2 nested2 = destination.getNested2();
if(nested2 == null){
   nested2 = new Nested2();
}
Nested3 nested3 = nested2.getNested3();
if(nested3 == null){
   nested3 = new Nested3();
}
...

In case of source nested:

...
Nested2 nested2 = null;
try{
   nested2 = source.getNested2();
}catch(NullPointerException e){
   Error.nestedBeanNull();
}
Nested3 nested3 = null;
try{
   nested3 = nested2.getNested3();
}catch(NullPointerException e){
   Error.nestedBeanNull();
}
...

The exception thrown contains a detailed message that permits to find fast the field that is null.

Clone this wiki locally