1

My XML stores Date value as String(Client requirement) and I want that string to be stored as Date in my database as it is required for few sorting and grouping.

I created a Class which contains both JAXB and JPA annotations in a single class.

I want my Class to read/write XML with String value, but when it stored in database JPA entity should convert it as date and store it in DB.

Present code I'm reading it as String as I couldn't find neat solution:

    @XmlElement(name = "TransactionDate", required = true)
    protected String transactionDate;

    @Basic
    @Column(name = "TRANSACTIONDATE", length = 255)
    public String getTransactionDate() {
        return transactionDate;
    }

    public void setTransactionDate(String value) {
        this.transactionDate = value;

Please help me in achieving it.

MUNNA S
  • 11
  • 3
  • 2
    This question may help (keeping the object's field as a `Date`, but XML serializing/deserializing as a `String`) : https://stackoverflow.com/questions/13568543/how-do-you-specify-the-date-format-used-when-jaxb-marshals-xsddatetime – Arnaud Sep 18 '17 at 12:40
  • You can create second read-only date field. First will stay mapped with JAXB, second will mapped with DB. – Sergey Morozov Sep 18 '17 at 12:42
  • 2
    you say you are receiving a date in xml as a String, what is the date format? if it is XSD:date or XSD datetime, you should easily be abe to unmarshall it as an XMLGregorianCalendar object which will allow easy conversion to java.util.Date – MartinByers Sep 18 '17 at 13:00
  • @Berger- Null is getting inserted in database when I used it. – MUNNA S Sep 19 '17 at 06:44
  • @SergeyMorozov -- Will it support both marshaling and un-marshaling. Actions that I do are - 1) Read XML and store it in DB using JPA 2) load data from database and marshal XML though JAXB. Both JPA and JAXb are written in same class. – MUNNA S Sep 19 '17 at 06:45
  • I was able to get it using Date Adapter. Only change that I have to do is maintain two different date formats for.marshal and unmarshal. Thanks @Berger – MUNNA S Sep 19 '17 at 09:30

1 Answers1

0

How do you specify the date format used when JAXB marshals xsd:dateTime?.

Using XmlAdapter we can achieve it as mentioned above. Only changes that I have done are maintaining two different dateformats to read Jaxb and write JPA.

MUNNA S
  • 11
  • 3