【问题标题】:Use Eclipselink and Moxy to write List<someObject> to file使用 Eclipselink 和 Moxy 将 List<someObject> 写入文件
【发布时间】:2012-11-19 22:37:05
【问题描述】:

我正在使用 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();
    }
}

感谢您提供的任何帮助。

【问题讨论】:

    标签: java jpa jaxb eclipselink moxy


    【解决方案1】:

    你在正确的轨道上。为了使事情尽可能简单,我建议创建一个包装器对象来包含您的结果列表,然后编组/解组该包装器对象。

    包装对象可能如下所示:

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

    希望这会有所帮助,

    瑞克

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-12-23
      • 1970-01-01
      • 2014-09-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多