Need to map classes to different databases at runtime in Hibernate

Posted by serg555 on Stack Overflow See other posts from Stack Overflow or by serg555
Published on 2010-06-09T02:34:28Z Indexed on 2010/06/09 2:42 UTC
Read the original article Hit count: 274

Filed under:
|
|
|

I have MainDB database and unknown number (at compile time) of UserDB_1, ..., UserDB_N databases. MainDB contains names of those UserDB databases in some table (new UserDB can be created at runtime).

All UserDB have exactly the same table names and fields.

How to handle such situation in Hibernate? (database structure cannot be changed).

Currently I am planning to create generic User classes not mapped to anything and just use native SQL for all queries:

session.createSQLQuery("select * from " + db + ".user where id=1")
    .setResultTransformer(Transformers.aliasToBean(User.class));

Is there anything better I can do? Ideally I would want to have mappings for UserDB tables and relations and use HQL on required database.

© Stack Overflow or respective owner

Related posts about java

Related posts about mysql