在 Java 中或一般情况下,独立于数据库的查询达到了极致
假设我有一个应用程序,理想情况下应该能够使用关系数据库、对象数据库、XML 文件或其他任何内容来保存其数据。本着对接口而不是实现进行编码的精神,我有一个通用的 DataStore 接口,它指定涉及数据存储的所有 I/O 的契约。该接口可以通过具体类实现,例如 RDBMSDataStore、OODBMSDataStore、XMLFileDataStore 等。
只要我保持 DataStore 接口的内容简单,即 getThis()
、getThose()
、saveThat()
, updateThis()
等。但是一旦我需要更复杂的查询,它就会崩溃。 XMLFileDataStore 类显然不理解 SQL,而 RDBMSDataStore 类显然不理解 XPath/XQuery。 OODBMSDataStore 根据所使用的 OODBMS 理解完全不同的东西。
我可以采用一种独立于语言的对象查询语言,用其中编写所有查询,然后让具体类将它们翻译成它们的母语,但如果我想完成的话,这是一项艰巨的任务。
Java 中是否有处理这种情况的标准或最佳实践?不幸的是,似乎世界上 99% 的人都将“数据库独立性”解释为“关系数据库独立性”,而完全忽略了对象数据库、XML 数据库、文档数据库等。
Let's say I have an app that should ideally be able to use a relational database, object database, XML files, or whatever to persist its data. In the spirit of coding to interfaces instead of implementations, I have a generic DataStore interface that specifies a contract for all I/O involving the data store. This interface can be implemented by concrete classes such as RDBMSDataStore, OODBMSDataStore, XMLFileDataStore, and so on.
This works well as long as I keep the contents of the DataStore interface simple - i.e. getThis()
, getThose()
, saveThat()
, updateThis()
, etc. But as soon as I require more complicated queries, it breaks down. The XMLFileDataStore class obviously doesn't understand SQL, and the RDBMSDataStore class obviously doesn't understand XPath/XQuery. And OODBMSDataStore understands something entirely different depending on the OODBMS in use.
I could adopt a language-independent object query language, write all my queries in that and then have the concrete classes translate them into their native language, but that's a huge task, if I want to be complete.
Are there standards or best practices for handling this kind of situation in Java? Unfortunately it seems like 99% of the world interprets "database independence" to mean "relational database independence" and ignores the object databases, XML databases, document databases, etc. entirely.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从我阅读问题的方式来看,这听起来很像 Hibernate 为 Java 带来的语义。它甚至具有处理 XML 作为内容后备存储的模式(使用 Dom4J)。 Hibernate API 有许多扩展点,可以允许添加 OODBMS 模型。即使 Hibernate 被证明不是最适合您的解决方案(在实现方面),我认为它提供了一个很好的示例,说明可用于解决您提出的问题的模式类型。
From the way I read the question, this sounds a lot like the semantic that Hibernate brings to the table for Java. It even has mode for dealing with XML as the content backing store (using Dom4J). The Hibernate API has a number of extension points that could allow the addition of an OODBMS model. Even if Hibernate turns out not to be the best solution for you (implementation-wise), I think it provides a good example of the types of patterns that can be used to solve the problems you proposed.