Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Difficulties Unmarshalling Timestamp
Difficulties Unmarshalling Timestamp [message #551289] Thu, 05 August 2010 15:52 Go to next message
Shelli Orton is currently offline Shelli Orton
Messages: 76
Registered: September 2009
Member
I am writing a JAX-RS (Jersey) webservice that is an interface to JPA (EclipseLink) entities for CRUD operations. I am using JAXB (EclipseLink) to convert the entities to/from XML in the requests/responses. I created this class to marshall/unmarshall the Timestamp fields in my entities:

public class TimestampXmlAdapter extends XmlAdapter<String, Timestamp>
{
    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");

    public static String format(Timestamp timestamp)
    {
        String formatted = "";

        if (timestamp != null)
        {
            formatted = dateFormat.format(timestamp);
        }

        return formatted;
    }

    @Override
    public String marshal(Timestamp timestamp) throws Exception
    {
        return TimestampXmlAdapter.format(timestamp);
    }

    @Override
    public Timestamp unmarshal(String timestamp) throws Exception
    {
        return new Timestamp(dateFormat.parse(timestamp).getTime());
    }

}


This works fine for the marshalling of the entities, but I am getting errors on the unmarshallig if the Timestamp element is empty. For example:
<MyObject>
    <id>111-222</id>
    <activeDate></activeDate>
</MyObject>

generates this error:
Local Exception Stack: 
Exception [EclipseLink-3001] (Eclipse Persistence Services - 2.0.1.v20100213-r6600): org.eclipse.persistence.exceptions.ConversionException
Exception Description: The object [], of class [class java.lang.String], could not be converted to [class java.sql.Timestamp].
	at org.eclipse.persistence.exceptions.ConversionException.couldNotBeConverted(ConversionException.java:71)

So, I updated the unmarshall method to:

    public Timestamp unmarshal(String timestamp) throws Exception
    {
        if (timestamp == null || timestamp.equals(""))
        {
            return null;
        }
        return new Timestamp(dateFormat.parse(timestamp).getTime());
    }

and that generates this error:
Local Exception Stack: 
Exception [EclipseLink-33] (Eclipse Persistence Services - 2.0.1.v20100213-r6600): org.eclipse.persistence.exceptions.DescriptorException
Exception Description: Trying to invoke [setActiveDate] on the object with the value [].  The number of actual and formal parameters differs, or an unwrapping conversion has failed.
Internal Exception: java.lang.IllegalArgumentException: argument type mismatch
Mapping: org.eclipse.persistence.oxm.mappings.XMLDirectMapping[activeDate-->activeDate/text()]
Descriptor: XMLDescriptor(my.package.model.MyObject --> [DatabaseTable(MyObject)])
	at org.eclipse.persistence.exceptions.DescriptorException.illegalArgumentWhileSettingValueThruMethodAccessor(DescriptorException.java:691)


Can anyone help me fix this?
Re: Difficulties Unmarshalling Timestamp [message #551490 is a reply to message #551289] Fri, 06 August 2010 11:45 Go to previous messageGo to next message
Matt MacIvor is currently offline Matt MacIvor
Messages: 14
Registered: July 2009
Junior Member
Hi,

This is related to a bug in EclipseLink that has been fixed in the latest versions. I've run the test on the latest EclipseLink 2.1 and 2.2 nightly builds and it passes.

You can get the latest 2.1.1 or 2.2.0 nightly builds here:
http://www.eclipse.org/eclipselink/downloads/nightly.php

If you're unable to upgrade to the latest you can work around this by using an EclipseLink customizer to correct the mapping that EclipseLink is generating. Here is how you would do that.

Add an XmlCustomizer annotation to your class that uses the Timestamp Adapter:

@XmlCustomizer(MyObjectCustomizer.class)
public class MyObject {
  private Timestamp timestamp;

  @XmlJavaTypeAdapter(TimestampXmlAdapter.class)
  public Timestamp getTimestamp() {
    ...
   }

  public Timestamp setTimestamp() {
    ...
  } 
}


Then create your customizer class. It should look something like this:

public class MyObjectCustomizer implements DescriptorCustomizer {
  public void customize(ClassDescriptor descriptor) throws Exception {
    XMLDirectMapping timestampMapping = (XMLDirectMapping)
            descriptor.getMappingForAttributeName("timestamp");
    timestampMapping.setNullValue(null);
  }
}


The issue is that EclipseLink is incorrectly setting a NullValue of "" on this mapping. By setting it to null in the customizer, the issue should be resolved.

-Matt
Re: Difficulties Unmarshalling Timestamp [message #551494 is a reply to message #551490] Fri, 06 August 2010 12:09 Go to previous message
Shelli Orton is currently offline Shelli Orton
Messages: 76
Registered: September 2009
Member
Thanks!

Updating fixed it. I was using version 2.1.0 and probably should have mentioned that in my original post.

Shelli
Previous Topic:Java Architecture for XML Binding (JAXB) - Class not found
Next Topic:Sharing using information with an entity listener
Goto Forum:
  


Current Time: Thu Aug 21 22:04:06 EDT 2014

Powered by FUDForum. Page generated in 0.08302 seconds