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); } }
/** * Checks class descriptors for {@link @RemoveMapping} and {@link RemoveMappings} annotations at the class level * and removes any specified mappings from the ClassDescriptor. * * @param session the current session. */ protected void handleRemoveMapping(Session session) { Map<Class, ClassDescriptor> descriptors = session.getDescriptors(); if (descriptors == null || descriptors.isEmpty()) { return; } for (ClassDescriptor classDescriptor : descriptors.values()) { List<DatabaseMapping> mappingsToRemove = new ArrayList<DatabaseMapping>(); List<RemoveMapping> removeMappings = scanForRemoveMappings(classDescriptor); for (RemoveMapping removeMapping : removeMappings) { if (StringUtils.isBlank(removeMapping.name())) { throw DescriptorException.attributeNameNotSpecified(); } DatabaseMapping databaseMapping = classDescriptor.getMappingForAttributeName(removeMapping.name()); if (databaseMapping == null) { throw DescriptorException.mappingForAttributeIsMissing(removeMapping.name(), classDescriptor); } mappingsToRemove.add(databaseMapping); } for (DatabaseMapping mappingToRemove : mappingsToRemove) { classDescriptor.removeMappingForAttributeName(mappingToRemove.getAttributeName()); } } }
import org.eclipse.persistence.config.DescriptorCustomizer; import org.eclipse.persistence.descriptors.ClassDescriptor; import org.eclipse.persistence.oxm.mappings.XMLObjectReferenceMapping; public class PhoneNumberCustomizer implements DescriptorCustomizer { @Override public void customize(ClassDescriptor descriptor) throws Exception { descriptor.removeMappingForAttributeName("contact"); XMLObjectReferenceMapping contactMapping = new XMLObjectReferenceMapping(); contactMapping.setAttributeName("contact"); contactMapping.setReferenceClass(Employee.class); contactMapping.addSourceToTargetKeyFieldAssociation("contact/@eID", "eId/text()"); contactMapping.addSourceToTargetKeyFieldAssociation("contact/@country", "country/text()"); descriptor.addMapping(contactMapping); } }
jaxbDescriptor.removeMappingForAttributeName(jaxbMapping.getAttributeName()); jaxbDescriptor.addMapping(xmlChoiceMapping); jaxbDescriptor.removeMappingForAttributeName(jaxbMapping.getAttributeName()); jaxbDescriptor.addMapping(xmlChoiceMapping);
/** * Build an XMLInverseMapping based on a particular mapping and replace that mapping with * the newly created XMLInverseMapping in jaxbDescriptor * @param jaxbDescriptor * @param mapping * @param mappedBy */ private static void convertMappingToXMLInverseReferenceMapping(ClassDescriptor jaxbDescriptor, DatabaseMapping mapping, ForeignReferenceMapping jpaMapping) { if ((mapping != null) && (jaxbDescriptor != null)) { if (!(mapping.isXMLMapping())) { return; } if ((jpaMapping.isAggregateCollectionMapping()) || (jpaMapping.isAggregateMapping())) { return; } XMLInverseReferenceMapping jaxbInverseMapping = new XMLInverseReferenceMapping(); copyAccessorToMapping(mapping, jaxbInverseMapping); jaxbInverseMapping.setProperties(mapping.getProperties()); jaxbInverseMapping.setIsReadOnly(mapping.isReadOnly()); jaxbInverseMapping.setMappedBy(jpaMapping.getAttributeName()); if (mapping.isAbstractCompositeCollectionMapping()) { jaxbInverseMapping.setContainerPolicy(mapping.getContainerPolicy()); jaxbInverseMapping.setReferenceClass(((XMLCompositeCollectionMapping) mapping).getReferenceClass()); } else if (mapping.isAbstractCompositeObjectMapping()) { jaxbInverseMapping.setReferenceClass(((XMLCompositeObjectMapping) mapping).getReferenceClass()); } jaxbDescriptor.removeMappingForAttributeName(mapping.getAttributeName()); jaxbDescriptor.addMapping(jaxbInverseMapping); } }
jaxbDescriptor.removeMappingForAttributeName(jaxbMapping.getAttributeName()); jaxbDescriptor.addMapping(xmlChoiceMapping); jaxbDescriptor.removeMappingForAttributeName(jaxbMapping.getAttributeName()); jaxbDescriptor.addMapping(xmlChoiceMapping);
/** * Build an XMLInverseMapping based on a particular mapping and replace that mapping with * the newly created XMLInverseMapping in jaxbDescriptor * @param jaxbDescriptor * @param mapping * @param mappedBy */ private static void convertMappingToXMLInverseReferenceMapping(ClassDescriptor jaxbDescriptor, DatabaseMapping mapping, ForeignReferenceMapping jpaMapping) { if ((mapping != null) && (jaxbDescriptor != null)) { if (!(mapping.isXMLMapping())) { return; } if ((jpaMapping.isAggregateCollectionMapping()) || (jpaMapping.isAggregateMapping())) { return; } XMLInverseReferenceMapping jaxbInverseMapping = new XMLInverseReferenceMapping(); copyAccessorToMapping(mapping, jaxbInverseMapping); jaxbInverseMapping.setProperties(mapping.getProperties()); jaxbInverseMapping.setIsReadOnly(mapping.isReadOnly()); jaxbInverseMapping.setMappedBy(jpaMapping.getAttributeName()); if (mapping.isAbstractCompositeCollectionMapping()) { jaxbInverseMapping.setContainerPolicy(mapping.getContainerPolicy()); jaxbInverseMapping.setReferenceClass(((XMLCompositeCollectionMapping) mapping).getReferenceClass()); } else if (mapping.isAbstractCompositeObjectMapping()) { jaxbInverseMapping.setReferenceClass(((XMLCompositeObjectMapping) mapping).getReferenceClass()); } jaxbDescriptor.removeMappingForAttributeName(mapping.getAttributeName()); jaxbDescriptor.addMapping(jaxbInverseMapping); } }