代码之家  ›  专栏  ›  技术社区  ›  Tomas Narros

在JPA上映射Oracle XMLType(EclipseLink)

  •  4
  • Tomas Narros  · 技术社区  · 14 年前

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

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

    所以,我的问题是:
    您能推荐一种使用JPA将XMLType数据列映射到Java对象类型的简单方法吗?

    提前谢谢。

    2 回复  |  直到 14 年前
        1
  •  2
  •   James    14 年前

    你试过把它映射成字符串吗?

    在EclipseLink中,您还可以使用DirectToXMLTypeMapping使用描述符RCustomizer(还不支持注释)映射它,或者像您所做的那样使用转换器。

        2
  •  2
  •   Tomas Narros    12 年前

    我认为分享詹姆斯的回答所产生的全部解决方案是可以的。

    首先,创建 DescriptorCustomizer 实施:

    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 对实体的命名,为 EntityManager 在处理 xmlField

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

    这个 xmlField公司 属性不需要 @Column anotation,因为它的映射定义在 描述子 实施。

    就这样。