是否可以将持久对象保存到文件系统

发布于 2024-12-24 16:19:25 字数 62 浏览 2 评论 0原文

我想使用 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 技术交流群。

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

发布评论

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

评论(7

鲜肉鲜肉永远不皱 2024-12-31 16:19:25

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.

最舍不得你 2024-12-31 16:19:25

如果您使用 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.

坏尐絯℡ 2024-12-31 16:19:25

您已经有了一个实体模型,我想您不想丢失它或其中包含的关系。实体模型旨在转换为关系数据库。

Hibernate 和任何其他 JPA 提供程序 (EclipseLink) 将此实体模型转换为 SQL。它们使用 JDBC 驱动程序提供与 SQL 数据库的连接。这个,你也需要保留。

正确的问题是:有人知道一种可以从 Java 内部启动的嵌入式 Java SQL 数据库吗?本主题中提到了很多这样的内容:

  • HyperSQL:将结果存储在 SQL 明文文件中,可以轻松导入到任何其他数据库中
  • H2:使用二进制文件,JAR 文件大小较小
  • Derby:使用二进制文件
  • Ashpool:将数据存储在XML 结构的文件

我在一个用于小数据的项目中使用了 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:

  • HyperSQL: stores the result in an SQL clear-text file, readily imported into any other database
  • H2: uses binary files, low JAR file size
  • Derby: uses binary files
  • Ashpool: stores data in an XML-structured file

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.

萌能量女王 2024-12-31 16:19:25

我不确切知道您的需求,但可能是以下之一:

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.

逆流 2024-12-31 16:19:25

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.

再可℃爱ぅ一点好了 2024-12-31 16:19:25

好吧,由于问题仍然悬而未决,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.

凉月流沐 2024-12-31 16:19:25

当然这是可能的,您可以简单地使用Java的文件io功能,需要以下步骤: -

  1. 创建一个文件对象
    2.创建一个 FileInputStream 对象(尽管有一些方法使用其他类)
  2. 将此对象包装在 Buffer 对象中或简单地放在 java.util.Scanner 中。
  3. 使用上一步中创建的对象的特定写入函数。

请注意,您的对象必须实现 Serialized 接口。 请参阅以下链接

Ofcourse this is possible, You can simply use file io features of Java, following steps are required:-

  1. Create a File Object
    2.Create an object of FileInputStream (though there are ways which use other Classes)
  2. Wrap this object in a Buffer object or simply inside a java.util.Scanner.
  3. use specific write functions of the object created in previous step.

Note that your object must implement Serializable interface. See following link,

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