简体   繁体   English

Opensource库将java对象的树保存到db或file

[英]Opensource library to save tree of java objects to db or file

I am looking for an opensource library which can be used to transparently save java objects(and their dependencies!) to file or db, a kind of snapshot. 我正在寻找一个开源库,可以用来透明地将java对象(及其依赖项!)保存到文件或数据库,这是一种快照。

Sample use case: I have the state of a game in a bunch of objects, player, score, location etc. User clicks "save" and now I need to save these objects(all of the objects to be saved can be annotated) to a file or db. 示例用例:我在一堆对象,玩家,得分,位置等中拥有游戏状态。用户点击“保存”,现在我需要保存这些对象(所有要保存的对象都可以注释)到文件或数据库。 If system crashes or user log's in later, I should be able to recover from this point on. 如果以后系统崩溃或用户登录,我应该能够从这一点开始恢复。

I am looking for a library that can do it as transparently and efficiently as possible. 我正在寻找一个可以尽可能透明和高效地完成它的库。 Transaction etc is not necessary. 交易等不是必需的。 My priorities would be 我的优先事项是

  1. OpenSource, free, Good documentation OpenSource,免费,良好的文档
  2. Ease of use. 使用方便。 User should not have to manually save the object after something is modified. 用户不必在修改内容后手动保存对象。 Of if (s)he has to, it should be trivial activity 如果他必须这样做,那应该是微不足道的活动
  3. Stable 稳定
  4. Should be easy to read from snapshot and recover. 应该很容易从快照中读取并恢复。

Does a library exist that covers all 4 or at least a good portion of the above req's? 是否存在涵盖上述所有4个或至少很大一部分需求的库? I am opent to aspect oriented approaches 我倾向于面向方面的方法

Am I asking the wrong q for my usecase? 我问错了q我的用例吗? Is it done very differently in the industry? 它在行业中的表现有很大不同吗?

You can use standard java serialization mechanism. 您可以使用标准的java序列化机制。 If all your classes that you want to save implement tag interface Serializable you can transparently save as big as you want graph of objects. 如果要保存的所有类都实现了标记接口Serializable ,则可以透明地保存对象图形。 It is not DB, it is file, but you can always read these objects back to your application and probably it is the best (or the first) option for you. 它不是数据库,它是文件,但您始终可以将这些对象读回您的应用程序,并且它可能是您最好的(或第一个)选项。

The disadvantage of this method is that the data is binary, ie you cannot read it using Notepad. 这种方法的缺点是数据是二进制的,即您无法使用记事本读取它。 And format is very sensitive to changes done in classes, so support of backwards/forward compatibility is problematic. 格式对类中所做的更改非常敏感,因此支持向后/向前兼容性是有问题的。

Other possibility is to serialize objects as XML or JSON. 其他可能性是将对象序列化为XML或JSON。 For XML try JAXB or XStream. 对于XML,请尝试JAXB或XStream。 For JSON take a look here . 对于JSON,请看这里

You should be able to use the built in serialization of java to solve this. 您应该能够使用java的内置序列化来解决这个问题。 If you want it to save automatically, wrap your model in a layer which saves it for you. 如果您希望自动保存,请将模型包装在一个为您保存的图层中。 This could even be done "automatically" using proxies (java.lang.reflect.Proxy). 甚至可以使用代理(java.lang.reflect.Proxy)“自动”完成。

However, I usually do not recommend automatically saving state as values often are related. 但是,我通常不建议自动保存状态,因为值通常是相关的。 Imagine a progress class which keeps track of which level you are on and how far within that level you have gotten (say 0%, 25%, 50% and 75%). 想象一下一个进度课程,它跟踪你所处的水平以及你所达到的水平(例如0%,25%,50%和75%)。

Now, you are just completing level 2 so the progress has to be updated to "Level 3, 0%". 现在,您刚刚完成2级,因此必须将进度更新为“3级,0%”。 If you first set the level the progress will (for a short period) have the state "Level 3, 75%" which is incorrect, and if you set how far first you will get "Level 2, 0%". 如果您首先设置级别,则进度将(在短时间内)具有状态“级别3,75%”,这是不正确的,并且如果您设置首先将获得“级别2,0%”。 Should the program stop at either of those points, then the user will have an incorrect state. 如果程序停在这两个点中的任何一个,则用户将具有不正确的状态。

There is an build-in-functionality called ObjectInput/ObjectOutput-Stream. 有一个名为ObjectInput / ObjectOutput-Stream的内置功能。

http://docs.oracle.com/javase/6/docs/api/java/io/ObjectInputStream.html http://docs.oracle.com/javase/6/docs/api/java/io/ObjectInputStream.html

If you like to have human-readable file's you may like to use XStream. 如果你想拥有人类可读的文件,你可能想要使用XStream。

http://x-stream.github.io/faq.html http://x-stream.github.io/faq.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM