在JPA上映射Oracle XMLType(EclipseLink)

我们有一个具有一些特殊要求的项目,其中一项是从Oracle 10g数据库的XMLType数据库列中获取数据。

我们已经找到了一个使用JDBC的简单解决方案,但它会驱动应用程序有点混乱,因为所有数据访问都是通过JPA完成的(所使用的实现是EclipseLink)。

我们做了一些研究,并找到了一些解决方案,如使用转换器和其他辅助类型,但实现似乎有点复杂。

所以,我的问题是:
您能否建议我使用JPA将XMLType数据列映射到Java Object类型的简单方法?

提前致谢。

您是否尝试将其映射为字符串?

在EclipseLink中,您还可以使用DescriptorCustomizer(尚未支持注释)使用DirectToXMLTypeMapping映射它,或者像您一样使用Converter。

我认为分享詹姆斯回答的完整解决方案会很好。

首先,创建一个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); } } 

然后,您所要做的就是在实体上使用@Customizer anotation,以便EntityManager在处理名为xmlField的属性时使用它(如前面的代码片段所示):

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

xmlField属性不需要@Column anotation,因为它的映射是在我们的DescriptorCustomizer实现中定义的。

而且有它。

Interesting Posts