Mapping Oracle XMLType on JPA (EclipseLink)

前端 未结 2 628
野性不改
野性不改 2021-01-03 05:34

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 solut

2条回答
  •  借酒劲吻你
    2021-01-03 06:16

    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.

提交回复
热议问题