Hibernate还是别的什么? [英] Hibernate or iBatis or something else?

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

问题描述

在我的项目中,我需要在运行期间在数据库之间切换。我试图使用Hibernate,但卡在一个地方,我需要映射对象与数据库中的表。问题是,我有几个表与前缀:documents2001,documents2002 ...正如我所理解的,我不能在运行时映射类与表。我尝试使用iBatis,但问题是数据库在运行时更改。在iBatis,这是很难做。\



也许有些建议,我应该怎么用?



我的要求:




  • 能够在运行时连接到不同的数据库

  • 运行时(如果类映射到表,就像在Hibernate中一样)。



UPDATE:
好的,我尽量解释:
\\
我必须写应用程序,它可以在运行时连接到不同的数据库。用户的app可以选择,要连接哪个数据库。所有数据库具有相同的结构。除此之外,用户可以在数据库中的表之间切换。表具有相同的结构。





  • 为什么我假设,我不能使用Hibernate:在Hibernate类中映射表,所以我不能在运行时更改表。这不允许我选择我可以连接的表。


  • 为什么我假设,我不能使用iBATIS。在iBATIS中,在运行时很难连接到不同的数据库。因此,用户将无法在运行时连接到不同的数据库。



也许还有另一个工具可以使用?

解决方案

在Ibatis中使用动态表名称是微不足道的。只需使用以下表达式:

  SELECT * FROM $ tableName $ 

其中 tableName 是参数类的属性。



<在Hibernate(或任何JPA提供程序)中使用动态表名称是非常困难的,如果不是不切实际(甚至不可能)。这个问题以前出现过。请参见 JPA:如何我在运行时指定一个类对应的表名?



在Ibatis中使用动态数据源需要你写一些代码,但不是全部那么多。基本上,Ibatis工作在 sqlMapClient 的概念,它有一个数据源和一个可以运行的查询列表。只需为每个数据库创建一个 sqlMapClient ,每个数据库都有不同的数据源,并使它们包含所有相同的查询文件(在sql map config中)。



DAO可以这样写,使得它注入了多个 sqlMapClients ,并选择在运行时使用哪一个。这是你必须自己编写的部分,但它很简单。



这是基于在编译时知道数据库。如果数据库在运行时之前是未知的,那么这有点难。它可能仍然是可能的,但我不知道Ibatis如何反应,如果你基本上从运行时交换数据源从 sqlMapClient 。它可能工作,它可能会爆炸。你必须尝试看看。



Hibernate也可以按照相同的原则工作:在DAO中注入多个持久化单元,并在运行时使用正确的单元。使用Hibernate(或任何JPA提供程序),您必须注意确保您的托管对象存储在正确的持久性单元中。我可以很容易地看到这变成了一个噩梦。



一个一般性的注释:不建议沿着动态表名或数据库的路径,重新做,为什么,问问自己这是否可以通过做一些更好的设计选择补救。


In my project i need to switch between databases during runtime. I tried to use Hibernate, but stuck in a place, where i need to map object with table in database. The problem is, that i have several tables with prefix: documents2001, documents2002 ... As i understood, i can't map class with table during runtime. I tried using iBatis, but the problem is in database changing during runtime. In iBatis, it is quite hard to do.\

Maybe some advices, what should i use?

My requirements:

  • Ability to connect to different databases during runtime
  • Ability to change table during runtime(if class is mapped to table, like it is in Hibernate).

UPDATE: Ok, i`ll try to explain:
I have to write application, which can connect to different databases during runtime. User of app can choose, which database to connect. All databases are with same structure. Additional to this, user can switch between tables in database. Tables are with the same structure.

  • Why i assumed, i can not use Hibernate: In Hibernate class is mapped with table, so i can not change table during runtime. This does not allow me to choose table, to which i can connect.
  • Why i assumed, i can not use iBATIS. In iBATIS it is very hard to connect to different database during runtime. So, user will not be able to connect to different database during runtime.

Maybe there is another tool i can use?

解决方案

Working with dynamic table names is trivial in Ibatis. Just use expressions like:

SELECT * FROM $tableName$

where tableName is a property on the parameter class.

Working with dynamic table names in Hibernate (or any JPA provider) is extraordinarily difficult if not impractical (or even impossible). This question has come up before. See JPA: How do I specify the table name corresponding to a class at runtime?.

Working with dynamic data sources in Ibatis will require you to write some code but not all that much. Basically, Ibatis works around the concept of a sqlMapClient, which has a data source and a list of queries that it can run. Just create one sqlMapClient for each database, each with a different data source, and have them include all the same query files (in the sql map config).

A DAO can be written such that it has multiple sqlMapClients injected and it selects which one to use at runtime. This is the part you'll have to write yourself but it's straightforward.

This is predicated on knowing the databases at compile-time. If the database isn't known until runtime then that's a little harder. It might still be possible but I'm not sure how Ibatis will react if you basically swap data sources at runtime from the sqlMapClient. It might work, it might blow up. You'll have to try it and see.

Hibernate may work here too along the same principle: you inject multiple persistence units into a DAO and use the right one at runtime. With Hibernate (or any JPA provider) you will have to watch out for making sure your managed objects are stored in the correct persistence unit. I can easily see this turning into a nightmare actually.

One general comment: it's not advised to go down the path of dynamic table names or databases so really consider what you're doing and why and ask yourself if this could be remedied by making some better design choices.

这篇关于Hibernate还是别的什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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