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

发布于 2024-08-16 03:44:53 字数 135 浏览 7 评论 0原文

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

魂ガ小子 2024-08-23 03:44:54

序列化到文件系统的传统方法是使用 Java 序列化。但是,您需要实现 Serialized到处。

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

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

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 this reference)

幻想少年梦 2024-08-23 03:44:54

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

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

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

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

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.

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

似最初 2024-08-23 03:44:54

到目前为止,我遇到的最简单的方法是 db4o

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

另外,还有一些非常好的方法可以通过其他方式进行查询。

The easiest way I came across as yet is 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.

む无字情书 2024-08-23 03:44:54

如果可以选择序列化,请考虑使用流行 API,例如 prevalayerSpace4J(更新)。关于对象普遍性:

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

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

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

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.

Check this article to learn more on this topic (more on Google).

断舍离 2024-08-23 03:44:54

听起来您可能想持久保存到数据库。然而,为了避免 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();
      }
   }
}

假设您持久保存 Foo 的实例,其中 mBar 为 42,则此解决方案将生成如下 XML 文件:

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

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 JAXB framework found in the javax.xml.bind package. To use JAXB you essentially annotation your POJO and create marshal and unmarshal methods like so:

@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:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Foo>
   <Bar>42</Bar>
</Foo>
白龙吟 2024-08-23 03:44:54

DataNucleus 是最简单的方法,因为它提供了 JDO 和 JPA API,可以持久保存几乎任何类型的数据存储曾经想要。当 DataNucleus 为您完成时,为什么要在其他回复之一中编写所有 JAXB 代码呢?全部均由 Java 标准支持且开源。

披露:此答案的作者与 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, and open source.

Disclosure: the author of this answer is associated with the DataNucleus project.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文