About Blaise Doughan

Team lead for the TopLink/EclipseLink JAXB & SDO implementations, and the Oracle representative on those specifications.

JAXB and Unmapped Properties

JAXB (JSR-222) is configuration by exception, meaning that there is a default mapping applied to domain objects. This means that sometimes you need to explicitly exclude a field/property. In this post I’ll discuss how this can be done using @XmlTransient or @XmlAccessorType(XmlAccessType.NONE) and when each option is appropriate.

@XmlTransient

When you mark a field/property with @XmlTransient you are explicitly telling your JAXB implementation to treat it as an unmapped property. This is useful when less than half of the fields/properties are unmapped.

package blog.unmapped;
 
import java.util.List;
import javax.xml.bind.annotation.*;
 
@XmlAccessorType(XmlAccessType.FIELD)
public class Customer {
 
    @XmlTransient
    private int id;
 
    private String firstName;
 
    private String lastName;
 
    private Address billingAddress;
 
    private Address shippingAddress;
 
    private List<PhoneNumber> phoneNumbers;
 
}

If more than half of the fields/properties are unmapped then we stop gaining the benefit of configuration by exception since we need to do more work to exclude properties with @XmlTransient than we would have had to do to map the mapped properties. Next I’ll demonstrate how we can leverage @XmlAccessorType(XmlAccessType.NONE) for this use case.

package blog.unmapped;
 
import java.util.List;
import javax.xml.bind.annotation.*;
 
@XmlAccessorType(XmlAccessType.FIELD)
public class Customer {
 
    @XmlTransient
    private int id;
 
    private String firstName;
 
    private String lastName;
 
    @XmlTransient
    private Address billingAddress;
 
    @XmlTransient
    private Address shippingAddress;
 
    @XmlTransient
    private List<PhoneNumber> phoneNumbers;
 
}

@XmlAccessorType(XmlAccessType.NONE)

By setting @XmlAccessorType(XmlAccessType.NONE) we are disabling configuration by exception. Now only explicitly mapped properties will be mapped. This is useful when less than half of the fields/properties of a domain object are mapped.

package blog.unmapped;
 
import java.util.List;
import javax.xml.bind.annotation.*;
 
@XmlAccessorType(XmlAccessType.FIELD)
public class Customer {
 
    @XmlTransient
    private int id;
 
    private String firstName;
 
    private String lastName;
 
    @XmlTransient
    private Address billingAddress;
 
    @XmlTransient
    private Address shippingAddress;
 
    @XmlTransient
    private List<PhoneNumber> phoneNumbers;
 
}

Further Reading

If you enjoyed this post then you also like:

Reference: JAXB and Unmapped Properties from our JCG partner Blaise Doughan at the Java XML & JSON Binding blog.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.

2 Responses to "JAXB and Unmapped Properties"

  1. pridisc says:

    Your last example with @XmlAccessorType(XmlAccessType.NONE) doesn’t do what it claims. You still have marked the fields @XMLTransient. Besides how does one explicitly mark a property?

  2. Mike says:

    Looks like the code snippet for @XmlAccessorType(XmlAccessType.NONE) is wrong, may be copy paste error?

Leave a Reply


6 − = four



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close