简体   繁体   中英

Is it possible to save persistent objects to the file system

I'd like to save persistent objects to the file system using Hibernate without the need for a SQL database.

Is this possible?

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.

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.

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.

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.

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.

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.

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 ,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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