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

  • SEARCH
  • Home
  • 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 6006505
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:32:55+00:00 2026-05-23T01:32:55+00:00

There are many topics on abstract mapping in Hibernate, but I couldn’t find something

  • 0

There are many topics on abstract mapping in Hibernate, but I couldn’t find something that matches my case.

Problem:

My domain model consists of several entities, which don’t inherit from each other. For example:

  • Employee
  • Group
  • Department
  • Report

Where:

Employee <-> Group (n:m),
Employee -> Department (n:1)

As far as this goes, no problem. But:

I want to be able to map Report to either one of Employee, Group or Department (1:1).
(The example is simplified as Report should be mappable to many more different other entities). Every entity must reside in its own table, as different externals want to connect to the database and the model is not to be changed regarding general table structure of the other entities.

What I tried

  1. I tried using the Hibernate Inheritance strategy with Table per Class, whereas User, Employee and Department are subclassed from a new AbstractEntity, sharing ID and Name property from parent class. So User, Employee and Department still have their own tables. I can now map Report <-> AbstractEntity and this works. BUT: As one can expect, this causes heavy performance problems when there are more than about 20 reports or so, as Hibernate internally has to do heavy joining on all subclass tables.

  2. Single Table per Hierarchy is not an option, because of change of the table structure (see Problem). Additionally, all entities only share ID and Name as common properties.

Possible Solution

One possible solution would be to implement the mapping by myself. That is, I store Id and entity class in Report. The getter for AbstractEntity could now decide based on the saved values from which table to load and return the correct entity.

But: Problem here is, I cannot do session stuff in the Entity Class, as the session is not known to the Report class. Also, this would mix up logic and model layer.

Workaround would be to write some kind of wrapper in the DAO, where the correct entity is loaded from the database and injected into the result. But this is very error prone, as every DAO method has to be overridden.

Question

So my question is: Is there a way to somehow catch or intercept the retrieval of the Report Entity from the database and inject loading of the correct AbstractEntity there?

Or is there another, more elegant way to do what I want (must be ;))?

I’m using Hibernate 3.6 using Annotations without EntityManager in a Java EE Environment with DAOs as in http://community.jboss.org/wiki/GenericDataAccessObjects.

  • 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-23T01:32:55+00:00Added an answer on May 23, 2026 at 1:32 am

    I’d take the opposite approach, in that I’d map the Report as a table per class hierarchy, and add a discriminator column to determine if it’s an Employee, Group, or Department report.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There are many similar questions, but I didn't find one that gets straight to
Yes, there are many topics about that, but I still didn't get it. I
It's my first question on SO. I know that there were many topics on
I have seen many questions about the above topics but none that address this.
There are many topics with this title, but none have helped me resolve my
There's been so many topics I've read on this, but I have to just
There are many topics on this and I have tried the solutions, but nothing
I've read every post on stack on similar topics which there is many, but
I know there are many topics with similar issues, but I have not been
How do I do this? There are many topics about this but I tried

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.