4

We have a project with some special requirements, one of wich is getting data from a XMLType database column from an Oracle 10g database.

We have found an easy solution using JDBC, but it would drive the application a little messy, as all the data access is being done through JPA (the implementation used is EclipseLink).

We have done some research, and have found some solutions, as using Converters and other auxiliar types, but the implementations seemed a little complicated.

So, my question is:
Could you recommend me an easy way to map an XMLType data column to a Java Object type, using JPA?

Thanks in advance.

0

2 Answers 2

2

Did you try just mapping it as a String?

In EclipseLink you can also map it using a DirectToXMLTypeMapping using a DescriptorCustomizer (no annotation support yet), or using the Converter as you have done.

2
  • Thanks for your answer. Currently, my co-workers have opted for mapping it a a String, and get the column from the database as a CLOB type. But I still want to know if there is a more direct way to do it at the Java layer. I'll check your solution. Oct 19, 2010 at 13:49
  • Finally, although in my opinion this is the right way to implement it, we are going to go ahead with the Clob workaround. Thanks anyway @James. Oct 20, 2010 at 10:39
2

I think that it would be fine to share the full solution resulting from James' answer.

First, create a DescriptorCustomizer implmentation:

import org.eclipse.persistence.config.DescriptorCustomizer;
import org.eclipse.persistence.descriptors.ClassDescriptor;
import org.eclipse.persistence.mappings.xdb.DirectToXMLTypeMapping;

public class XMLDataCustomizer  implements DescriptorCustomizer {

    public void customize(final ClassDescriptor descriptor) throws Exception {
        descriptor.removeMappingForAttributeName("xmlField");
        DirectToXMLTypeMapping mapping = new DirectToXMLTypeMapping();
        mapping.setAttributeName("xmlField"); //name of the atribute on the Entity Bean
        mapping.setFieldName("XML_COLUMN"); //name of the data base column
        descriptor.addMapping(mapping);
    }

}

Then, all you have to do is use the @Customizer anotation on the entity, for the EntityManager to make use of it when handling the property called xmlField (as seen at the previous code snippet):

@Entity
@Table(name="TABLE_NAME")
@NamedQueries({ /* ... */})
@Customizer(XMLDataCustomizer.class)
public class DataEntity implements Serializable {
   /* ... */

   private String xmlField;

   /* .... */ 
}

The xmlField attribute does not need @Column anotation, as it's mapping is defined at our DescriptorCustomizer implementation.

And there is it.

1
  • so what does this appoach buy you? xmlField is still a string in java land?? Apr 30, 2014 at 14:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.