简体   繁体   English

使用Hibernate处理文本文件

[英]Using Hibernate to work with Text Files

I am using Hibernate in a Java application to access my Database and it works pretty well with MS-SQL and MySQL. 我在Java应用程序中使用Hibernate来访问我的数据库,它在MS-SQL和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. 但是我必须在某些表单上显示的一些数据必须来自文本文件,而文本文件我指的是人类可读文件,它们可以是CSV,Tab-Delimited,甚至是每行的键值对。我的数据就像这样简单,但我当然喜欢XML文件。

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. 我的问题是:我可以使用hibernate来读取这些文件,使用HQL,Query,EntityManager以及Hibernate为我提供访问文件的所有资源。 Which file format should I use and How I configure My persistence.xml file to recognize files as Tables? 我应该使用哪种文件格式以及如何配置My persistence.xml文件以将文件识别为表格?

Hibernate is written against the JDBC API. Hibernate是针对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. 因此,您需要一个能够使用您感兴趣的文件格式的JDBC驱动程序。显然,即使对于只读访问,这也不会很好,但如果不是高优先级,它可能仍然有用。 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. 在Windows系统上,您可以为分隔的文本文件,Excel文件等设置ODBC数据源。然后,您可以在Java应用程序中设置JdbcOdbcDriver以使用此数据源。

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. 我会使用导入/导出机制将真实数据库(即使它是像Berkeley DB或Derby这样的进程内数据库)转换为文本文件。 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. 是的,这是一个额外的步骤,但它可以是自动化的,并且性能不会比尝试直接使用文本文件更差(它可能会更好,总体而言),并且它将更加强大,容易发展。

A quick google came up with 一个快速的谷歌想出来

Hope this might provide some inspiration? 希望这可能会提供一些灵感?

Like erickson said, your only hope is in finding a JDBC driver for that task. 就像埃里克森所说,你唯一的希望就是找到一个用于该任务的JDBC驱动程序。 There is maybe 也许还有 xlsql xlsql (CSV, XML and Excel driver) which could fit the task. (CSV,XML和Excel驱动程序)可以适合任务。 After that, you just have to either find or write the most simple Hibernate Dialect which fits your driver. 在那之后,您只需找到或编写适合您的驱动程序的最简单的Hibernate方言。

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

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