English 中文(简体)
Hibernate or iBatis or something else?
原标题:

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.

问题回答

Do the tables have the same structure, just different names?

This is definitely possible to map in iBATIS.

You would just need to include something like this in your SQL Map:

<select id="selectAll" resultMap="result" parameterClass="myParameterClass">
    SELECT ColumnA, ColumnB FROM $tableName$
</select>

Assuming that I have understood your question correctly, that should solve your problem.

I don t fully understand your requirement, but this is a possibility:

Both Hibernate and iBatis can work from a DataSource, so you could create your own implementation of javax.sql.DataSource that returns different connections each time getConnection() is called.

Using a DataSource in Hibernate





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签