0

I'm looking at some legacy code that is an Entity that is mapped with JPA. This object also gets converted to XML via JaxB. I'm new to JPA and JaxB. Below is a code sniplet:

@Entity
@Audited
@Table(name = "TTRANSACTION_ITEM")
@DiscriminatorColumn()
public class TransactionItem {
    @Transient

    private Order order;
    private List<Comment> comments;
    private List<Attachment> attachments;

    @XmlTransient
    private Timestamp lastPrintDate;
    private String createdBy;

    @XmlTransient
    private Timestamp creationDate;

    @XmlTransient
    private Timestamp lastUpdateDate;
    private String lastUpdateBy;
    private int isSendDiploma;

    @Transient
    private Student aStudent;

// NOTE, I'm forced to make THESE TWO PUBLIC or else i won't show in XML:

@Transient
public  String studentFirstName;

@Transient
public  String studentLastName;

...
...
}

Question:

1) Does the name of the elements in the generated xml file come from the fields of the database table? For example, in the generated xml file, I see sendDiploma instead of isSendDiploma.

2) Why does instance variable need to be private? I changed it to public or protected and the element will not appear in the XML. Oddly, if I want to include extra variables such as (studentFirstName) in the java code to xml, I need to use a public . For example public String studentFirstName. Because I don't want studentFirstName to be in the database, I use @Transient. I added studentFirstName because for whatever reason, I don't see any elements containing "aStudent" and it's values such as firstName and lastName.

3) How come the element order in the XML is unpredictable? Where does the order come from? I don't see any JaxB annotation that is used to dictate the ordering. Unfortunately, I can't include the xml output here because it's sensitive

Derick
  • 135
  • 1
  • 3
  • 12
  • For question 3: http://stackoverflow.com/questions/5435138/jaxb-and-property-ordering – guido Mar 14 '15 at 03:14
  • 1
    For question 2: http://stackoverflow.com/questions/10013282/jaxb-is-the-annotation-xmlaccessortype-is-only-for-serialization-and-nothing – guido Mar 14 '15 at 03:15
  • Thanks, I got it to work via your link. Turns out, my coworker didn't use @XmlElement annotation. Once that's used, I can use a private modifer for studentFirstName. I'll have to study up on jaxb later on. So how do I mark this as solved? – Derick Mar 16 '15 at 21:52
  • no need to; because your issue was solved by another answer on another post on this site, other people will just mark it as duplicate – guido Mar 16 '15 at 22:05

0 Answers0