Here's a simple entity with an XML document 'resume' attribute:
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import org.eclipse.persistence.annotations.Customizer;
import org.w3c.dom.Document;
@Entity
@Customizer(XMLTypeAttributeCustomizer.class)
public class JobCandidate {
@Id
@GeneratedValue
private long id;
private Document resume;
public long getId() {
return id;
}
public void setId(final long id) {
this.id = id;
}
public Document getMessage() {
return resume;
}
public void setMessage(Document message) {
this.resume = message;
}
}
And here is a customizer class that will redefine the mapping for the resume attribute to be an EclipseLink DirectToXMLTypeMapping:
import org.eclipse.persistence.config.DescriptorCustomizer;
import org.eclipse.persistence.descriptors.ClassDescriptor;
import org.eclipse.persistence.mappings.xdb.DirectToXMLTypeMapping;
public class XMLTypeAttributeCustomizer implements DescriptorCustomizer {
@Override
public void customize(final ClassDescriptor descriptor) throws Exception {
// Remove JPA default Basic mapping
descriptor.removeMappingForAttributeName("resume");
final DirectToXMLTypeMapping mapping = new DirectToXMLTypeMapping();
mapping.setAttributeName("resume");
mapping.setFieldName("RESUME");
mapping.getField().setColumnDefinition("sys.XMLTYPE");
descriptor.addMapping(mapping);
}
}
The hightlighted line sets the database type of the mapping which is used by EclipseLink DDL generation and produces:
[EL Fine]: CREATE TABLE JOBCANDIDATE (ID NUMBER(19) NOT NULL, RESUME sys.XMLTYPE, PRIMARY KEY (ID))