Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1082345
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:17:04+00:00 2026-05-16T22:17:04+00:00

Is there a JAXB annotation to ignore a parent class, when you have an

  • 0

Is there a JAXB annotation to ignore a parent class, when you have an @XmlElement on a List of the child classes?

Just to clarify – I was wondering if there was a better way than marking all of the parent classes getters/setters as transient, and then having to go back to the child classes and add getters/setters and annotating those as XmlElements as well

An Example:

public class GenericHelper {
    String name="";
    String dates="";
    String roleName="";
    String loe="";
    @XmlTransient
    public String getName() {return name;}
public void setName(String name) {this.name = name;}
@XmlTransient
public String getDates() {return dates;}
public void setDates(String dates) {this.dates = dates;}
@XmlTransient
public String getRoleName() {return roleName;}
public void setRoleName(String roleName) {this.roleName = roleName;}
@XmlTransient
public String getLOE() {return loe;}
public void setLOE(String loe) {
    this.loe = loe.replace("%", "").trim();
}
}

and

public class SpecificHelper extends GenericHelper {
List<ProjectHelper> projects;
public SpecificHelper (){
    projects=new ArrayList<ProjectHelper>();
}
@XmlElement(name = "project")
@XmlElementWrapper (name = "projectlist")
public List<ProjectHelper> getProjects() {return projects;}
public void setProjects(List<ProjectHelper> projects) {this.projects = projects;}
@XmlElement
public String getName(){
    return super.getName();
}

@Override
public String toString(){
    String ret="SpecificHelper [";
    ret+="name:"+name+";";
    ret+="dates:"+dates+";";
    ret+="roleName:"+roleName+";";
    ret+="loe:"+loe+";";
    ret+="\n\tprojects:"+projects+";";
    return ret+"]";
}
}

So in this example, if I take out the XmlTransient annotations in GenericHelper, any class that extends it, if I were to have a method getSpecificHelper() that returned a list of all employers, and annotate it with XmlElement, ALL of those items will return with a name, LOE, RoleName, etc. I am looking for a class annotation to go on GenericHelper so I can avoid having to use all of the @XmlTransients individually, and only use the XmlElement notations I have put in the SpecificHelper

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T22:17:05+00:00Added an answer on May 16, 2026 at 10:17 pm

    How about?

    The Parent Class

    We will use XmlAccessType.NONE to tell JAXB that only explicitly annotated fields/properties are mapped.

    import javax.xml.bind.annotation.XmlAccessType;
    import javax.xml.bind.annotation.XmlAccessorType;
    
    @XmlAccessorType(XmlAccessType.NONE)
    public class Parent {
    
        private String parentProperty1;
        private String parentProperty2;
    
        public String getParentProperty1() {
            return parentProperty1;
        }
    
        public void setParentProperty1(String parentProperty1) {
            this.parentProperty1 = parentProperty1;
        }
    
        public String getParentProperty2() {
            return parentProperty2;
        }
    
        public void setParentProperty2(String parentProperty2) {
            this.parentProperty2 = parentProperty2;
        }
    
    }
    

    The Child Class

    We will use XmlAccessType.PROPERTY on the child. Any properties from the parent class we want to include will need to be overridden and be explicitly annotated. In this example we will bring in parentProperty2 from the Parent class. You will only need to override the getter from the parent class.

    import javax.xml.bind.annotation.XmlAccessType;
    import javax.xml.bind.annotation.XmlAccessorType;
    import javax.xml.bind.annotation.XmlElement;
    import javax.xml.bind.annotation.XmlRootElement;
    
    @XmlRootElement
    @XmlAccessorType(XmlAccessType.PROPERTY)
    public class Child extends Parent {
    
        private String childProperty;
    
        @Override
        @XmlElement
        public String getParentProperty2() {
            return super.getParentProperty2();
        }
    
        public String getChildProperty() {
            return childProperty;
        }
    
        public void setChildProperty(String childProperty) {
            this.childProperty = childProperty;
        }
    
    }
    

    Demo Class

    import javax.xml.bind.JAXBContext;
    import javax.xml.bind.Marshaller;
    
    public class Demo {
    
        public static void main(String[] args) throws Exception {
            JAXBContext jc = JAXBContext.newInstance(Child.class);
    
            Child child = new Child();
            child.setParentProperty1("parentProperty1");
            child.setParentProperty2("parentProperty2");
            child.setChildProperty("childProperty");
    
            Marshaller marshaller = jc.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.marshal(child, System.out);
        }
    }
    

    Output

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <child>
        <childProperty>childProperty</childProperty>
        <parentProperty2>parentProperty2</parentProperty2>
    </child>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say, I have two classes: @XmlRootElement class A { @XmlElement String propertyOfA; @XmlElement B
I wonder if there is any support for different versions of class in JAXB.
According to the documentation JAXB factory methods do not have arguments. Is there a
I'm using JAXB for quick-and-dirty XML generation. I have a class that represents a
Let's say I have the following two classes: package example.model; public class Model {
I have defined my bindings in the following way <jaxb:bindings node=xs:complexType[@name='Parent']> ........... <jaxb:bindings node=xs:element[@name='children']>
I have a package which just includes and xsd file to generate (via JAXB)
There's some way to generate a custom method within an class generated with JAXB.
Reflecting on JAXB annotated objects, is there a way to determine if a class/field/method
I have a problem, I have some JAXB generated java files between which there

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.