Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用Eclipselink和Moxy将列表写入文件_Java_Jpa_Jaxb_Eclipselink_Moxy - Fatal编程技术网

Java 使用Eclipselink和Moxy将列表写入文件

Java 使用Eclipselink和Moxy将列表写入文件,java,jpa,jaxb,eclipselink,moxy,Java,Jpa,Jaxb,Eclipselink,Moxy,我使用的是Eclipselink JPA,有一组DB实体,我想写入一个文件,以防我的DB连接在后续查询中不可用。我想使用Moxy简单地封送我的整个结果集,然后在以后的时间取消对该文件的封送,从而重新创建我的原始结果集,它是我的JPA实体对象。由于Eclipselink和Moxy在某种程度上是集成的,我想知道我的JPA代码中的一些信息,例如: public void getDataFROMDATABASE() { factory = Persistence.createEntityManager

我使用的是Eclipselink JPA,有一组DB实体,我想写入一个文件,以防我的DB连接在后续查询中不可用。我想使用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();
    }
}

感谢您提供的任何帮助。

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

包装器对象可能如下所示:

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

}
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();
    }
}
希望这有帮助

瑞克