持久化 Java 对象的轻量级选项是什么
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/450864/
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
What are the lightweight options one has to persist Java objects
提问by Yann Semet
What are the lightweight options one has to persist Java objects ?
持久化 Java 对象的轻量级选项是什么?
I'm looking for the easiest possible solution. I don't want anything fancy featurewise just something simple that works with reasonnably simple objects (some collections, relationships and simple inheritance) and doesn't bring too much apparent complexity (if any) to the existing codebase.
我正在寻找最简单的解决方案。我不想要任何花哨的功能,只是一些简单的东西,可以与合理简单的对象(一些集合、关系和简单的继承)一起工作,并且不会给现有的代码库带来太多明显的复杂性(如果有的话)。
The options I'm aware of include Hibernate and frameworks such as EMF but they sound (and have been experienced to be) too complex and time-consuming. I'd like something out of the box, preferably file oriented than dababase oriented, that I can just put on top of my plain old java objects.
我知道的选项包括 Hibernate 和 EMF 等框架,但它们听起来(并且已经经历过)太复杂和耗时。我想要一些开箱即用的东西,最好是面向文件的而不是面向 dababase 的,我可以把它放在我普通的旧 java 对象之上。
This is a newbie question, thanks in advance for any tutorial-like, context clarifying guidance.
这是一个新手问题,提前感谢您提供任何类似教程的上下文澄清指导。
回答by Allain Lalonde
回答by Chris Dail
If you are looking at something simple you might also want the data in a format you can read (not a binary file or database). If that is the case, you should look at JAXB (Java's XML Binding) that is part of Java 6 and later. There are other technologies that may be able to do this better such as XML Beans but this one is built in.
如果您正在查看一些简单的内容,您可能还需要一种您可以读取的格式的数据(而不是二进制文件或数据库)。如果是这种情况,您应该查看 JAXB(Java 的 XML 绑定),它是 Java 6 及更高版本的一部分。还有其他技术可以更好地做到这一点,例如 XML Beans,但这是内置的。
Take a look at this page from Java's API. It is pretty straight forward to serializing and deserializing Java objects.
从 Java 的 API 看一下这个页面。序列化和反序列化 Java 对象非常简单。
http://java.sun.com/javase/6/docs/api/javax/xml/bind/JAXBContext.html
http://java.sun.com/javase/6/docs/api/javax/xml/bind/JAXBContext.html
Basically you use the following:
基本上你使用以下内容:
JAXBContext jc = JAXBContext.newInstance( "com.acme.foo" );
// unmarshal from foo.xml
Unmarshaller u = jc.createUnmarshaller();
FooObject fooObj = (FooObject)u.unmarshal( new File( "foo.xml" ) );
// marshal to System.out
Marshaller m = jc.createMarshaller();
m.marshal( fooObj, System.out );
Just make sure your FooObject has the @XmlRootElement annotation. Java bean properties are automatically interpreted but you can use other annotations to control how the XML looks and model more complex objects.
只需确保您的 FooObject 具有 @XmlRootElement 注释。Java bean 属性会自动解释,但您可以使用其他注释来控制 XML 的外观和建模更复杂的对象。
回答by Sarel Botha
The ObjectOutputStream and ObjectInputStream can do this. It's built into Java and allows you to serialize/deserialize objects quite easily.
ObjectOutputStream 和 ObjectInputStream 可以做到这一点。它内置于 Java 中,允许您非常轻松地序列化/反序列化对象。
The bad thing about these is that if you change your objects you can't import existing old objects. You'll have to think of a way to stay compatible with existing objects that might already be saved on a user's computer, such as adding a version number to your classes and creating the ability to convert old objects to new ones.
这些的坏处是,如果您更改对象,则无法导入现有的旧对象。您必须想办法与可能已保存在用户计算机上的现有对象保持兼容,例如向类添加版本号并创建将旧对象转换为新对象的能力。
More info here:
更多信息在这里:
- http://java.sun.com/j2se/1.4.2/docs/api/java/io/ObjectOutputStream.html
- http://java.sun.com/j2se/1.4.2/docs/api/java/io/ObjectInputStream.html
- http://java.sun.com/j2se/1.4.2/docs/api/java/io/ObjectOutputStream.html
- http://java.sun.com/j2se/1.4.2/docs/api/java/io/ObjectInputStream.html
EDIT: You may also consider adding the following attribute to all your classes right off the bat. This may allow you to then add attributes to classes and still be able to load old Object files. In RMI-land this works, but I'm not sure about files. Don't ever remove attributes though.
编辑:您也可以考虑立即将以下属性添加到您的所有类中。这可能允许您向类添加属性,并且仍然能够加载旧的对象文件。在 RMI-land 这有效,但我不确定文件。永远不要删除属性。
static final long serialVersionUID = 1L;
回答by ng.
Try Simple XML persistence http://simple.sourceforge.net. Its really simple and requires no configuration. Just take your existing POJOs and annotate them, then you can read and write them to a file. Cyclical object graphs are supported as well as all Java collections. It can be used like so.
尝试简单的 XML 持久性http://simple.sourceforge.net。它真的很简单,不需要配置。只需获取您现有的 POJO 并对其进行注释,然后您就可以读取它们并将它们写入文件。支持循环对象图以及所有 Java 集合。它可以像这样使用。
Serializer serializer = new Persister();
MyObject object = serializer.read(MyObject.class, new File("file.xml));
and writing is just as easy
写作同样简单
serializer.write(myInstance, new File("file.xml"));
This is an extremely lightweight approach, no dependancies other than standard Java 1.5. Compared with other object to XML technologies such as JAXB, XStream, Castor, which are dependant on a whole host of other projects Simple XML is very lightweight and memory efficient.
这是一种非常轻量级的方法,除了标准 Java 1.5 之外没有任何依赖项。与其他对象到 XML 技术(如 JAXB、XStream、Castor)相比,这些技术依赖于大量其他项目,简单的 XML 非常轻量级且内存效率高。
回答by kgiannakakis
See this similar question Light-weight alternative to hibernate. The most light weight framework I know of is iBatis
看到这个类似的问题轻量级替代 hibernate。我所知道的最轻量级的框架是iBatis
回答by willcodejavaforfood
回答by willcodejavaforfood
One simple approach is to serialize (as in Serializable) your objects to disk.
一种简单的方法是将对象序列化(如在 Serializable 中)到磁盘。
回答by Milhous
回答by tddmonkey
I have to say the initial learning curve for Hibernate is relatively shallow. You should be able to get a test system up and running in less than a day. It's only when you want the more advanced features where it starts to get steeper. I would recommend you definitely take a look, I mean what's a day if you end up not choosing it?
我不得不说 Hibernate 的初始学习曲线相对较浅。您应该能够在不到一天的时间内启动并运行测试系统。只有当您想要更高级的功能时,它才会变得更陡峭。我建议你一定要看看,我的意思是,如果你最终不选择它,那一天算什么?
Having said that, have you considered just serializing your objects directly to disk if you just want something quick n dirty?
话虽如此,如果您只是想要一些快速且肮脏的东西,您是否考虑过将对象直接序列化到磁盘?
回答by Archie
I have used PersistentObjectextensively in production code and it serves my needs well.
我在生产代码中广泛使用了PersistentObject,它很好地满足了我的需求。

