使用Eclipselink和Moxy将List<someObject>写入文件

3

我正在使用Eclipselink JPA,并拥有一组数据库实体,我想在后续查询中无法连接到数据库时将其写入文件。我希望使用Moxy简单地编组我的整个结果集,然后在以后的某个时间解组该文件,从而重新创建我的原始结果集(即我的JPA实体对象)。由于Eclipselink和Moxy有些集成,因此我想在我的JPA代码内部了解例如:

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();
    }
}

感谢您能提供任何帮助。
1个回答

2

你已经走在正确的道路上。为了尽可能简化事情,我建议创建一个包装对象来包含你的结果列表,然后进行编组/解组该包装对象。

包装对象可以如下所示:

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;
    }

}

您的persistMatrixData方法应该像这样:

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();
    }
}

然后将它们读取回来:

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();
    }
}

希望这有所帮助,
Rick

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接