What is a good solution to link different tables in Hibernate based on some field value?

Posted by serg555 on Stack Overflow See other posts from Stack Overflow or by serg555
Published on 2010-05-27T17:09:10Z Indexed on 2010/05/27 17:11 UTC
Read the original article Hit count: 158

Filed under:
|
|

I have article table and several user tables a_user, b_user, ... with exactly the same structure (but different data). I can't change anything in *_user tables except their table name prefix but can change everything else (user tables contain only user information, there is nothing about article or user_type in them).

I need to link article to a user (many-to-one), but user table name is defined by user_type field. For example

Article table record:
...
user_id="5"
user_type="a"

means that it is linked to a user with id=5 from a_user table (id 5 is not unique in users scope, each user table can have its id 5).

Any suggestions how to handle this situation? How can I map this relation in Hibernate (xml mapping, no annotations) so it will automatically pick up correct user for an article during select/update? How should I map user tables (one or multiple classes?)?

I would need to run some queries like this:

from Article a where a.userType=:type and a.user.name=:name

Thanks.

© Stack Overflow or respective owner

Related posts about java

Related posts about hibernate