使用Hibernate处理文本文件 [英] Using Hibernate to work with Text Files

查看:97
本文介绍了使用Hibernate处理文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个Java应用程序中使用 Hibernate 来访问我的数据库,并且它与MS-SQL一起工作得很好和MySQL。但是,我必须在某些表单上显示的一些数据必须来自文本文件,而通过文本文件我的意思是人类可读文件,它们可以是CSV,Tab-Delimited,甚至每行一个键值对,我的数据就像这样简单,但我的首选当然是XML文件。



我的问题是:我可以使用Hibernate使用HQL,Query,EntityManager读取这些文件以及Hibernate提供的用于访问文件的所有资源。哪个文件格式,我应该使用和如何配置我的persistence.xml文件来识别文件作为表?


解决方案

Hibernate是为了防止写JDBC API。因此,您需要一个能够处理您感兴趣的文件格式的JDBC驱动程序。显然,即使对于只读访问,这也不会很好,但如果不是高优先级,它可能仍然有用。在Windows系统中,您可以设置态ODBC数据源的分隔符的文本文件,Excel文件,等等,那么你可以设置JdbcOdbcDriver在Java应用程序中使用此数据源。



对于我所处理的大多数应用程序,我不会考虑这种方法;我将使用导入/导出机制将真实数据库(即使它是一个进程内数据库,如Berkeley DB或Derby)转换为文本文件。是的,这是一个额外的步骤,但它可以是自动化的,性能不会比尝试直接使用文本文件差得多(它可能会更好,总体而言),它会更强大,易于开发。


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?

解决方案

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.

这篇关于使用Hibernate处理文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆