Excluding fields in JAXB

frislasialf

New Member
I have 2 classes:\[code\]@XmlRootElementpublic class A { private Long id; private B b; // setters and getters}\[/code\]and\[code\]@XmlRootElementpublic class B { private Long id; private String field1; private String field2; // setters and getters}\[/code\]By default, if I transform an instance of class \[code\]A\[/code\] to the XML, I will have all its fields (\[code\]id\[/code\]) and the referenced \[code\]B\[/code\] class fields (\[code\]id\[/code\], \[code\]field1\[/code\], \[code\]field2\[/code\]) like this:\[code\]<a> <id>2</id> <b> <id>5</id> <field1>test1</field1> <field2>test3</field2> </b></a>\[/code\]Is is possible to modify what fields from referenced class \[code\]B\[/code\] are included in the XML of the \[code\]A\[/code\] class? E.g. I want to say that when I transform an instance of \[code\]A\[/code\] class, I just want to get \[code\]id\[/code\] from the \[code\]B\[/code\] class (no \[code\]field1\[/code\] and \[code\]field2\[/code\] fields), so I want to get:\[code\]<a> <id>2</id> <b> <id>5</id> </b></a>\[/code\]I don't want to permanently annotate the \[code\]B\[/code\] class (using \[code\]@XMLTransient\[/code\] or \[code\]@XMLElement\[/code\]) to achieve it, as there are cases in which I want to export whole \[code\]B\[/code\] class as is (with \[code\]id\[/code\], \[code\]field1\[/code\] and \[code\]field2\[/code\].)
I just don't want to export all these fields when the \[code\]B\[/code\] class is referenced from \[code\]A\[/code\].Is this even possible with JAX-B?
 
Back
Top