gpt4 book ai didi

java - 使用 Eclipselink 和 Moxy 将 List 写入文件

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:31:24 24 4
gpt4 key购买 nike

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

感谢您提供的任何帮助。

最佳答案

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

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

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

希望对您有所帮助,

瑞克

关于java - 使用 Eclipselink 和 Moxy 将 List<someObject> 写入文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13463593/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com