Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Write List<Object> to file using Eclipselink Moxy(Write List<Object> to file using Eclipselink Moxy)
Write List<Object> to file using Eclipselink Moxy [message #986317] Mon, 19 November 2012 22:15 Go to next message
David Pierre is currently offline David Pierre
Messages: 1
Registered: November 2012
Location: So Cal
Junior Member
Hello
I'm using eclipselink JPA and have a collection of DB Entities I'd want to write to a file in case my DB connection is not available on subsequent queries. I'd like to use Moxy to simply marshal my entire result-set then at a later time unmarhall that file, thus recreating my original result-set(which are my JPA Entity objects). Since Eclipselink and Moxy are somewhat integrated I'd like to know inside my JPA code for example:

public void getDataFROMDATABASE() {

factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME,getProperties());
EntityManager em = factory.createEntityManager();
// Read the existing entries and write to console
TypedQuery<CtoPolicyMatrix> query = em.createQuery("SELECT pm FROM CtoPolicyMatrix pm", CtoPolicyMatrix.class);

List<CtoPolicyMatrix> results = query.getResultList();
// NOW PRESIST RESULT IN CASE OF DB CONNECTION FAILURE
presistMatrixData(List results;
em.close();
}

public void presistMatrixData(List results){
// Save CtoPolicyMatrix to XML
try {
jc = JAXBContext.newInstance(CtoPolicyMatrix.class);
Marshaller marshaller = jc.createMarshaller();
StringWriter writer = new StringWriter();
marshaller.marshal(results, writer);
???? NOT SURE WHAT TO DO HERE TO WRITE MY COLLECTION TO FILE

} catch (JAXBException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public void retrieveMatrixData(List results){
// Load CtoPolicyMatrix from XML
???? REALLY NOT SURE HOW TO RETRIEVE MY JPA ENTITY COLLECTION FROM FILE
Unmarshaller unmarshaller;
try {
unmarshaller = jc.createUnmarshaller();
StringReader reader = new StringReader(writer.toString());
List<CtoPolicyMatrix> savedResults = (List<CtoPolicyMatrix>) unmarshaller.unmarshal(reader);
} catch (JAXBException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

Thanks for any help you can provide.
Re: Write List<Object> to file using Eclipselink Moxy [message #986530 is a reply to message #986317] Tue, 20 November 2012 18:33 Go to previous message
Rick Barkhouse is currently offline Rick Barkhouse
Messages: 22
Registered: July 2009
Junior Member
Hi David,

You are on the right track. To make things as easy as possible I would suggest creating a wrapper object to contain your list of results, and then marshal/unmarshal that wrapper object.

A wrapper object could look like this:

    import java.util.List;

    import javax.xml.bind.annotation.XmlRootElement;

    @XmlRootElement
    public class MatrixData {

        private List<CtoPolicyMatrix> data;

        public List<CtoPolicyMatrix> getData() {
            return data;
        }

        public void setData(List<CtoPolicyMatrix> data) {
            this.data = data;
        }
        
    }


Your persistMatrixData method would look like this:

    public void persistMatrixData(List results) {
        // Save CtoPolicyMatrix to XML
        try {
            MatrixData data = new MatrixData();
            data.setData(results);

            File file = new File("D:/Temp/MatrixData.xml");

            jc = JAXBContext.newInstance(MatrixData.class);
            Marshaller marshaller = jc.createMarshaller();
            marshaller.marshal(data, file);
        } catch (JAXBException e) {
            e.printStackTrace();
        }
    }


And reading them back in:

    public void retrieveMatrixData(List results) {
        // Load CtoPolicyMatrix from XML
        Unmarshaller unmarshaller;
        try {
            File file = new File("D:/Temp/MatrixData.xml");

            unmarshaller = jc.createUnmarshaller();
            MatrixData data = (MatrixData) u.unmarshal(file);

            List<CtoPolicyMatrix> savedResults = data.getData();
        } catch (JAXBException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }


Hope this helps,
Rick
Previous Topic:EclipseLink Partition and native query
Next Topic:pool: 1 connection
Goto Forum:
  


Current Time: Sun Oct 26 04:29:02 GMT 2014

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

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