简体   繁体   中英

JAXB String to JPA Date in Java

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM