DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

SBOMs are essential to circumventing software supply chain attacks, and they provide visibility into various software components.

Related

  • Comprehensive Guide to Property-Based Testing in Go: Principles and Implementation
  • Implement Hibernate Second-Level Cache With NCache
  • Modify JSON Data in Postgres and Hibernate 6
  • Top 10 C# Keywords and Features

Trending

  • Contract-Driven ML: The Missing Link to Trustworthy Machine Learning
  • Streamline Your ELT Workflow in Snowflake With Dynamic Tables and Medallion Design
  • Stop Prompt Hacking: How I Connected My AI Agent to Any API With MCP
  • Optimizing Cloud Costs With Serverless Architectures: A Technical Perspective

JAXB's @XmlTransient and Property Order

By 
Blaise Doughan user avatar
Blaise Doughan
·
Aug. 25, 12 · Interview
Likes (0)
Comment
Save
Tweet
Share
50.4K Views

Join the DZone community and get the full member experience.

Join For Free
In previous articles I wrote about how the @XmlTransient annotation can be used at the type level to have a class excluded from the inheritance hierarchy, or at the field/property level to unmap a field/property.  In this article I'll demonstrate how doing this impacts the propOrder setting on the @XmlType annotation.

Java Model

Base
This class will serve as the root of the inheritance hierarchy.  This will be a mapped class, and as such there is nothing special we need to do, to make this happen.
package blog.proporder.xmltransient;
 
public abstract class Base {
 
    private int id;
 
    public int getId() {
        return id;
    }
 
    public void setId(int id) {
        this.id = id;
    }
 
}
Person

To exclude a class from being mapped as part of the inheritance hierarchy you simply need to annotate it with @XmlTransient.   Any super classes of this class that are not annotated with @XmlTransient (i.e. Base) will still be mapped.
package blog.proporder.xmltransient;
 
import javax.xml.bind.annotation.XmlTransient;
 
@XmlTransient
public class Person extends Base {
 
    private String name;
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
}

Customer

Since the parent class (Person) has been marked @XmlTransient the name property will be treated as part of the Customer class and can be included in the propOrder.  The Customer class also extends Base which was not marked @XmlTransient so the id property can not be specified in the propOrder.  The propOrder setting must not include a field/property that has been annotated with @XmlTransient.

package blog.proporder.xmltransient;
 
import java.util.List;
import javax.xml.bind.annotation.*;
 
@XmlRootElement
@XmlType(propOrder = { "phoneNumbers", "name"})
public class Customer extends Person {
 
    private String password;
    private List<String> phoneNumbers;
 
    @XmlTransient
    public String getPassword() {
        return password;
    }
 
    public void setPassword(String password) {
        this.password = password;
    }
 
    @XmlElement(name = "phone-number")
    public List<String> getPhoneNumbers() {
        return phoneNumbers;
    }
 
    public void setPhoneNumbers(List<String> phoneNumbers) {
        this.phoneNumbers = phoneNumbers;
    }
 
     
}
Demo Code


Below is the demo code that can be used to run this example:

package blog.proporder.xmltransient;
 
import java.io.File;
import javax.xml.bind.*;
 
public class Demo {
 
    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Customer.class);
 
        Unmarshaller unmarshaller = jc.createUnmarshaller();
        File xml = new File("input.xml");
        Customer customer = (Customer) unmarshaller.unmarshal(xml);
 
        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(customer, System.out);
    }
 
}
XML (input.xml/Output)


Below is the input to, and output from running the demo code.  Note how the properties of classes marked with @XmlTransient are included, but properties marked @XmlTransient are not.

<?xml version="1.0" encoding="UTF-8"?>
<customer>
   <id>123</id>
   <phone-number>555-1111</phone-number>
   <phone-number>555-2222</phone-number>
   <name>Jane Doe</name>
</customer>

 

 

 

Property (programming)

Published at DZone with permission of Blaise Doughan, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Comprehensive Guide to Property-Based Testing in Go: Principles and Implementation
  • Implement Hibernate Second-Level Cache With NCache
  • Modify JSON Data in Postgres and Hibernate 6
  • Top 10 C# Keywords and Features

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • [email protected]

Let's be friends: