views:

1138

answers:

4

Hey all,

I am using Hibernate in a Java application to access my Database and it works pretty well with MS-SQL and MySQL. But some of the data I have to show on some forms has to come from Text files, and by Text files I mean Human-Readable files, they can be CSV, Tab-Delimited, or even a key, value pair, per line since my data is as simple as this, but my preference of course is XML files.

My question is: Can I use hibernate to read those files using HQL, Query , EntityManager and all those resources Hibernate provides me to access files. Which file format should I use and How I configure My persistence.xml file to recognize files as Tables?

+3  A: 

Hibernate is written against the JDBC API. So, you need a JDBC driver that works with the file format you are interested in. Obviously, even for read-only access, this isn't going to perform well, but it might still be useful if that's not a high priority. On a Windows system, you can set up ODBC datasources for delimited text files, Excel files, etc. Then you can set up the JdbcOdbcDriver in your Java application to use this data source.

For most of the applications I work on, I would not consider this approach; I would use an import/export mechanism to convert from a real database (even if it's an in-process database like Berkeley DB or Derby) to the text files. Yes, it's an extra step, but it could be automated, and the performance isn't likely to be much worse than trying to use the text files directly (it will likely be much better, overall), and it will be more robust and easy to develop.

erickson
A: 

@erickson

I am aware of the problems, but I am working with a legacy database, as changing a small part of the system to adapt it. It become impossible to create such tables for these data that is hard-coded in the old system. So, the best approach so far is to leave them as text, for some admin manipulate and maybe in the future they agree with me and change everything to a more effective resolution.

And, thinking forward, I was hoping to use Hibernate, because, when this day come, The changes could be minimal.

I am trying to take a good approach on an ugly problem.

Fernando Barrocal
A: 

Like erickson said, your only hope is in finding a JDBC driver for that task. There is maybe xlsql (CSV, XML and Excel driver) which could fit the task. After that, you just have to either find or write the most simple Hibernate Dialect which fits your driver.

Damien B
+3  A: 

A quick google came up with

Hope this might provide some inspiration?

toolkit