Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 818281
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T02:07:43+00:00 2026-05-15T02:07:43+00:00

I have article table and several user tables a_user , b_user , … with

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T02:07:43+00:00Added an answer on May 15, 2026 at 2:07 am

    Hibernate has @Any annotation (and <any> xml element) for such designs.

    Sorry for annotations, but I’m not good in hbm.xml. It can be translated into XML, see <any> element:

    @Entity @Table(name = "Article")
    public class Article {
    
        @Any(metaColumn = @Column(name = "user_type"))
        @AnyMetaDef(
                idType = "integer",
                metaType = "string",
                metaValues = {
                        @MetaValue(value = "A", targetEntity = UserA.class),
                        @MetaValue(value = "B", targetEntity = UserB.class)
                }
        )
        @JoinColumn(name = "user_id")
        private User user;
    
        ...
    }
    

    UserA and UserB shares the same schema defined by User:

    @MappedSuperclass
    abstract public class User {
        @Id
        private Long id;
    
        private String name;
    
        ...
    }
    
    @Entity @Table(name = "a_user")
    public class UserA extends User {}
    
    @Entity @Table(name = "b_user")
    public class UserB extends User {}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table for articles with several fields ,each article can have photo/photos
I have had several contexts where table names or schemas were not hard-coded, but
I have an table Article that is in relationship with Steps (OneToMany) and Steps
I have one table, e.g. pricerules, that have stored special prices by article for
I have read few articles about table partioning but still I am bit confused
I have Article table: id | type | date ----------------------- 1 | A |
I have table: Article: ID | TITLE | CONTENT 1 | aaa+s | sdsd
I have a table with this structure (simplified): artID: 1 artName: TNT ArtBrand: ACME
I have an article system which is used by several users. Articles have their
I currently have database setup like so: CREATE TABLE `article` ( `id` int(11) NOT

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.