1

When adding JAX-B Java annotations for Java classes - if I have a parent Class Entry, with two children, Book and JournalArticle,

Would I add these annotations for all three classes:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement

ie:

@XmlSeeAlso({au.com.library.Book.class, au.com.library.JournalArticle.class})
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement
public abstract class Entry implements Serializable{


private static final long serialVersionUID = -1895155325179947581L;

@XmlElement(name="title")
protected String title;
@XmlElement(name="author")
protected String author;
@XmlElement(name="year")
protected int year;

and

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement
public class Book extends Entry {

@XmlElement(name="edition")
private String edition;
@XmlElement(name="publisher")
private String publisher;
@XmlElement(name="placeOfPublication")
private String placeOfPub;

and

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement
public class JournalArticle extends Entry {

@XmlElement(name="journalTitle")
private String journalTitle;
@XmlElement(name="volume")
private String volume;
@XmlElement(name="issue")
private String issue;
@XmlElement(name="pageNumbers")
private String pgNumbers;
Mat
  • 202,337
  • 40
  • 393
  • 406
AlexM
  • 133
  • 2
  • 3
  • 17
  • You should add the annotations only if those classes will be marshaled/unmarshaled by the JAX-B process. Remember that is not the same sending/retrieving a parent class or a child class by the WS. They will be treated as different classes. – Luiggi Mendoza Oct 19 '12 at 04:48
  • @Luiggi Mendoza - I want them to both be treated as type "Entry". I am making a list of these Entry objects, and when I add to the list, I first check if the object exists, and only add if the object doesnt already exist in the list (using the List().contains() method) - but it keeps re-adding the same items - and when I debug, when it checks if the object already exists (when I add the SAME Book() object which is already in the list with the same attributes) it gives the new Book a different ID number to the existing Book object(that has the exact same attributes). – AlexM Oct 19 '12 at 04:55
  • Note that the classes marshaled/unmarshaled by JAX-B must not have any business logic method (i.e. `equals` method). When the list is unmarshaled (in the consumer or the provider side), you will get a totally new element (different from the one you sent). For your case, I would use a custom `Comparator` in the method instead of the `List#contains`. – Luiggi Mendoza Oct 19 '12 at 05:25

1 Answers1

1

The annotation XmlAccessorType can be inherited so I believe it is not mandatory to declare it again on child classes.

@Inherited
@Retention(value=RUNTIME)
@Target(value={PACKAGE,TYPE})
public @interface XmlAccessorType

This is not the case for XmlRootElement, so you will have to annotate each base class with it.

You can find more information on the @Inherited annotation on the javadoc.

Update for your comment:

@Retention(value=RUNTIME) means that the class keeps this annotation even at runtime, that is, a program can use the Java reflection API to check if the annotation is present on a class.

@Target(value={PACKAGE,TYPE}) means that this annotation can be used to annotate classes, interfaces or enums (this is for the value=TYPE), and also at a whole package level (this is for value=PACKAGE). You can see this thread explaining how this can be useful.

More information on the Javadoc:

Community
  • 1
  • 1
Alex
  • 25,147
  • 6
  • 59
  • 55
  • Thanks for the response - so in this case, what would the @Retention(value=?), and the @Target(value=?) equal? – AlexM Oct 19 '12 at 05:02