Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Config Migratin Using Moxy
Config Migratin Using Moxy [message #957807] Thu, 25 October 2012 14:09 Go to next message
Gaurav Malhotra is currently offline Gaurav MalhotraFriend
Messages: 6
Registered: April 2012
Junior Member
Hi,
I am new to Moxy but I have been using Eclipselink and toplink grid for over 5 year.

We are trying to build config migration tool. My product uses ecliplink/toplinkgrid as an ORM framework.

So I have eclipselink ClassDescriptor with me. What I want to achieve is to covert ClassDescriptor to XMLDescriptor. I don't want to use any moxy XML configuration(s).

I have been seeing moxy examples but not able to understand. So basically I want to create moxy XMLClassDescriptor at SessionCustomizer phase.
That is, from ClassDescriptor get the DatabaseMapping(s) ( classDescriptor.getMappings()) and then create XMLMapping from them

Is it possible?? How?

Gaurav Malhotra
Technical Architect
Oracle NL
Re: Config Migratin Using Moxy [message #958108 is a reply to message #957807] Thu, 25 October 2012 19:16 Go to previous message
Blaise Doughan is currently offline Blaise DoughanFriend
Messages: 163
Registered: July 2009
Senior Member

Hi Gaurav,

EclipseLink MOXy's org.eclipse.persistence.oxm.XMLDescriptor is a subclass of ClassDescriptor so you will immediately recognize many of its APIs. MOXy's mappings are in the org.eclipse.persistence.oxm.mappings package. They actually extend from DatabaseMapping so you will recognize things like AttributeAccessor and ContainerPolicy.

CustomerProject

Below is what the metadata would look like if created with MOXy's native API. This is very similar to creating EclipseLink's ORM metadata via the native APIs.

package nativeapi;

import org.eclipse.persistence.oxm.XMLDescriptor;
import org.eclipse.persistence.oxm.mappings.*;
import org.eclipse.persistence.sessions.Project;

public class CustomerProject extends Project {

    public CustomerProject() {
        this.addDescriptor(buildCustomerDescriptor());
        this.addDescriptor(buildAddressDescriptor());
        this.addDescriptor(buildPhoneNumberDescriptor());
    }

    private XMLDescriptor buildCustomerDescriptor() {
        XMLDescriptor descriptor = new XMLDescriptor();
        descriptor.setJavaClass(Customer.class);
        descriptor.setDefaultRootElement("customer");
        
        XMLDirectMapping nameMapping = new XMLDirectMapping();
        nameMapping.setAttributeName("name");
        nameMapping.setXPath("personal-info/name/text()");
        descriptor.addMapping(nameMapping);
        
        XMLCompositeObjectMapping addressMapping = new XMLCompositeObjectMapping();
        addressMapping.setAttributeName("address");
        addressMapping.setXPath("contact-info/address");
        addressMapping.setReferenceClass(Address.class);
        descriptor.addMapping(addressMapping);
        
        XMLCompositeCollectionMapping phoneNumbersMapping = new XMLCompositeCollectionMapping();
        phoneNumbersMapping.setAttributeName("phoneNumbers");
        phoneNumbersMapping.setXPath("contact-info/phone-number");
        phoneNumbersMapping.setReferenceClass(PhoneNumber.class);
        descriptor.addMapping(phoneNumbersMapping);
        
        return descriptor;
    }
    
    private XMLDescriptor buildAddressDescriptor() {
        XMLDescriptor descriptor = new XMLDescriptor();
        descriptor.setJavaClass(Address.class);
        
        XMLDirectMapping streetMapping = new XMLDirectMapping();
        streetMapping.setAttributeName("street");
        streetMapping.setXPath("street/text()");
        descriptor.addMapping(streetMapping);
        
        return descriptor;
    }

    private XMLDescriptor buildPhoneNumberDescriptor() {
        XMLDescriptor descriptor = new XMLDescriptor();
        descriptor.setJavaClass(PhoneNumber.class);
        
        XMLDirectMapping valueMapping = new XMLDirectMapping();
        valueMapping.setAttributeName("value");
        valueMapping.setXPath("text()");
        descriptor.addMapping(valueMapping);
        
        return descriptor;
    }

}


Demo

Below is some sample code demonstrating how to leverage the metadata to read/write XML.

package nativeapi;

import java.io.File;
import org.eclipse.persistence.oxm.*;

public class Demo {

    public static void main(String[] args) {
        CustomerProject project = new CustomerProject();
        XMLContext xmlContext = new XMLContext(project);
        
        XMLUnmarshaller unmarshaller = xmlContext.createUnmarshaller();
        File xml = new File("src/nativeapi/input.xml");
        Customer customer = (Customer) unmarshaller.unmarshal(xml);
        
        XMLMarshaller marshaller = xmlContext.createMarshaller();
        marshaller.marshal(customer, System.out);
    }

}


input.xml/Output

<?xml version="1.0" encoding="UTF-8"?>
<customer>
   <personal-info>
      <name>Jane Doe</name>
   </personal-info>
   <contact-info>
      <address>
         <street>123 A Street</street>
      </address>
      <phone-number>555-1111</phone-number>
      <phone-number>555-2222</phone-number>
   </contact-info>
</customer>


Customer

package nativeapi;

import java.util.List;

public class Customer {

    private String name;
    private Address address;
    private List<PhoneNumber> phoneNumbers;

}


Address

package nativeapi;

public class Address {

    private String street;

}


PhoneNumber

package nativeapi;

public class PhoneNumber {

    private String value;

}

I will contact you by email to find out more information about what you are trying to build.

-Blaise

[Updated on: Thu, 25 October 2012 19:40]

Report message to a moderator

Previous Topic:[SOLVED] integrity constraint violation after mergin list of unmanaged entities
Next Topic:EntityListener not called (SOLVED)
Goto Forum:
  


Current Time: Fri Apr 26 23:29:49 GMT 2024

Powered by FUDForum. Page generated in 0.02935 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top