Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use Eclipselink and Moxy to write List<someObject> to file

I’m using Eclipselink JPA and have a collection of DB Entities I 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 RESULTS to file IN CASE OF DB CONNECTION FAILURE**
    presistMatrixData(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) {
        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.

like image 994
user1837085 Avatar asked Dec 11 '25 07:12

user1837085


1 Answers

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

like image 187
Rick Barkhouse Avatar answered Dec 13 '25 05:12

Rick Barkhouse



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!