Tuesday, August 30, 2011

Mapping XMLTYPE

A recent posting on the TopLink Forum made it clear that previous descriptions of how to map a JPA entity attribute of type org.w3c.dom.Document to an Oracle database XMLTYPE column didn't provide enough information for schema (DDL) generation. So I thought I'd post an example that does. In the current 2.3.0 release of EclipseLink a mapping to XMLTYPE must be configured programatically through a descriptor customizer but declarative annotation and XML configuration support is scheduled for an upcoming release.

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))

Thursday, July 28, 2011

GlassFish 3.1.1 Ships with EclipseLink 2.3!

GlassFish 3.1.1 shipped with EclipseLink 2.3 today! Check out my post on the EclipseLink Team Blog for details.

Shaun Smith, EclipseLink Team
@shaunmsmith

Tuesday, March 1, 2011

The Poetry of Google Translated Twitter

Doug Clarke recently setup a Google Reader aggregator that included a search of Twitter for the term "JPA". Some of the messages are not in English but Google Reader is happy to translate them. Unfortunately, or maybe fortunately in this case, mechanical translation isn't perfect and sometimes results in what feels like free verse poetry.

Here's a sample:
  • JPA is the itches of God
  • I eat, jpa back. a kiss that cheese is my: *
  • In JPA I do not feel hungry. I have to live there.