1 Vote Vote

Need to map classes to different databases at runtime in Hibernate

Posted by topdog 422 days ago Questions| databases userdb classes All

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.

Originally asked by: serg on Stack Overflow

Discuss Bury


Who Voted for this Question