持久化 java 对象的最简单方法是什么?
现在我有一个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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
序列化到文件系统的传统方法是使用 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)如果您不了解 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.
到目前为止,我遇到的最简单的方法是 db4o:
另外,还有一些非常好的方法可以通过其他方式进行查询。
The easiest way I came across as yet is db4o:
Plus there are really nice ways to query in other ways.
如果可以选择序列化,请考虑使用流行 API,例如 prevalayer 或 Space4J(更新)。关于对象普遍性:
请查看本文以了解有关此主题的更多信息(更多信息请访问 Google)。
If serialization is an option, consider using a prevalence API like prevalayer or Space4J (more recent). About object prevalence:
Check this article to learn more on this topic (more on Google).
听起来您可能想持久保存到数据库。然而,为了避免 DB 的复杂性,将 POJO 持久保存到文件系统的一种简单解决方案是将它们序列化为 XML 文档。 Java 1.6 API 包括 javax.xml.bind 包中的 JAXB 框架。要使用 JAXB,您本质上是注释 POJO 并创建 marshal 和 unmarshal 方法,如下所示:
假设您持久保存 Foo 的实例,其中 mBar 为 42,则此解决方案将生成如下 XML 文件:
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:
Lets say you persist an instance of Foo where mBar is 42 then this solution would produce an XML file like so:
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.