Need to map classes to different databases at runtime in Hibernate
- by serg555
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.