持久化 Java 对象的最简单方法是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1955396/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 18:42:07  来源:igfitidea点击:

What's the easiest way to persist java objects?

javapersistencejdoobject-persistenceapache-torque

提问by James

Right now I have java program whose classes are currently POJOs and stored in volatile memory. These need to be persisted. As I understand it two popular choices are JDO and the Java Persistence API. For someone who know little about SQL, Torque, etc, which is the easiest way to add persistence to my program's data?

现在我有 Java 程序,其类当前是 POJO 并存储在易失性内存中。这些都需要坚持。据我了解,两个流行的选择是 JDO 和 Java Persistence API。对于对 SQL、Torque 等知之甚少的人来说,向我的程序数据添加持久性的最简单方法是什么?

回答by Brian Agnew

The traditional way to serialise to the filesystem is to use Java Serialisation. However you need to implement Serializableeverywhere.

序列化到文件系统的传统方法是使用Java 序列化。但是,您需要在任何地方实现Serializable

A simpler solution is to serialise to XML (and then dump to the filesystem) using XStream. You don't need to implement any interfaces, and most everything serialises and deserialises without further intervention. You can further customise the serialisation if required. The only problem I've ever had is serialising an inner class without intentionally serialising the containing outer class (this is due to the implicit thisreference)

一个更简单的解决方案是使用XStream序列化为 XML(然后转储到文件系统)。您不需要实现任何接口,并且大多数事情都无需进一步干预即可序列化和反序列化。如果需要,您可以进一步自定义序列化。我遇到的唯一问题是序列化一个内部类,而没有故意序列化包含的外部类(这是由于隐式this引用)

回答by duffymo

Serialize the objects to the file system if you don't know SQL or relational databases.

如果您不了解 SQL 或关系数据库,请将对象序列化到文件系统。

You'll have to learn JDBC to use JDO, JPA, Hibernate, or anything else. Unless your POJOs are terribly complex I'd recommend starting there and working your way up.

您必须学习 JDBC 才能使用 JDO、JPA、Hibernate 或其他任何东西。除非您的 POJO 非常复杂,否则我建议您从那里开始并逐步提高。

Make sure you learn about normalization and proper design of indexes.

确保您了解索引的规范化和正确设计。

回答by Fabian Steeg

The easiest way I came across as yet is db4o:

我遇到的最简单的方法是db4o

ObjectContainer db = Db4o.openFile(location);
db.store(myObject);
List<MyObject> myObjects = db.query(MyObject.class);

Plus there are really nice ways to query in other ways.

此外,还有其他非常好的查询方式。

回答by Pascal Thivent

If serialization is an option, consider using a prevalence API like prevalayeror Space4J(more recent). About object prevalence:

如果序列化是一种选择,请考虑使用流行的 API,如prevalayerSpace4J(更新)。关于对象流行度:

Prevalence is a concept started by Klaus Wuestefeld on how to store data in a real object oriented way, using only memory snapshots, transaction logs and serialization.

Prevalence 是 Klaus Wuestefeld 提出的关于如何以真正的面向对象的方式存储数据的概念,仅使用内存快照、事务日志和序列化。

Check this articleto learn more on this topic (more on Google).

查看本文以了解有关此主题的更多信息(更多信息请访问 Google)。

回答by DataNucleus

DataNucleus is the easiest way since it provides JDO and JPA APIs for persistence to pretty much any type of datastore you would ever want. Why write all of that JAXB code in one of the other replies when DataNucleus does it for you ? All backed by Java standards

DataNucleus 是最简单的方法,因为它提供了 JDO 和 JPA API,用于持久化您想要的几乎任何类型的数据存储。当 DataNucleus 为您做这件事时,为什么要在其他回复之一中编写所有这些 JAXB 代码?全部由 Java 标准支持

回答by jenglert

Sounds like you may want to persist to a DB. However, to avoid the complexities of a DB one simple solution for persisting POJOs to the file system is to serialize them to an XML document. The Java 1.6 API includes the JAXBframework found in the javax.xml.bind package. To use JAXB you essentially annotation your POJO and create marshal and unmarshal methods like so:

听起来您可能想要坚持到数据库。但是,为了避免 DB 的复杂性,将 POJO 持久化到文件系统的一个简单解决方案是将它们序列化为 XML 文档。Java 1.6 API 包括javax.xml.bind 包中的JAXB框架。要使用 JAXB,您本质上需要注释您的 POJO 并创建 marshal 和 unmarshal 方法,如下所示:

@XmlRootElement(name="Foo")
public class Foo {

   @XmlElement(name="Bar")
   public int mBar;

   public static void marshal(Foo foo, OutputStream out) IOException {      
      try {
         JAXBContext jc = JAXBContext.newInstance(Foo.class);
         Marshaller marshaller = jc.createMarshaller();
         marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
         marshaller.marshal(qaConfig, out);
      }
      catch (JAXBException ex) {
         throw new IOException(ex);
      }
      finally {
         out.close();
      }
   }

   public static Foo unmarshal(InputStream in) throws IOException {

      try {
         JAXBContext jc = JAXBContext.newInstance(Foo.class);
         Unmarshaller unmarshaller = jc.createUnmarshaller();

         return (Foo)unmarshaller.unmarshal(in);
      }
      catch (JAXBException ex) {
         throw new IOException(ex);
      }
      finally {
         in.close();
      }
   }
}

Lets say you persist an instance of Foo where mBar is 42 then this solution would produce an XML file like so:

假设您保留了一个 mBar 为 42 的 Foo 实例,那么此解决方案将生成一个 XML 文件,如下所示:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Foo>
   <Bar>42</Bar>
</Foo>