使用eclipselink在JPA对象上jaxb marshall期间的日期对话错误 [英] Date conversation error during jaxb marshall on a JPA object using eclipselink

查看:110
本文介绍了使用eclipselink在JPA对象上jaxb marshall期间的日期对话错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我坚持这个Date会话错误很长一段时间...
我在TomcatEE环境下使用eclipselinks,openJPA,并尝试使用jaxb进行编组。我遇到了一个问题,一个JPA对象,包含Date,TimeStamp元素。
异常消息是---

I stuck on this Date conversation error for quite some time ... I am using eclipselinks, openJPA under TomcatEE environment, and trying to use jaxb doing marshalling. I met a problem for marshall one JPA object, which contains Date, TimeStamp elements. The exception message is ---

javax.xml.bind.MarshalException
- 链接异常:
[异常[EclipseLink] -3002](Eclipse Persistence Services - 2.3.2.v20111125-r10461):org.eclipse.persistence.exceptions.ConversionException
异常描述:对象[3/19/12 12:00 AM],类[ class org.apache.openjpa.util.java $ util $ Date $ proxy],来自映射[org.eclipse.persistence.oxm.mappings.XMLDirectMapping [createTs - > createTs / text()]]和描述符[XMLDescriptor(xxx) .xxxx.xxx.xxxx.entities.ApplicationEntity - > [])],无法转换为[类java.util.Date]。]

javax.xml.bind.MarshalException - with linked exception: [Exception [EclipseLink-3002] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.ConversionException Exception Description: The object [3/19/12 12:00 AM], of class [class org.apache.openjpa.util.java$util$Date$proxy], from mapping [org.eclipse.persistence.oxm.mappings.XMLDirectMapping[createTs-->createTs/text()]] with descriptor [XMLDescriptor(xxx.xxxx.xxx.xxxx.entities.ApplicationEntity --> [])], could not be converted to [class java.util.Date].]

奇怪事情是jaxb转换工作对一些客户,但不是其他客户。我试图为这个字段crtTs放置@XmlElement(type = Date.class),它不起作用。

The strange thing is jaxb converting works OK for some customers, but not some other customer. I tried to put @XmlElement(type=Date.class) for this field crtTs, It doesn't work.

感谢您的帮助。

LL

推荐答案

我能够重现你的问题正在看到。您可以使用以下错误来跟踪我们在此问题上的进展:

I have been able to reproduce the issue you are seeing. You can use the following bug to track our progress on this issue:

  • http://bugs.eclipse.org/383639

周围工作

DateAdapter

您可以使用 XmlAdapter 将有问题的日期转换为正确的 java.util.Date

You could use an XmlAdapter to convert the problematic date into a proper java.util.Date.

package forum11145711;

import java.util.Date;
import javax.xml.bind.annotation.adapters.XmlAdapter;

public class DateAdapter extends XmlAdapter<Date, Date>{

    @Override
    public Date unmarshal(Date date) throws Exception {
        return date;
    }

    @Override
    public Date marshal(Date date) throws Exception {
        if(null == date) {
            return date;
        }
        return new Date(date.getTime());
    }

}

Root

@XmlJavaTypeAdapter 注释用于利用 XmlAdapter

package forum11145711;

import java.util.Date;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

@XmlRootElement
public class Root {

    private Date date;

    @XmlJavaTypeAdapter(DateAdapter.class)
    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

}

MyDate

下面是我在这个例子中使用的 java.util.Date 的子类。

Below is the subclass of java.util.Date I'm using in this example.

package forum11145711;

import java.util.Date;

public class MyDate extends Date {

}

演示

下面是一些演示代码,可用于证明一切正常:

Below is some demo code you can use to prove that everything works:

package forum11145711;

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(Root.class);

        Root root = new Root();
        root.setDate(new MyDate());

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(root, System.out);
    }

}

输出

<?xml version="1.0" encoding="UTF-8"?>
<root>
   <date>2012-06-27T10:39:49.081</date>
</root>

这篇关于使用eclipselink在JPA对象上jaxb marshall期间的日期对话错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆