是否可以将持久对象保存到文件系统
我想使用 Hibernate 将持久对象保存到文件系统,而不需要 SQL 数据库。
这可能吗?
I'd like to save persistent objects to the file system using Hibernate without the need for a SQL database.
Is this possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
Hibernate 在 JDBC 之上工作,因此您只需要一个 JDBC 驱动程序和匹配的 Hibernate 方言。
然而,JDBC 基本上是 SQL 的抽象,因此无论您使用什么,其外观、运行方式和运行方式都将像 SQL 数据库一样 — 您不妨使用一个数据库,这样可以避免很多麻烦。此外,任何此类解决方案在大小和复杂性方面都将与 Derby 等轻量级 Java DB 相当。
当然,如果你不绝对坚持使用Hibernate,还有很多其他选择。
Hibernate works on top of JDBC, so all you need is a JDBC driver and a matching Hibernate dialect.
However, JDBC is basically an abstraction of SQL, so whatever you use is going to look, walk and quack like an SQL database - you might as well use one and spare yourself a lot of headaches. Besides, any such solution is going to be comparable in size and complexity to lighweight Java DBs like Derby.
Of course if you don't insist absolutely on using Hibernate, there are many other options.
如果您使用 JDBC 纯文本驱动程序,这在技术上似乎是可能的;但是我还没有看到任何提供写访问权限的开源软件;我在sourceforge 上找到的那个是只读的。
It appears that it might technically be possible if you use a JDBC plaintext driver; however I haven't seen any opensource ones which provide write access; the one I found on sourceforge is read-only.
您已经有了一个实体模型,我想您不想丢失它或其中包含的关系。实体模型旨在转换为关系数据库。
Hibernate 和任何其他 JPA 提供程序 (EclipseLink) 将此实体模型转换为 SQL。它们使用 JDBC 驱动程序提供与 SQL 数据库的连接。这个,你也需要保留。
正确的问题是:有人知道一种可以从 Java 内部启动的
嵌入式 Java SQL 数据库
吗?本主题中提到了很多这样的内容:我在一个用于小数据的项目中使用了 HyperSQL,并在一个包含大型数据库(2Gb 或更多)的项目中使用了 Apache Derby。 Apache Derby 在这些庞大的数据库上表现更好。
You already have an entity model, I suppose you do not want to lose this nor the relationships contained within it. An entity model is directed to be translated to a relational database.
Hibernate and any other JPA provider (EclipseLink) translate this entity model to SQL. They use a JDBC driver to provide a connection to an SQL database. This, you need to keep as well.
The correct question to ask is: does anybody know an
embedded Java SQL database
, one that you can start from within Java? There are plenty of those, mentioned in this topic:I have used HyperSQL on one project for small data, and Apache Derby for a project with huge databases (2Gb and more). Apache Derby performs better on these huge databases.
我不确切知道您的需求,但可能是以下之一:
1 - 如果您的需求只是摆脱 SQL,您可以使用 NoSQL 数据库。
Hibernate 通过 Hibernate OGM 支持它 ( http://www.hibernate.org/subprojects/ogm )。
有一些数据库,如 Cassandra、MongoDB、CouchDB、Hadoop...您有一些建议 这里
。
2 - 现在,如果您不想使用数据库服务器(服务进程始终运行),您可以使用 Apache Derby。它是一个数据库,就像任何其他 SQL 一样,但不需要服务器。它使用单个文件来保存数据。您可以使用您的程序轻松传输所有数据库。
看一下: http://db.apache.org/derby/
3 - 如果您真的想要一些文本纯文件,你可以像迈克尔·博格沃特说的那样。但我不知道在这种情况下 Hibernate 是否是一个好主意。
I don't know exactaly your need, but maybe it's one of below:
1 - If your need is just run away from SQL, you can use a NoSQL database.
Hibernate suports it through Hibernate OGM ( http://www.hibernate.org/subprojects/ogm ).
There are some DBs like Cassandra, MongoDB, CouchDB, Hadoop... You have some suggestions Here
.
2 - Now, if you want not to use a database server (with a service process running always), you can use Apache Derby. It's a DB just like any other SQL, but no need of a server. It uses a singular file to keep data. You can easily transport all database with your program.
Take a look: http://db.apache.org/derby/
3 - If you really want some text plain file, you can do like Michael Borgwardt said. But I don't know if Hibernate would be a good idea in this case.
H2 和 HyperSQL< /a> 支持嵌入模式(在 JVM 内部运行,而不是在单独的服务器中运行)并保存到本地文件;这些仍然是 SQL 数据库,但对于 Hibernate 来说没有太多其他选择。
Both H2 and HyperSQL support embedded mode (running inside your JVM instead of in a separate server) and saving to local file(s); these are still SQL databases, but with Hibernate there's not many other options.
好吧,由于问题仍然悬而未决,OP 表示他愿意接受新的方法/建议,这是我的(有点晚了,但还可以)。
你知道Prevayler吗?它是一个 Java Prevalence 实现,它将所有业务对象保留在 RAM 中并在文件系统中保留快照/更改日志,这样它非常快速且可靠,因为如果发生任何崩溃,它会恢复其最后的状态并将每个更改重新应用到它。
此外,在您的应用程序中设置和运行非常容易。
Well, since the question is still opened and the OP said he's opened to new approaches/suggestions, here's mine (a little late but ok).
Do you know Prevayler? It's a Java Prevalence implementation which keep all of your business objects in RAM and mantain Snapshots/Changelogs in the File System, this way it's extremely fast and reliable, since if there's any crash, it'll restore it's last state and reapply every change to it.
Also, it's really easy to setup and run in your app.
当然这是可能的,您可以简单地使用Java的文件io功能,需要以下步骤: -
2.创建一个 FileInputStream 对象(尽管有一些方法使用其他类)
java.util.Scanner
中。请注意,您的对象必须实现
Serialized
接口。 请参阅以下链接,Ofcourse this is possible, You can simply use file io features of Java, following steps are required:-
2.Create an object of FileInputStream (though there are ways which use other Classes)
java.util.Scanner
.Note that your object must implement
Serializable
interface. See following link,