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 679939
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T01:18:56+00:00 2026-05-14T01:18:56+00:00

I have an application that uses Hibernate for its domain objects. One part of

  • 0

I have an application that uses Hibernate for its domain objects. One part of the app is common between a few apps, and it has no knowledge of the other systems. In order to handle relations, our class looks like this:

@Entity
public class SystemEvent {
   @Id @GeneratedValue
   public int entity_id;

   @Column(name="event_type")
   public String eventType;

   @Column(name="related_id")
   public int relatedObjectId;
}

relatedObjectId holds a foreign key to one of several different objects, depending on the type of event. When a system wants to know about events that are relevant to its interests, it grabs all the system events with eventType “NewAccounts” or some such thing, and it knows that all of those relatedObjectIds are IDs to a “User” object or similar.

Unfortunately, this has caused a problem down the line. I can’t figure out a way to tell Hibernate about this mapping, which means that HQL queries can’t do joins. I’d really like to create an HQL query that looks like this:

SELECT users FROM SystemEvent event join Users newUsers where event.eventType = 'SignUp'

However, Hibernate has no knowledge of the relationship between SystemEvent and Users, and as far as I can tell, there’s no way to tell it.

So here’s my question: Is there any way to tell Hibernate about a relationship when your domain objects reference each other via ID numbers and not class references?

  • 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-14T01:18:56+00:00Added an answer on May 14, 2026 at 1:18 am

    First off, when you have an column with a name like ‘eventType’ you are talking about a discriminator column.

    Secondly, a lot of the power/convenience that an ORM provides is the ability to interact with your Business Objects not your Database.

    I would take a look at the Inheritance Chapter in the Hibernate docs.

    If you can, I’d setup an abstract class called SystemEvent which, from your example above, would just have and ID. Each concrete implementation would contain the specific field/property to the ‘relatedObject’. I typically use a ‘table per class’ or ‘table per sub-class’ mapping for this type of scenario.

    @Entity
    @Inheritance(strategy=InheritanceType.JOINED)
    public abstract class SystemEvent {
       @Id @GeneratedValue
       public int eventId;
    }
    
    @Entity
    @PrimaryKeyJoinColumn(name="event_id")
    public class SignupEvent extends SystemEvent {
       @Column(name="user_id")
       public User user;
    }
    
    @Entity
    @PrimaryKeyJoinColumn(name="event_id")
    public class OtherEvent extends SystemEvent {
       @Column(name="other_id")
       public OtherAssociatedObject otherAssociatedObject;
    }
    

    You would modify your HQL to look like this:

    SELECT users FROM SignupEvent event join Users newUsers
    

    Let me know if you want me to clarify any part of this answer.

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

Sidebar

Related Questions

No related questions found

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.